Tkinter / Threads: "Invalid command name"

M

Michael Schutte

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; like telnet is a client. The user
should be able to bind() to a port and wait for a client. The written
and recieved data is stored in a Text widget, self.__text. The
accept()ing is done in a seperate thread (using the threading module),
in method self.__tryConnect().
But the seperate thread raises an exception at
self.__text["state"]="normal" .
A part of the code:

def go(self, portn):
# Open the window and connect.

# Bind self.__server to ("", portn), call listen(), ...

# When everything's ok, start the accept()ing thread.
thr=threading.Thread(target=self.__tryConnect)
thr.start()

def __tryConnect(self):
# Accept a connection
success=1
self.__text["state"]="normal" # Here the exception is raised
self.__text.insert("end", "Waiting for connection... ")
try: self.__client, addr=self.__server.accept()
except: success=0
if not success:
self.__text.insert("end", "failed.\n")
else:
self.__text.insert("end", "ok, connected with %s.\n" % addr[0])
self.__text["state"]="disabled"

But when I join() the thread, everything works as it should. Eventually
the self.__text reference is lost when __tryConnect() returns?
The exception is:

Exception in thread Thread-1:
Traceback (most recent call last):
...
File "...", line xx, in configure:
self.tk.call((self.w, configure)
TclError: invalid command name ".123123123.12341234" ,

while ".123123123.12341234" is the text widgets identification.
Can anyone help me?

Michael Schutte <[email protected]>
 
R

Russell E. Owen

Michael Schutte said:
...I want to write a server application; like telnet is a client. The user
should be able to bind() to a port and wait for a client. The written
and recieved data is stored in a Text widget, self.__text. The
accept()ing is done in a seperate thread (using the threading module),
in method self.__tryConnect().
But the seperate thread raises an exception at
self.__text["state"]="normal" .
A part of the code:

You should only deal with Tkinter from the main thread.

(That was most certainly true in the past. I'm not quite positive that
it is still supposed to be true, but I can assure you that it doesn't
work well on my system.)

I suggest you use a Lock or Queue or some other inter-thread
communication object to let the accept thread communicate with the rest
of the application.

-- Russell

(You might also want to use Twisted framework.)
 
E

Eric Brunel

Russell said:
...I want to write a server application; like telnet is a client. The user
should be able to bind() to a port and wait for a client. The written
and recieved data is stored in a Text widget, self.__text. The
accept()ing is done in a seperate thread (using the threading module),
in method self.__tryConnect().
But the seperate thread raises an exception at
self.__text["state"]="normal" .
A part of the code:


You should only deal with Tkinter from the main thread.

(That was most certainly true in the past. I'm not quite positive that
it is still supposed to be true, but I can assure you that it doesn't
work well on my system.)

I suggest you use a Lock or Queue or some other inter-thread
communication object to let the accept thread communicate with the rest
of the application.

I usually use the event_generate method with custom events on Tkinter widgets
from secondary threads to report events to the main thread. This seems to work
well on all platforms I've worked with (Linux, Windows, Solaris). The
communication via a Lock or Queue is a bit less "natural", since you have to
check regularly if a secondary thread tries to tell you something.

My ¤0.02. HTH.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top