Re: tkinter + interrupts
From: Eric Brunel (eric_brunel_at_despammed.com)
Date: 09/15/04
- Next message: Fritz Bosch: "(macro) recorder for service requests in a model-view-controller architecture"
- Previous message: jack: "python task manager"
- In reply to: Ajay: "tkinter + interrupts"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 15 Sep 2004 11:41:05 +0200
Ajay wrote:
> hi!
>
> on my gui, i have a "start server" button and a "stop server" button.
> the problem is "start server" will loop infinitely and respond to requests.
> i'd like to be able to click on "stop server" and get the server to stop.
>
> how would i go about doing it? i can think of a soln involving threads,
> where i have "start server" in a separate thread and have it continously
> poll a shared variable. but is there another solution not involving
> threads.
It's possible, but it may be a bit weird. All you have to do is return the
control back to the Tkinter mainloop at regular intervals via the update method
so that the GUI remains active. Here is an example:
--------------------------------------------------------
import time
from Tkinter import *
root = Tk()
lbl = Label(root, text='Blink')
lbl.pack(side=TOP)
doLoop = 1
def start():
x = 0
while doLoop:
if x:
lbl.configure(fg='red')
else:
lbl.configure(fg='black')
x = not x
time.sleep(1)
root.update()
def stop():
global doLoop
doLoop = 0
Button(root, text='Start', command=start).pack(side=LEFT)
Button(root, text='Stop', command=stop).pack(side=LEFT)
root.mainloop()
--------------------------------------------------------
This is a bit weird, since the GUI is not *always* active; it is only once every
second. So clicking the 'Stop' button may have a one second delay before being
taken into account.
Also remember that the example above is very simple: in real life, you may not
have the occasion to call the update method regularly enough. So a solution
involving threads is sometimes hard to avoid...
HTH
-- - Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> - PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
- Next message: Fritz Bosch: "(macro) recorder for service requests in a model-view-controller architecture"
- Previous message: jack: "python task manager"
- In reply to: Ajay: "tkinter + interrupts"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]