473,503 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

6 New Member
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
10 1652
bvdet
2,851 Recognized Expert Moderator Specialist
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
bretthl
6 New Member
nope, not from "Quit".
Mar 1 '15 #3
bvdet
2,851 Recognized Expert Moderator Specialist
So, if you don't mean clicking the 'Quit' button after clicking the 'Start' button, what do you mean?
Mar 1 '15 #4
bretthl
6 New Member
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
dwblas
626 Recognized Expert Contributor
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
bretthl
6 New Member
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
2,851 Recognized Expert Moderator Specialist
It works fine if you remove the while loop. Did you try that?
Mar 2 '15 #8
bretthl
6 New Member
The point was stop the while loop using "Quit" (button2).

Anyone know a good online tkinter resource?
Mar 2 '15 #9
bvdet
2,851 Recognized Expert Moderator Specialist
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
bretthl
6 New Member
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

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

Similar topics

2
26203
by: Rob | last post by:
My first GUI so be gentle... When I start my program I call a class that runs the initial window. While in this class if a certain button is pressed it calls a function outside the class. This...
4
4350
by: Charlie Lesh | last post by:
Hey all: Is there any way to draw on the 'root' window? I'm thinking about a python screensaver for linux. I know that there are xlib bindings for python, maybe a little outdated, but do they...
3
2311
by: Mickel Grönroos | last post by:
Hi everybody, I'm using QuickTimeTcl (3.1) to be able to play movie files in my Tkinter application (Python 2.3.2) on Windows 2000. I was planning to write a simple wrapper class,...
4
6343
by: Tim Jarman | last post by:
Apologies in advance for the long post - I wanted to be sure I included all the relevant details. The answer is probably very, very simple. I am doing something stupid here, but I don't know what...
1
1969
by: snoylr | last post by:
This is my first day working with Tkinter. I'm using python2.3 on WindowsXP. I found this program and entered it. from Tkinter import * class App: def _init_(self,master): frame =...
5
13289
by: mzdude | last post by:
I've just started playing with Python. Installed 2.5 on Windows XP system. I'm working through some of the examples in Programming Python 3ed by Mark Lutz. Given the following example when the Quit...
8
4736
rhitam30111985
by: rhitam30111985 | last post by:
hi all.. python noob here.. i am creating a GUI for an applcation... now i was just testing the working of askstring : from tkSimpleDialog import askstring string=askstring('site','enter site:')...
2
3820
by: Sean DiZazzo | last post by:
Is there any way to open a Tkinter.askopenfilename() without opening a root window alongside the file chooser? I simply want a script to open a dialog and return the chosen file's path to...
4
2149
by: MartinRinehart | last post by:
Everything I've read about Tkinter says you create your window and then call its mainloop() method. But that's not really true. This is enough to launch a default window from the console: ...
1
1114
by: Lie Ryan | last post by:
On Wed, 01 Oct 2008 11:33:59 +0100, dudeja.rajat wrote: The root window is the main window, not the DOS box. I think in windows, you should use pythonw.exe or something to open the python...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5021
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1519
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
397
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.