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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# -*- 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() |