By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,903 Members | 1,856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,903 IT Pros & Developers. It's quick & easy.

How to structure tkinter to terminate command=def() from root window

P: 6
The following builds a two button window. "Run" will not "Quit". I have a structure problem. Can anyone please suggest a workaround.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python
  2. from Tkinter import *
  3.  
  4. def start():
  5.     while True:
  6.       print "Stop me if you can from Quit"
  7.       root.update_idletasks()
  8.  
  9. root = Tk()
  10. root.title('Example')
  11.  
  12. button1 = Button(root,text = 'Start', command = start)
  13. button1.grid(row = 0, column = 0)
  14.  
  15. button2 = Button(root,text = 'Quit', command = root.destroy)
  16. button2.grid(row = 1, column = 0)
  17.  
  18. root.mainloop()
Feb 26 '15 #1
Share this Question
Share on Google+
10 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Expand|Select|Wrap|Line Numbers
  1. def start():
  2.     print "Stop me if you can from Quit"
  3.     root.after(100, start)
Feb 26 '15 #2

P: 6
nope, not from "Quit".
Mar 1 '15 #3

bvdet
Expert Mod 2.5K+
P: 2,851
So, if you don't mean clicking the 'Quit' button after clicking the 'Start' button, what do you mean?
Mar 1 '15 #4

P: 6
What you suggested did not work.

I have found that by changing "root.update_idletasks()" to
root.update() works. I have some tk learning to do. Thanks.
Mar 2 '15 #5

Expert 100+
P: 621
It works for me using "after". Did you leave in the "while True" statement so it still hangs? In any case you have to be more specific than "What you suggested did not work" for more suggestions.
Mar 2 '15 #6

P: 6
The following works. I need to figure out the difference between root.update() and root.update_idletasks().

Expand|Select|Wrap|Line Numbers
  1.  
  2.     #!/usr/bin/env python
  3.     from Tkinter import *
  4.  
  5.     def start():
  6.         while True:
  7.           print "Stop me if you can from Quit"
  8.           root.update()
  9.  
  10.     root = Tk()
  11.     root.title('Example')
  12.  
  13.     button1 = Button(root,text = 'Start', command = start)
  14.     button1.grid(row = 0, column = 0)
  15.  
  16.     button2 = Button(root,text = 'Quit', command = root.destroy)
  17.     button2.grid(row = 1, column = 0)
  18.  
  19.     root.mainloop()
  20.  
  21.  
Mar 2 '15 #7

bvdet
Expert Mod 2.5K+
P: 2,851
It works fine if you remove the while loop. Did you try that?
Mar 2 '15 #8

P: 6
The point was stop the while loop using "Quit" (button2).

Anyone know a good online tkinter resource?
Mar 2 '15 #9

bvdet
Expert Mod 2.5K+
P: 2,851
While not complete (for example it doesn't cover pack or place geometry), this is my go to for reference.

Regarding widget.update():
"This method forces the updating of the display. It should be used only if you know what you're doing, since it can lead to unpredictable behavior or looping. It should never be called from an event callback or a function that is called from an event callback.

In your solution, it exits the while loop, but does so by creating an exception. You will need to catch the exception or find a better way.
Mar 2 '15 #10

P: 6
Oh wow, thanks for that reference! From my alma matter (1990).

I noticed the exception when run from terminal. Your right, I need work that. I will give that reference a good read before proceeding further.
Mar 2 '15 #11

Post your reply

Sign in to post your reply or Sign up for a free account.