471,353 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,353 software developers and data experts.

Tkinter & threads communication.

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
3 3090
bartonc
6,596 Expert 4TB
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
dazzler
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
6,596 Expert 4TB
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.

Similar topics

reply views Thread by Charles Starrett | last post: by
2 posts views Thread by Michael Schutte | last post: by
5 posts views Thread by george.trojan | last post: by
6 posts views Thread by RahimAsif | last post: by
9 posts views Thread by Tuvas | last post: by
14 posts views Thread by Hendrik van Rooyen | last post: by
2 posts views Thread by Kevin Walzer | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.