473,695 Members | 2,600 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tkinter wait_variable problem: hangs at termination

I want to support execution of simple user-written scripts in a Tkinter
application. The scripts should be able to wait for data and such
without hanging the GUI (and without having to write the script as a
bunch of asynchronously called subroutines).

I decided to use Tkinter's wait_variable. I built a "script runner"
object that has suitable wait methods. Internally each of these methods
registers a callback that sets a variable when the wait condition is
satisfied, then calls wait_variable to wait until the variable is set.

The resulting scripts are simple and seem to work well, e.g.:

myscript(sr):
# do some normal python stuff that is fast
sr.waitForData( ...)
# more normal fast python stuff
sr.waitForMS(ti me)
# etc.

Unfortunately, if a user closes the root window while wait_variable is
waiting, the application never fully quits. On my unix box the root
window closes but the command-line prompt never returns and ^C is
ignored. The process isn't using excess cpu cycles; it's just not
listening.

I have tried registering an atexit handler and adding a __del__ method
so that the variable being waited on is toggled at shutdown, but it
makes no difference.

Here is an example:
<ftp://ftp.astro.washi ngton.edu/pub/users/rowen/python/WaitBug.py>
Press "Start" to start the script (which simply prints a number to
sys.stdout every second, then quits. Close the root window while the
script is waiting to print the next number, or after pausing the script,
and you'll see the problem.

Any suggestions for how to avoid/work around this problem? If it's a
Tkinter bug I'll report it.

-- Russell

P.S. I saw that some perl/Tk users got around a different problem by
faking wait_variable by running a tight "do one event, see if the wait
condition is satisfied" loop.

I don't think Tkinter allows one to execute a single event, and I
suspect it would be inefficient even if it were possible. I might be
able to use update (though the nice thing about wait_variable is most of
the action happens down at the tcl/tk level, presumably making it
maximally efficient).

(This is one of those rare times I wish Tkinter worked at the C level
the way perl's tk interface does.)
Jul 18 '05 #1
2 3886
<veröffentlic ht & per Mail versendet>

Russell E. Owen wrote:
I want to support execution of simple user-written scripts in a Tkinter
application. The scripts should be able to wait for data and such
without hanging the GUI (and without having to write the script as a
bunch of asynchronously called subroutines).

I decided to use Tkinter's wait_variable. I built a "script runner"
object that has suitable wait methods. Internally each of these methods
registers a callback that sets a variable when the wait condition is
satisfied, then calls wait_variable to wait until the variable is set.

The resulting scripts are simple and seem to work well, e.g.:

myscript(sr):
# do some normal python stuff that is fast
sr.waitForData( ...)
# more normal fast python stuff
sr.waitForMS(ti me)
# etc.

Unfortunately, if a user closes the root window while wait_variable is
waiting, the application never fully quits. On my unix box the root
window closes but the command-line prompt never returns and ^C is
ignored. The process isn't using excess cpu cycles; it's just not
listening.

I have tried registering an atexit handler and adding a __del__ method
so that the variable being waited on is toggled at shutdown, but it
makes no difference.

Here is an example:
<ftp://ftp.astro.washi ngton.edu/pub/users/rowen/python/WaitBug.py>
Press "Start" to start the script (which simply prints a number to
sys.stdout every second, then quits. Close the root window while the
script is waiting to print the next number, or after pausing the script,
and you'll see the problem.

Any suggestions for how to avoid/work around this problem? If it's a
Tkinter bug I'll report it.

-- Russell

P.S. I saw that some perl/Tk users got around a different problem by
faking wait_variable by running a tight "do one event, see if the wait
condition is satisfied" loop.

I don't think Tkinter allows one to execute a single event, and I
suspect it would be inefficient even if it were possible. I might be
able to use update (though the nice thing about wait_variable is most of
the action happens down at the tcl/tk level, presumably making it
maximally efficient).

(This is one of those rare times I wish Tkinter worked at the C level
the way perl's tk interface does.)


Without any deeper insight in your script - could the following meet your
needs?

# instead of atexit.register ():
def dw():
self.cancel()
self._tk.destro y()
self._tk.protoc ol("WM_DELETE_W INDOW", dw)

and further down:

root = Tkinter.Tk()
ScriptRunner._t k = root

That way your runner would get notified if the window shall be closed.

Peter


Jul 18 '05 #2
In article <ca************ *@news.t-online.com>,
Peter Otten <__*******@web. de> wrote:
<veröffentlich t & per Mail versendet>

Russell E. Owen wrote:
I want to support execution of simple user-written scripts in a Tkinter
application. The scripts should be able to wait for data and such
without hanging the GUI (and without having to write the script as a
bunch of asynchronously called subroutines).

I decided to use Tkinter's wait_variable. I built a "script runner"
object that has suitable wait methods...
...
Unfortunately, if a user closes the root window while wait_variable is
waiting, the application never fully quits. On my unix box the root
window closes but the command-line prompt never returns and ^C is
ignored. The process isn't using excess cpu cycles; it's just not
listening.
Without any deeper insight in your script - could the following meet your
needs?

# instead of atexit.register ():
def dw():
self.cancel()
self._tk.destro y()
self._tk.protoc ol("WM_DELETE_W INDOW", dw)

and further down:

root = Tkinter.Tk()
ScriptRunner._t k = root

That way your runner would get notified if the window shall be closed.


This fix does, indeed work! Also, there is an even easier solution: it
turns out to be sufficient to bind to <Destroy>. The callback sets the
variable being waited on and the application no longer hangs.

However, wait_variable proved to unsuitable because if multiple users
use wait_variable, they stack up. If a new routine executes
wait_variable while an existing one is waiting, the new routine has to
fully finish executing before the first one resumes. This is
understandable in hindsight, but disappointing. As a result:
- only one script could be run at a time
- if wait_variable is used elsewhere (and some dialog boxes are reported
to do so) then that would also pause an executing script

I ended up implementing user-written scripts as generators, instead. The
user's script ends up looking like this:
def myscript(sr):
yield sr.doCmd(...)
...
yield sr.waitMS(...)

where sr is a ScriptRunner object. sr's doCmd, waitMS etc. set up a
termination condition that causes the script generator's next() method
to be executed, until the script is finished.

This works fully normally with Tk's event loop. No wait_variable magic
is involved and multiple scripts can peacefully run at the same time.

The big problem is that users are likely to forget the "yield"
statement. To help with this I increment one counter each time a wait
subroutine begins and another counter each time the script generator is
executed. Any discrepancy indicates a missing yield.

-- Russell
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3347
by: Tom Locke | last post by:
Hi All, I'm having trouble with the python shell within emacs. It's hanging when I use tkinter. Setup is: Windows XP emacs 21.3 py-mode 4.6 Recipe:
0
1396
by: Burt Leavenworth | last post by:
Have used TKINTER for years with Python v1.5.2 with great success. But after installing verson 2.0 (and most recently 2.2.3) I have a problem exiting widgets by clicking on the exit button (x on upper right --- )---the system hangs. It's driving me nuts. Can some kind soul indicate what PATH statement they're using which deals with TCL/TK, etc? Burt Leavenworth.
0
3069
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in the context of a main window. The problem does not occur on HPUX or Linux. The following simple example code illustrates the problem and the work around I've come up with; However, I'd like, very much, to get rid of the kludgy work around....
2
3423
by: John Pote | last post by:
Running my programme in Python 2.3.4 I received the following msg in the consol :- (Pent III running W2K prof) """ Exception in Tkinter callback Traceback (most recent call last): File "c:\apps\python\234\lib\lib-tk\Tkinter.py", line 1345, in __call__ return self.func(*args) File "c:\apps\python\234\lib\lib-tk\Tkinter.py", line 459, in callit
4
5076
by: Ivo Woltring | last post by:
Hi Pythoneers, I am trying to make my own gui for mencoder.exe (windows port of the terrific linux mencoder/mplayer) to convert divx to Pocket_PC size. My current app creates a batch script to run the mencoder with the needed params, but now I want to integrate mencoder as a subprocess in my app. What already works: the starting of the subprocess.Popen and the subsequent killing of the process if I want it to.
0
1650
by: Miki Tebeka | last post by:
Hello All, The following script "hangs" on win32 system: from Tkinter import * from tkSimpleDialog import askstring root = Tk() root.withdraw() # <<< Problem here askstring("Yap", "What's up?")
0
3496
by: David | last post by:
I've written a small windows service, and I'm having a problem that I'm spending a lot more time on than I'd like. If anyone has experienced this problem and has any hints or solutions; they would be appreciated. Simply leaving the threadMain() method if something unexpected occurs during thread execution leaves the thread in a ThreadState.Running state. This (apparently??) causes the Thread.Join() in service.OnStop() to hang...
1
2607
by: Ichor | last post by:
This is my code. I've tried a trillion different things. But basically what I want is the radiobuttons to appear. Select one and hit the okay button. Then the screen prints out the var of which button was selected (just to verify). And I want the whole program to wait and do nothing until this process is done. (What I'd want to do next is another radiobutton set, but I'm not there yet.) Help! I've been researching this online all day today....
1
2058
by: kalichakradhar | last post by:
hi i am new to python and Tkinter programming. i am creating a GUI in which i am loading some GIF files and i need to load the GIF files into the GUI dynamically at run time when some flags are set. now the problem is after i create the GUI and call main_loop then i am not getting the control to GUI and the applicaiton hangs. how to get control to the GUI even after main_loop is executed.please help me in finding the solution to this problem....
0
8649
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8586
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9004
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8864
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8838
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6506
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4351
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4592
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2289
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.