Here's a crude demo that shows how to print text strings to a Label. Note that long strings are not wrapped. If you need that, you'll have to wrap the strings yourself by inserting \n
newline characters, or use the wraplength
option, as mentioned in the Label widget docs. Alternatively, you could use a Text widget instead of a Label to display the text.
My test
function simulates the action of the code in your "test.py" script.
import tkinter as tk
from time import sleep
# A dummy `test` function
def test():
# Delay in seconds
delay = 2.0
sleep(delay)
print_to_gui('Files currently transferring')
sleep(delay)
print_to_gui('Currently merging all pdfs')
sleep(delay)
print_to_gui('PDFs have been merged')
sleep(delay)
print_to_gui('Finished!\nYou can click the "Run test"\n'
'button to run the test again.')
# Display a string in `out_label`
def print_to_gui(text_string):
out_label.config(text=text_string)
# Force the GUI to update
top.update()
# Build the GUI
top = tk.Tk()
top.wm_title("testest")
top.minsize(width=300, height=150)
top.maxsize(width=300, height=150)
b = tk.Button(top, text='Run test', command=test)
b.config(width=15, height=1)
b.pack()
# A Label to display output from the `test` function
out_label = tk.Label(text='Click button to start')
out_label.pack()
top.mainloop()
Note that we normally do not call time.sleep
in a GUI program because it causes the whole program to freeze while the sleep is occurring. I've used it here to simulate the blocking delay that will occur when your real code is performing its processing.
The usual way to do delays in a Tkinter program that doesn't block normal GUI processing is to use the .after
method.
You will notice that I replaced your from tkinter import *
with import tkinter as tk
. This means we need to type eg tk.Label
instead of Label
, but that makes the code easier to read, since we now know where all the different names come from. It also means we aren't flooding our namespace with all the names that tkinter defines. import tkinter as tk
just adds 1 name to the namespace, from tkinter import *
adds 136 names in the current version. Please see Why is “import *” bad? for further info on this important topic.
Here's a slightly fancier example that appends new text to the current Label text, with word wrapping. It also has a function clear_label
to remove all the text from the Label. Rather than storing the text directly in the Label it uses a StringVar. This makes it easier to access the old text so that we can append to it.
import tkinter as tk
from time import sleep
# A Dummy `test` function
def test():
# Delay in seconds
delay = 1.0
clear_label()
print_to_label('Files currently transferring')
sleep(delay)
print_to_label('Currently merging all pdfs')
sleep(delay)
print_to_label('PDFs have been merged')
sleep(delay)
print_to_label('\nFinished!\nYou can click the "Run test" '
'button to run the test again. '
'This is a very long string to show off word wrapping.'
)
# Append `text_string` to `label_text`, which is displayed in `out_label`
def print_to_label(text_string):
label_text.set(label_text.get() + '\n' + text_string)
# Force the GUI to update
top.update()
def clear_label():
label_text.set('')
top.update()
# Build the GUI
top = tk.Tk()
top.wm_title("testest")
top.minsize(width=300, height=150)
top.maxsize(width=300, height=350)
b = tk.Button(top, text='Run test', command=test)
b.config(width=15, height=1)
b.pack()
# A Label to display output from the `test` function
label_text = tk.StringVar()
out_label = tk.Label(textvariable=label_text, wraplength=250)
label_text.set('Click button to start')
out_label.pack()
top.mainloop()