Home > database >  Python tkinter print result can't complete
Python tkinter print result can't complete

Time:10-07

import filecmp
import os
import tkinter as tk
import tkinter.font as tkFont
from tkinter import *

def gg():
    disk = (number_entry.get())

    if disk == "2" :
        DIRS = [r'C:\Newtest\1', r'C:\Newtest\2', r'C:\Newtest\3',r'C:\Newtest\4',r'C:\Newtest\5',r'C:\Newtest\6',r'C:\Newtest\7',r'C:\Newtest\8',r'C:\Newtest\9',r'C:\Newtest\10']
        FILES = [('copy1.txt', 'copy2.txt'), ('fcmp1.txt', 'fcmp2.txt'), ('filecp1.txt', 'filecp2.txt')]

        for e, dir in enumerate(DIRS, 1):
            cmp = True
            for file in FILES:
                try:
                    if not filecmp.cmp(os.path.join(dir, file[0]), os.path.join(dir, file[1])):
                        cmp = False
                        break
                except Exception as ex:
                        print(f'Error -> {ex}', file=sys.stderr)
                        continue
            x = (f'E_{e} compare {"pass" if cmp else "fail"}')
            print(x)
            result_label.configure(text=x,wraplength=300,font=fontExample3)

        DIRS = [r'C:\Newtest\1', r'C:\Newtest\2', r'C:\Newtest\3',r'C:\Newtest\4',r'C:\Newtest\5',r'C:\Newtest\6',r'C:\Newtest\7',r'C:\Newtest\8',r'C:\Newtest\9',r'C:\Newtest\10']
        FILES = [('copy1.txt', 'copy2.txt'), ('fcmp1.txt', 'fcmp2.txt'), ('filecp1.txt', 'filecp2.txt')]

        for e, dir in enumerate(DIRS, 1):
            cmp = True
window = tk.Tk()
window.title('XXX')
window.geometry('1200x700')
window.configure(background='Azure')


fontExample = tkFont.Font(family="Segoe UI", size=20)
fontExample2 = tkFont.Font(family="Segoe UI", size=10)
fontExample3 = tkFont.Font(family="Segoe UI", size=15)
header_label = tk.Label(window, text='XXXX',font=fontExample)
header_label.pack()

number_frame = tk.Frame(window)
number_frame.pack(side=tk.TOP)
number_label = tk.Label(number_frame, text='Number of disks',font=fontExample2)
number_label.pack(side=tk.LEFT)
number_entry = tk.Entry(number_frame)
number_entry.pack(side=tk.LEFT)
result_label = tk.Label(window)
result_label.pack()
calculate_btn = tk.Button(window, text='First calculate', command=gg,font=fontExample3)
calculate_btn.pack()

window.mainloop()

Dear All ,

I have the question in the line: result_label.configure(text=x,wraplength=300,font=fontExample3)

I want the result can print complete like my console.
Consele result is :

E_1 compare pass  
E_2 compare fail  
E_3 compare pass  
E_4 compare pass  
E_5 compare pass  
E_6 compare pass
E_7 compare pass
E_8 compare pass
E_9 compare pass
E_10 compare fail

But in the tkinter result is -> E_10 compare fail

Please tolerate me as a newbie in tkinter.

Thanks!

CodePudding user response:

If you want to add to the text that is already on the label, you can do this:

result_label.configure(text=result_label['text']   x   '\n')

result_label['text'] returns the current text of the widget (equivalent to result_label.cget('text'))

You also don't need to configure wraplength and font each time, just do it once when creating the label like this:

result_label = tk.Label(window, wraplenght=300, font=fontExample3)

Also:
I strongly advise against using wildcard (*) when importing something, You should either import what You need, e.g. from module import Class1, func_1, var_2 and so on or import the whole module: import module then You can also use an alias: import module as md or sth like that, the point is that don't import everything unless You actually know what You are doing; name clashes are the issue.

I strongly suggest following PEP 8 - Style Guide for Python Code. Function and variable names should be in snake_case, class names in CapitalCase. Have two blank lines around function and class declarations.

  • Related