I am attempting to call an external function from the press of a button in a Tkinter GUI; the function 'insert's into a Text element. This works perfectly; as long as it is not followed closely by time.sleep(). If it isn't followed, the text updates correctly; if it is, the text does not update, though the function still seems to be running the loop, as it will happily print text to the console.
Here's my example script:
from Tkinter import *
from time import sleep
def test_function(gui_self):
while True:
print "Debug message"
gui_self.main_.insert(END, "Test")
class GUI:
def __init__(self, master):
frame = Frame(master, width=300, height=500)
frame.pack()
self.main_ = Entry(frame, width=80, justify=CENTER)
self.main_.pack()
test_function(self)
root = Tk()
application = GUI(root)
root.mainloop()
This works well. However, changing test_function to the following seems to mess things up:
def test_function(gui_self):
while True:
print "Debug message"
gui_self.main_.insert(END, "Test")
sleep(5)
Any suggestions to fix whatever it is that I am doing wrong here? I'm sure it's just an issue that I, as a fairly basic level user, am not using the correct syntax for.
As a side note, the 'sleep' command also seems to freeze the program entirely, which I find rather annoying and unattractive. Is there a method to pause the loop for a variably set (eg, var = 1(second)) amount of time, other than 'pipe'ing threads, that would be efficient and more.. clean?
Thanks.