Jun-22-2019, 02:24 PM
Hi ,
I have created a python program based on Tkinter to compare two text files, i need display line number before every line. now i have solution but with this solution alignment will be a problem. I am attaching my code and screen shot of application.
my current solution is i am inserting line number at the beginning of the line as part of the text. i need a better solution
kindly help me.
I have created a python program based on Tkinter to compare two text files, i need display line number before every line. now i have solution but with this solution alignment will be a problem. I am attaching my code and screen shot of application.
my current solution is i am inserting line number at the beginning of the line as part of the text. i need a better solution
kindly help me.
# -*- coding: utf-8 -*- """ Created on Wed Jun 19 14:58:18 2019 @author: Mohamed """ import tkinter as tk import tkinter.filedialog root1= tk.Tk() def comparison(Source,Target): root = tk.Tk() Source_List=[] Target_List=[] with open(Source) as Source_File_Pointer: Source_List=str(Source_File_Pointer.read().splitlines()) with open(Target) as Target_File_Pointer: Target_List=str(Target_File_Pointer.read().splitlines()) tk.Label(root, text=Source_List).pack(side=tk.LEFT) tk.Label(root, text=Target_List).pack(side=tk.RIGHT) root.mainloop() print(Source_List) print(Target_List) def UserInterface(Source,Target): temp="" root = tk.Tk() textContainer = tk.Frame(root, borderwidth=1, relief="sunken") root.title("Comparison") #First Text Window text = tk.Text(textContainer, width=24, height=13, wrap="none", borderwidth=0) textVsb = tk.Scrollbar(textContainer, orient="vertical", command=text.yview) textHsb = tk.Scrollbar(textContainer, orient="horizontal", command=text.xview) text.configure(yscrollcommand=textVsb.set, xscrollcommand=textHsb.set) #Second text window text1 = tk.Text(textContainer, width=24, height=13, wrap="none", borderwidth=0) text1Vsb = tk.Scrollbar(textContainer, orient="vertical", command=text1.yview) text1Hsb = tk.Scrollbar(textContainer, orient="horizontal", command=text1.xview) text1.configure(yscrollcommand=text1Vsb.set, xscrollcommand=text1Hsb.set) text.tag_configure('highlightline', background='yellow',font='Times 10', relief='sunken') text.tag_configure('error', background='yellow' ,foreground='red',font='Times 10 bold', relief='sunken') text1.tag_configure('highlightline', background='yellow',font='Times 10', relief='sunken') text1.tag_configure('error', background='yellow' ,foreground='red',font='Times 10 bold', relief='sunken') text.tag_configure('line',background='grey',relief='sunken') text1.tag_configure('line',background='grey',relief='sunken') with open(Source) as source,open(Target) as target: line_number=1 for line,line1 in zip(source,target): if(line==line1): # I am inserting line number at the begginning of the line here if line_number < 10: temp=str(line_number)+" " elif line_number >= 10: temp=str(line_number) text.insert(tkinter.INSERT,temp,('line')) text1.insert(tkinter.INSERT,temp,('line')) text1.insert(tkinter.INSERT,line1) text.insert(tkinter.INSERT,line) line_number=line_number + 1 print(line) print(line1) else: if line_number < 10: temp=str(line_number)+" " elif line_number >= 10: temp=str(line_number) text.insert(tkinter.INSERT,temp,('line')) text1.insert(tkinter.INSERT,temp,('line')) line_number=line_number+1 for word,word1 in zip(line.strip().split(),line1.strip().split()): if(word==word1): text.insert(tkinter.INSERT,word,('highlightline')) text.insert(tkinter.INSERT," ",('highlightline')) text1.insert(tkinter.INSERT,word1,('highlightline')) text1.insert(tkinter.INSERT," ",('highlightline')) else: text.insert(tkinter.INSERT,word,('error')) text1.insert(tkinter.INSERT,word1,('error')) text.insert(tkinter.INSERT," ",('highlightline')) text1.insert(tkinter.INSERT," ",('highlightline')) text.insert(tkinter.INSERT,"\n") text1.insert(tkinter.INSERT,"\n") # Source_Value="" # Target_Value="" # Source_missing="Extra or Non Matching Records \n" # Target_missing="Extra or Non Matching Records \n" # Missing_Flag="" # with open(Source) as Source_File_Pointer: # Source_List=Source_File_Pointer.read().splitlines() # print(Source_List) # with open(Target) as Target_File_Pointer: # Target_List=Target_File_Pointer.read().splitlines() # print(Target_List) # for source_item in range(len(Source_List)): # Missing_Flag=True # for target_item in range(len(Target_List)): # if(Source_List[source_item]==Target_List[target_item]): # Source_Value=Source_Value+Source_List[source_item]+'\n' # Target_Value=Target_Value+Target_List[target_item]+'\n' # Missing_Flag=False # # if(Missing_Flag==True): # # Source_missing=Source_missing+Source_List[source_item]+'\n' # # for target_item in range(len(Target_List)): # Missing_Flag=True # # for source_item in range(len(Source_List)): # if(Target_List[target_item]==Source_List[source_item]): # Missing_Flag=False # # if(Missing_Flag==True): # Target_missing=Target_missing+Target_List[target_item]+'\n' # # Source_Value=Source_Value+Source_missing # Target_Value=Target_Value+Target_missing # text.grid(row=0, column=0, sticky="nsew") textVsb.grid(row=0, column=1, sticky="ns") textHsb.grid(row=1, column=0, sticky="ew") text1.grid(row=0, column=2, sticky="nsew") text1Vsb.grid(row=0, column=3, sticky="ns") text1Hsb.grid(row=1, column=2, sticky="ew") textContainer.grid_rowconfigure(0, weight=1) textContainer.grid_columnconfigure(0, weight=1) textContainer.grid_columnconfigure(2, weight=1) textContainer.pack(side="top", fill="both", expand=True) root.mainloop() # root = tkinter.Tk(className=" Another way to create a Scrollable text area") # # with open(Source, "r") as Source_File_Poniter: # textPad = ScrolledText(root, width=50, height=40) # textPad.insert(tkinter.INSERT,Source_File_Poniter.read()) # textPad.pack(side=tkinter.LEFT) # # with open(Target, "r") as Target_File_Poniter: # textPad1 = ScrolledText(root, width=50, height=40) # textPad1.insert(tkinter.INSERT,Target_File_Poniter.read()) # textPad1.pack(side=tkinter.RIGHT) # root.mainloop() def Fileselection(): global Source global Target Source="" Target="" def Source_selection(): global Source Source=tkinter.filedialog.askopenfilename(initialdir = "/",title = "Source File",filetypes = (("all files","*.*"),)) def Target_selection(): global Target Target=tkinter.filedialog.askopenfilename(initialdir = "/",title = "Target File",filetypes = (("all files","*.*"),)) Source=tk.Button(root1,text="Source File",command=lambda:Source_selection()).pack() tk.Button(root1,text="Target File",command=lambda:Target_selection()).pack() tk.Button(root1,text="Compare",command=lambda:UserInterface(Source,Target)).pack() print(Source) #UserInterface() Fileselection() root1.title("Comparison") root1.mainloop()