Tkinter & threads communication.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dazzler
    New Member
    • Nov 2007
    • 75

    Tkinter & threads communication.

    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 =)

    Code:
    from Tkinter import *
    import threading
    
    master = Tk()                              #make the main window and hide it
    master.minsize(width=300, height=300)
    master.withdraw()
    
    class first_window():
       def __init__(self):
          global start_window
    
          def thread_is_finished(self):
             main_app()  #CONNECTION is made, start main application window
             
             # start_window.withdraw() #APPLICATION HANGS IF I TRY TO HIDE THIS?!?
             # start_window.destroy() #DESTROY DOESN'T WORK EITHER :(
    
          start_window = Toplevel()            #show the starting window
          start_window.minsize ( width=150, height=150 )
          start_window.bind("<<foo>>",thread_is_finished)
          master.update()                      #update (bind will be "saved")
    
          server_start().start()               #start the server in a new thread
    
    class main_app():
       def __init__(self):
          master.deiconify()                   #show the main window
    
    class server_start ( threading.Thread ):
       def run (self):
          global start_window
          #...here's the server socket code...
          start_window.event_generate("<<foo>>") #when finished generate event
    
    master.after_idle(first_window)            #after mainloop start first_window
    master.mainloop()
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    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...

    Comment

    • dazzler
      New Member
      • Nov 2007
      • 75

      #3
      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_gene rate("<<myevent 1>>", when='tail')"


      so for my code:
      Code:
      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?

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by dazzler
        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_gene rate("<<myevent 1>>", when='tail')"
        Thanks for that. Mr Brunel is certainly prolific. Here's the link:
        python mail list
        so for my code:
        Code:
        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.

        Comment

        Working...