|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Quote:
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. |
|
#3
|
||||
|
||||
|
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 *** |
|
#4
|
||||
|
||||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Tkinter threads |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|