Skip to main content

File Compare Tool in Python and tkinter

 Below is the desktop application developed in Python to compare 2 files and find out the differences.

Here is the code:

import tkinter as tk

from tkinter import filedialog

from tkinter import ttk

import time

import threading



class TextComparerApp:

    def __init__(self, root):

        self.root = root

        self.root.title("Text Compare: by Vinayak")

        self.root.geometry("800x730")  # Set default window size to 800x730 pixels


        # Frame to hold upload buttons

        self.upload_frame = tk.Frame(root)

        self.upload_frame.pack(pady=10)


        # First file upload button

        self.upload_button1 = tk.Button(self.upload_frame, activebackground="#99e0aa", text="<--- Insert File 1 --->",

                                        command=self.upload_file1)

        self.upload_button1.pack(side=tk.LEFT, padx=(10, 5))


        # Second file upload button

        self.upload_button2 = tk.Button(self.upload_frame, activebackground="#99e0aa", text="<--- Insert File 2 --->",

                                        command=self.upload_file2)

        self.upload_button2.pack(side=tk.RIGHT, padx=(5, 10))


        # Frame to hold text areas

        self.text_frame = tk.Frame(root)

        self.text_frame.pack(pady=(0, 10))


        # Text area 1 to display file contents

        self.text_area1 = tk.Text(self.text_frame, height=15, width=45)

        self.text_area1.pack(side=tk.LEFT, padx=(10, 5))


        # Text area 2 to display file contents

        self.text_area2 = tk.Text(self.text_frame, height=15, width=45)

        self.text_area2.pack(side=tk.RIGHT, padx=(5, 10))


        # Compare button

        self.compare_button = tk.Button(root, text="Compare", bg="#d4ebf2", activeforeground="White",

                                        activebackground="#4dacc9", command=self.start_comparison)

        self.compare_button.pack()


        # Progress bar

        self.progress_bar = ttk.Progressbar(root, orient="horizontal", mode="determinate")

        self.progress_bar.pack(fill=tk.X, padx=10, pady=(0, 5))


        # Text area 3 to display differences

        self.text_area3 = tk.Text(root, height=20, width=80)

        self.text_area3.pack(pady=(0, 5))


        # Clear button

        self.clear_button = tk.Button(root, activebackground="#ff3333", activeforeground="White",

                                      text="Clear Everything", command=self.clear_all)

        self.clear_button.pack()


    def upload_file1(self):

        file_path = filedialog.askopenfilename()

        if file_path:

            self.file1_content = self.read_file_content(file_path)

            self.text_area1.delete("1.0", tk.END)

            self.text_area1.insert(tk.END, self.file1_content)


    def upload_file2(self):

        file_path = filedialog.askopenfilename()

        if file_path:

            self.file2_content = self.read_file_content(file_path)

            self.text_area2.delete("1.0", tk.END)

            self.text_area2.insert(tk.END, self.file2_content)


    def read_file_content(self, file_path):

        with open(file_path, 'r') as file:

            return file.read()


    def start_comparison(self):

        if hasattr(self, 'file1_content') and hasattr(self, 'file2_content'):

            self.compare_button.config(state="disabled")  # Disable the Compare button

            self.progress_bar.start(10)  # Start the progress bar (filling up)

            threading.Thread(target=self.compare_files).start()  # Start comparison in a separate thread

        else:

            tk.messagebox.showinfo("Error", "Please upload both files before comparing.")


    def compare_files(self):

        # Simulate a time-consuming operation

        time.sleep(3)


        differences = self.compare_files_content(self.file1_content, self.file2_content)


        self.progress_bar.stop()  # Stop the progress bar

        self.compare_button.config(state="normal")  # Enable the Compare button again


        self.text_area3.delete("1.0", tk.END)

        self.text_area3.insert(tk.END, differences)


    def compare_files_content(self, file1_content, file2_content):

        differences = ""


        # Split the contents of each file into lines

        lines_file1 = file1_content.splitlines()

        lines_file2 = file2_content.splitlines()


        # Flag to check if there are differences

        has_differences = False


        # Iterate through lines

        for line_num, (line1, line2) in enumerate(zip(lines_file1, lines_file2), start=1):

            if line1 != line2:

                # Add the differences to the string

                differences += f"Difference found at line {line_num}:\n \n"

                differences += f"File 1: {line1}\n \n"

                differences += f"File 2: {line2}\n \n"

                has_differences = True


        # If no differences found, set the message to "NO DIFFERENCES"

        if not has_differences:

            differences = "***NO DIFFERENCES***"


        # If one file has more lines than the other

        if len(lines_file1) > len(lines_file2):

            for extra_line_num in range(len(lines_file2), len(lines_file1)):

                differences += f"Extra line in File 1 at line {extra_line_num + 1}: {lines_file1[extra_line_num]}\n"

        elif len(lines_file2) > len(lines_file1):

            for extra_line_num in range(len(lines_file1), len(lines_file2)):

                differences += f"Extra line in File 2 at line {extra_line_num + 1}: {lines_file2[extra_line_num]}\n"


        return differences


    def clear_all(self):

        self.text_area1.delete("1.0", tk.END)

        self.text_area2.delete("1.0", tk.END)

        self.text_area3.delete("1.0", tk.END)

        if hasattr(self, 'file1_content'):

            del self.file1_content

        if hasattr(self, 'file2_content'):

            del self.file2_content



