Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th-1:00PM EST. Register Today!
  #1  
Old February 18th, 2004, 07:22 AM
Wizard2003's Avatar
Wizard2003 Wizard2003 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 206 Wizard2003 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 29 m 24 sec
Reputation Power: 6
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?

Reply With Quote
  #2  
Old February 18th, 2004, 07:59 AM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 7
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
Quote:
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.

Reply With Quote
  #3  
Old February 18th, 2004, 04:41 PM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
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
__________________
*** Experimental Python Markup CGI V2 ***

Reply With Quote
  #4  
Old February 18th, 2004, 04:47 PM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Tkinter threads


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway