473,246 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 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 3231
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

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

Similar topics

0
by: Charles Starrett | last post by:
I am a just a dabbler in programming, but I'd like to make some simple cross-platform tools for generating webpages, as well as for text munging. One thing I need is a Mac OS 9/Windows GUI...
2
by: Michael Schutte | last post by:
I know, questions about Tkinter and threads have been answered very often, but I want to ask anyway. I am using Python 2.2 on a Linux (SuSE Linux 8.1) system. I want to write a server application;...
5
by: george.trojan | last post by:
My application consists of Tkinter GUI that has to communicate with a remote server. The communication is bi-directional: the GUI responds to remote requests and user actions uch as pressing a...
1
by: corrado | last post by:
Hello I have an application running several thread to display some financial data; basically I have a thread displaying HTML tables by means of Tkhtml, another implementing a scrolling ticker...
6
by: RahimAsif | last post by:
Hi guys, I would like some advice on thread programming using C#. I am writing an application that communicates with a panel over ethernet, collects data and writes it to a file. The way the...
9
by: Tuvas | last post by:
I am building a tkinter program. A part of this program is to read data from an incoming interface, and depending on the data, will display a bit of text on the tk dialog, it decodes this data, so...
14
by: Hendrik van Rooyen | last post by:
Hi, I get the following: hvr@LINUXBOXMicrocorp:~/Controller/libpython display.py UpdateStringProc should not be invoked for type font Aborted and I am back at the bash prompt - this is...
2
by: Kevin Walzer | last post by:
I'm trying to decide whether I need threads in my Tkinter application or not. My app is a front end to a command-line tool; it feeds commands to the command-line program, then reads its output and...
2
by: Dudeja, Rajat | last post by:
Hi, So, now I've finally started using Eclipse and PyDev as an IDE for my GUI Application. I just wrote some sample programs as an hands on. Now I would like to take up Tkinter. I'm using...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.