By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

Tkinter & threads communication.

P: 75
My application: "starting window" with some misc. buttons etc. & server socket connection is running at the same time in a new thread.

When connection is made, the starting windows is closed and the main window will be opened.

Problem: I can't close the starting window which is Toplevel window, if I try to withdraw or destroy it, the application freeze.

I'm quite newbie so please try to answer as clearly as possible =)

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import threading
  3.  
  4. master = Tk()                              #make the main window and hide it
  5. master.minsize(width=300, height=300)
  6. master.withdraw()
  7.  
  8. class first_window():
  9.    def __init__(self):
  10.       global start_window
  11.  
  12.       def thread_is_finished(self):
  13.          main_app()  #CONNECTION is made, start main application window
  14.  
  15.          # start_window.withdraw() #APPLICATION HANGS IF I TRY TO HIDE THIS?!?
  16.          # start_window.destroy() #DESTROY DOESN'T WORK EITHER :(
  17.  
  18.       start_window = Toplevel()            #show the starting window
  19.       start_window.minsize ( width=150, height=150 )
  20.       start_window.bind("<<foo>>",thread_is_finished)
  21.       master.update()                      #update (bind will be "saved")
  22.  
  23.       server_start().start()               #start the server in a new thread
  24.  
  25. class main_app():
  26.    def __init__(self):
  27.       master.deiconify()                   #show the main window
  28.  
  29. class server_start ( threading.Thread ):
  30.    def run (self):
  31.       global start_window
  32.       #...here's the server socket code...
  33.       start_window.event_generate("<<foo>>") #when finished generate event
  34.  
  35. master.after_idle(first_window)            #after mainloop start first_window
  36. master.mainloop()
  37.  
Nov 12 '07 #1
Share this Question
Share on Google+
3 Replies


bartonc
Expert 5K+
P: 6,596
Tkinter does not provide a thread-safe environment. What you are attempting is possible, but you'll need a better toolkit. These days, wxPython is what's hot (IMO). Some useful links can be found in this article.

Hope that helps...
Nov 12 '07 #2

P: 75
I FOUND THE SOLUTION, damn I'm happy =D

wise man words: "Eric Brunel wrote:
> This is where the problem is: if you do just a event_generate without
> specifying the 'when' option, the binding is fired immediately in the
> current thread. To be sure that an event is created and that the thread
> switch actually happens, do: "app.event_generate("<<myevent1>>", when='tail')"


so for my code:
Expand|Select|Wrap|Line Numbers
  1. start_window.event_generate("<<foo>>", when='tail')
and now the code is not crashing when trying to close first window

, thx Bartonc but I have about 1300lines of code (about 2weeks work) and I don't want to change GUI now =) and I like using .place for placing gui components ;-D

is wxpython more thread-safe then?
Nov 12 '07 #3

bartonc
Expert 5K+
P: 6,596
I FOUND THE SOLUTION, damn I'm happy =D

wise man words: "Eric Brunel wrote:
> This is where the problem is: if you do just a event_generate without
> specifying the 'when' option, the binding is fired immediately in the
> current thread. To be sure that an event is created and that the thread
> switch actually happens, do: "app.event_generate("<<myevent1>>", when='tail')"
Thanks for that. Mr Brunel is certainly prolific. Here's the link:
python mail list
so for my code:
Expand|Select|Wrap|Line Numbers
  1. start_window.event_generate("<<foo>>", when='tail')
and now the code is not crashing when trying to close first window

, thx Bartonc but I have about 1300lines of code (about 2weeks work) and I don't want to change GUI now =) and I like using .place for placing gui components ;-D

is wxpython more thread-safe then?
I'm impressed that Tkinter is working in this manner. wxPython is a complete toolkit for working on (w)indows and (x) platforms, including threads and processes.
Nov 12 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.