tkinter + interrupts

Discussion in 'Python' started by Ajay, Sep 15, 2004.

  1. Ajay

    Ajay Guest

    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





    ----------------------------------------------------------------
    This message was sent using IMP, the Internet Messaging Program.
    Ajay, Sep 15, 2004
    #1
    1. Advertising

  2. Ajay

    Eric Brunel Guest

    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
    Eric Brunel, Sep 15, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. D.Sn

    Interrupts in VB (API)

    D.Sn, Jan 12, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    2,754
    Willy Denoyette [MVP]
    Jan 12, 2004
  2. Ciaran
    Replies:
    12
    Views:
    6,050
    Frank van Eijkelenburg
    Feb 27, 2004
  3. guru10
    Replies:
    1
    Views:
    1,197
  4. Victor Bazarov

    Keyboard interrupts

    Victor Bazarov, Mar 28, 2005, in forum: C++
    Replies:
    8
    Views:
    8,766
    Ioannis Vranos
    Mar 29, 2005
  5. engsol
    Replies:
    8
    Views:
    687
    Peter Hansen
    Jan 13, 2005
Loading...

Share This Page