root = tk.Tk()

app = TextComparerApp(root)

root.mainloop()



# create an exe in python

# pyinstaller --onefile json_compare_2.py

Comments

Popular posts from this blog

Add, remove, search an item in listview in C#

Below is the C# code which will help you to add, remove and search operations on listview control in C#. Below is the design view of the project: Below is the source code of the project: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Treeview_control_demo {     public partial class Form2 : Form     {         public Form2()         {             InitializeComponent();             listView1.View = View.Details;                   }         private void button1_Click(object sender, EventArgs e)         {             if (textBox1.Text.Trim().Length == 0)...

MySQL practical Tutorials part 9- SQL not operator, SQL Not Like, SQL greater than, SQL less than greater than operator

 ========================================================================= Not Equal SELECT title FROM books WHERE released_year = 2017;   SELECT title FROM books WHERE released_year != 2017;   SELECT title, author_lname FROM books;   SELECT title, author_lname FROM books WHERE author_lname = 'Harris';   SELECT title, author_lname FROM books WHERE author_lname != 'Harris'; ========================================================================= Not Like SELECT title FROM books WHERE title LIKE 'W';   SELECT title FROM books WHERE title LIKE 'W%';   SELECT title FROM books WHERE title LIKE '%W%';   SELECT title FROM books WHERE title LIKE 'W%';   SELECT title FROM books WHERE title NOT LIKE 'W%'; ========================================================================= Greater Than SELECT title, released_year FROM books ORDER BY released_year;   SELECT title, released_year FROM books  WHERE released_year > 2000 ORDER BY release...

MULTIPLEXER , Design & Implement the given 4 variable function using IC74LS153. Verify its Truth-Table

TITLE: MULTIPLEXER   AIM: Design & Implement the given 4 variable function using IC74LS153. Verify its Truth-Table.   LEARNING OBJECTIVE: ·        To learn about IC 74153 and its internal structure. ·        To realize 8:1 MUX and 16:1 MUX using IC 74153.   COMPONENTS REQUIRED: IC 74153, IC 7404, IC 7432, CDS, wires, Power supply. IC PINOUT:            1)     IC 74153 2)      IC 7404:                                              3) IC 7432 THEORY:   ·        Multiplexer is a combinational circuit that is one of the most widely used in digital design. ·        The multiplexer is a data selector which gates one out of several inputs to a sin...