473,545 Members | 1,118 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 3856
<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
3339
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
1393
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,...
0
3051
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...
2
3402
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...
4
5056
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...
0
1646
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
3489
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...
1
2602
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...
1
2053
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...
0
7456
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...
0
7390
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...
0
7649
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7410
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...
0
4940
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3442
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...
0
3437
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1869
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 we have to send another system
0
692
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.