Python Forum
[Tkinter] Line Number Tkinter Text Are
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Line Number Tkinter Text Are
#1
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.

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()
Reply
#2
Quote:alignment will be a problem
Not sure what this means but I would use a Listbox for each column (sorry don't know anything about the Text widget). Also, see Vegaseat's solution using Treeview at https://www.daniweb.com/programming/soft...-in-python
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Updating tkinter text BliepMonster 5 11,932 Nov-28-2022, 01:42 AM
Last Post: deanhystad
  [PyQt] [Solved]Change text color of one line in TextBrowser Extra 2 7,047 Aug-23-2022, 09:11 PM
Last Post: Extra
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 10,285 Jun-26-2022, 06:26 PM
Last Post: menator01
  tkinter change the text of the checkbox zazas321 1 5,362 Sep-17-2021, 06:19 AM
Last Post: zazas321
  Line numbers in Text widget rfresh737 3 7,910 Apr-15-2021, 12:30 PM
Last Post: rfresh737
  tkinter text widget word wrap position chrisdb 6 10,394 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 7,158 Mar-10-2021, 04:21 PM
Last Post: Sir
  [PyGTK] How to center text on multi-line buttons? Lomax 3 5,844 Jan-23-2021, 03:23 PM
Last Post: Lomax
Photo Tkinter TEXT background image _ShevaKadu 5 10,347 Nov-02-2020, 10:34 AM
Last Post: joe_momma
  tkinter | Button color text on Click Maryan 2 4,394 Oct-09-2020, 08:56 PM
Last Post: Maryan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020