Thread: Tkinter threads

    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    206
    Rep Power
    12

    Tkinter threads


    Does anybody know how to synchronize Tkinter threads?
    I tried to build a tk widget with and OK button. When the button is pressed the widget should be destroyed and a function from an other module should be invoked.
    Code:
    [...]
    def okPressed(self, event=None):
      self.quit()
      <mymodule>.<myfunction> (<myparams>)
    [...]
    Now the problem is that <myfunction> is be invoked but the widget is not destroyed until the function returns.
    I guess the destroy call is put into a message queue and is processed when the function returns.

    Is there a way to synchronize the calls?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    Originally Posted by Wizard2003
    Does anybody know how to synchronize Tkinter threads?
    I tried to build a tk widget with and OK button. When the button is pressed the widget should be destroyed and a function from an other module should be invoked.
    Code:
    [...]
    def okPressed(self, event=None):
      self.quit()
      <mymodule>.<myfunction> (<myparams>)
    [...]
    Now the problem is that <myfunction> is be invoked but the widget is not destroyed until the function returns.
    I guess the destroy call is put into a message queue and is processed when the function returns.

    Is there a way to synchronize the calls?
    Is there a reason you are threading your app? Part of the reason GUIs are event-based is so the framework can worry about handling multiple events and synchronicity. If you do use threading in a GUI app, you generally don't put stuff that would affect any of the other portions of code in it, just stuff that you don't want to block on.
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.
  4. #3
  5. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    The following code builds up a window, if you click Quit it finishes, if you click Repeat it destroys itself but gets rebuilt.
    Code:
    from Tkinter import *
    class App:
    
        def __init__(self, master):
            self.master = master
            self.again = False
            self.button = Button(master, text="QUIT", fg="red", command=self.quit)
            self.button.pack(side=LEFT)
            self.hi_there = Button(master, text="Repeat", command=self.repeat)
            self.hi_there.pack(side=LEFT)
    
        def quit(self):
            self.master.destroy()
            
        def repeat(self):
            self.again = True
            self.quit()
    
    while True:
        root = Tk()
        app = App(root)
        root.mainloop()
        if not app.again:
            break
  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    This example is similar to the last but uses a wait loop on a child window. The advantage in this code is that the main window remains open even though the child is destroyed and then rebuilt.
    Code:
    from Tkinter import *
    again = False
    class App:
        def __init__(self, master):
            global again
            self.master = master
            again = False
            self.quit_button = Button(self.master, text="QUIT", fg="red", command=self.quit)
            self.quit_button.pack(side=LEFT)
            self.rep_button = Button(self.master, text="Repeat", command=self.repeat)
            self.rep_button.pack(side=LEFT)
            
        def wait(self):
            self.master.wait_window()
            
        def quit(self):
            self.master.destroy()
            
        def repeat(self):
            global again
            again = True
            self.quit()
    
    root = Tk()
    while True:
        mywin = Toplevel()
        app = App(mywin)
        app.wait()
        if not again:
            break
    root.mainloop()
    One of these examples should apply to your problem.
    Grim

IMN logo majestic logo threadwatch logo seochat tools logo