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.

# -*- 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 5,663 Nov-28-2022, 01:42 AM
Last Post: deanhystad
  [PyQt] [Solved]Change text color of one line in TextBrowser Extra 2 4,761 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 4,667 Jun-26-2022, 06:26 PM
Last Post: menator01
  tkinter change the text of the checkbox zazas321 1 3,753 Sep-17-2021, 06:19 AM
Last Post: zazas321
  Line numbers in Text widget rfresh737 3 5,316 Apr-15-2021, 12:30 PM
Last Post: rfresh737
  tkinter text widget word wrap position chrisdb 6 7,458 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 5,541 Mar-10-2021, 04:21 PM
Last Post: Sir
  [PyGTK] How to center text on multi-line buttons? Lomax 3 4,155 Jan-23-2021, 03:23 PM
Last Post: Lomax
Photo Tkinter TEXT background image _ShevaKadu 5 7,650 Nov-02-2020, 10:34 AM
Last Post: joe_momma
  tkinter | Button color text on Click Maryan 2 3,314 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