Bottom Page

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()
Quote
#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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  tkinter how to unselect text in textbox Battant 0 79 Feb-17-2020, 02:00 PM
Last Post: Battant
  Make Label Text background (default color) transparent using tkinter in python barry76 1 2,251 Nov-28-2019, 10:19 AM
Last Post: Larz60+
  [Tkinter] tkinter progress bar for decending number justaloser 1 387 Nov-10-2019, 12:31 AM
Last Post: Larz60+
  Active tkinter text output during loop dvanommen 2 980 Oct-18-2019, 02:23 PM
Last Post: dvanommen
  Part of code is adding extra new line when saving to text file. lovepeace 9 381 Aug-24-2019, 12:52 PM
Last Post: lovepeace
  Unable to update or refresh label text in tkinter jenkins43 3 1,257 Jul-24-2019, 02:09 PM
Last Post: Friend
  how to insert image into Text widget Tkinter atlass218 5 2,595 Apr-17-2019, 05:28 AM
Last Post: atlass218
  [Tkinter] How do I center this text in tkinter? ejected 4 10,552 Mar-29-2019, 10:35 PM
Last Post: woooee
  [PyQt] Assign Label Text as number in Hz mekha 17 2,726 Jul-18-2018, 12:47 AM
Last Post: mekha
  How to delete text from a tkinter Text widget? Tang 1 17,683 May-20-2018, 09:26 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)