tkinter + interrupts

A

Ajay

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.

thanks

cheers
 
E

Eric Brunel

Ajay said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top