tkinter halt or stop a function (def) from root window

933 views Asked by At

If a function is called from the root window and the function reaches no solution or the user wants to stop the function, can this be done from the root window and if so how? The following code produces two buttons - Start starts "while" using start() but start() cannot be halted by Quit. Using root.update_idletasks() in start() produces no effect.

#!/usr/bin/env python
from Tkinter import *

def start():
    while True:
      print "Stop me if you can from Quit"
      root.update_idletasks()

root = Tk()
root.title('Example')

button1 = Button(root,text = 'Start', command = start)
button1.grid(row = 0, column = 0)

button2 = Button(root,text = 'Quit', command = root.destroy)
button2.grid(row = 1, column = 0)

root.mainloop()
1

There are 1 answers

0
brett On

REPLACE root.update_idletasks() WITH root.update() and start() kills the root window from button2.

#!/usr/bin/env python
from Tkinter import *

def start():
    while True:
      print "Stop me if you can from Quit"
      root.update()

root = Tk()
root.title('Example')

button1 = Button(root,text = 'Start', command = start)
button1.grid(row = 0, column = 0)

button2 = Button(root,text = 'Quit', command = root.destroy)
button2.grid(row = 1, column = 0)

root.mainloop()