473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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

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 1650
bvdet
2,851 Expert Mod 2GB
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
nope, not from "Quit".
Mar 1 '15 #3
bvdet
2,851 Expert Mod 2GB
So, if you don't mean clicking the 'Quit' button after clicking the 'Start' button, what do you mean?
Mar 1 '15 #4
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 Expert 512MB
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
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 Expert Mod 2GB
It works fine if you remove the while loop. Did you try that?
Mar 2 '15 #8
The point was stop the while loop using "Quit" (button2).

Anyone know a good online tkinter resource?
Mar 2 '15 #9
bvdet
2,851 Expert Mod 2GB
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
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
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
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
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
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
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
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
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
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
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
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.