Multithreading tkinter question

Mark English Mark.English at liffe.com
Wed Dec 15 13:21:48 EST 2004


Is there a safe way to run tkinter in a multithreaded app where the
mainloop runs in a background thread ?
Here's some test code demonstrating the problem. I'm running Python2.4
under Windows 2000.


----------------Code snip starts-------------
from Tkinter import *

def GetTkinterThread():
    import threading
    def TestTkinter():
        def greeting():
            print "Hello stdout world !"

        win = Frame()
        win.pack()
        Label(win, text="Hello container world").pack(side=TOP)
        Button(win, text="Hello", command=greeting).pack(side=TOP)
        Button(win, text="Quit", command=win.quit).pack(side=RIGHT)

        win.mainloop()
    #Run the mainloop in another thread
    t = threading.Thread(None, TestTkinter, 'Test Tkinter Thread')
    t.setDaemon(1)   #Keep going
    t.start()
    print 'Hi'
    return t

t = GetTkinterThread() #This works fine

#Now press the "Hello" button on the Tkinter window
#Now press the return key at the python prompt

----------------Code snip ends-------------
With a debug build the call stack looks like this:
 	python24_d.dll!Py_FatalError(const char * msg=0x1e23ca14)  Line
1513	C
 	python24_d.dll!PyThreadState_Swap(_ts * new=0x0098d0b8)  Line
293 + 0xa	C
 	python24_d.dll!PyEval_RestoreThread(_ts * tstate=0x0098d0b8)
Line 309 + 0x9	C
 	_tkinter_d.pyd!EventHook()  Line 2969 + 0xc	C
 	python24_d.dll!my_fgets(char * buf=0x009ef3e8, int len=100,
_iobuf * fp=0x1027c838)  Line 46	C
 	python24_d.dll!PyOS_StdioReadline(_iobuf * sys_stdin=0x1027c838,
_iobuf * sys_stdout=0x1027c858, char * prompt=0x0087f974)  Line 125 +
0x11	C
	python24_d.dll!PyOS_Readline(_iobuf * sys_stdin=0x1027c838,
_iobuf * sys_stdout=0x1027c858, char * prompt=0x0087f974)  Line 205 +
0x12	C

Is this because of access to sys.stdout ? Or some deeper darker problem
? Is there a right way to achieve this ? Basically I want to be able to
call a function from the Python prompt which creates a Tkinter window
(not necessarily interactive apart from a close button) to display some
data, but leaves the Python prompt active so I can carry on from there.

Haven't found anything about this yet. All sample multithreaded Tkinter
code I've seen uses the main thread as the GUI thread. Also, should I be
posting this to another newsgroup ?

Thanks for any help,
Mark


-----------------------------------------------------------------------
The information contained in this e-mail is confidential and solely 
for the intended addressee(s). Unauthorised reproduction, disclosure, 
modification, and/or distribution of this email may be unlawful. If you 
have received this email in error, please notify the sender immediately 
and delete it from your system. The views expressed in this message 
do not necessarily reflect those of LIFFE Holdings Plc or any of its subsidiary companies.
-----------------------------------------------------------------------




More information about the Python-list mailing list