[Pythonmac-SIG] Threading with Tkinter on OSX - Bus Error

Peter de Tagyos pdetagyos@cox.net
Sat, 20 Jul 2002 14:02:29 -0700


Your modifications did the trick.  Thanks a lot!  For some reason I was 
thinking that when the delegate.message() routine was being called, it 
was switching threads.  Duuuh.

I'll play around with it a bit, and try to figure out a generic message 
forwarding design.  I'll let you know what I come up with, in case 
anyone is interested.

Thanks again for the speedy reply!

Peter



On Saturday, July 20, 2002, at 12:36 PM, Tony Lownds wrote:

> # 
> --------------------------------------------------------------------------
> # -- 
> buserr.py -------------------------------------------------------------
>
> from Tkinter import *
> from threading import *
> import time
> import Queue
>
> # Class 
> frm ----------------------------------------------------------------
> class frm(Frame):
>
> 	# Builtins ---------------------------------------------------------
> 	def __init__(self, parent=None):
> 		self.queue = Queue.Queue(10)
> 		Frame.__init__(self, master=parent)
> 		if ((parent.__class__ == Toplevel) or (parent.__class__ == Tk)):
> 			parent.title("Testing")
> 			parent.geometry("400x300")
> 		self.pack()
> 		self.__createGUI()
>
> 		# Start the message thread
> 		msgTh = Thread(target=msgThread, args=(self,))
> 		msgTh.start()
>
> 	# Public Interface --------------------------------------------------
> 	def message(self, msg):
> 		"""Add the given message to the text; may fail with "Full" """
> 		self.queue.put((self.__message, msg))
>
> 	def runloop(self):
> 		"""similar to mainloop, but also checks our Queue"""
> 		while 1:
> 			try:
> 				dispatch = self.queue.get_nowait()
> 			except Queue.Empty:
> 				root.tk.dooneevent()
> 			else:
> 				apply(dispatch[0], dispatch[1:])
>
> 	# Private Methods ---------------------------------------------------
> 	def __createGUI(self):
> 		"""Create all the GUI components"""
> 		self.txtFoo = Text(self)
> 		self.txtFoo.pack(side=TOP, fill=X, expand=1, padx=10)
>
> 	def __message(self, msg):
> 		"""Add the given message to the text"""
>                 currText = self.txtFoo.get(1.0, END)
>                 newText = currText + msg
> 		print "About to explode..."
>                 self.txtFoo.insert(END, newText)
> 		print "We never get here!"
>
>
> # Thread 
> routine ------------------------------------------------------------
> def msgThread(delegate):
> 	time.sleep(3)
> 	delegate.message("Testing")
>
>
> # 
> Main ----------------------------------------------------------------------
> root = Tk()
> gui = frm(root)
> gui.runloop()