Retry: Help: Tkinter mainloop rewrite, when do we want to exit/wh ere is the "app.OnExit" callback?

Mike Fletcher mfletch at tpresence.com
Wed Oct 25 16:52:54 EDT 2000


Seems this didn't go through (or the subject wasn't clear enough).  I've
been asked how to run Tkinter under micro-threads.  I only use wxPython, so
I'm not really sure how to write the mainloop substitute for Tkinter.  For
wxPython I use the OnExit handler for the wxApp to kill the mainloop
micro-thread.  Could some Tkinter guru suggest the correct test/event for
when to shut down the mainloop?

Side note: are all the mouse interactions under Tkinter atomic C functions?
For instance, dragging or resizing the window stops all other micro-threads
until the drag/resize stops.

Any pointers appreciated,
Mike

8<_________________ Tkinter test ____________
import uthread9
from Tkinter import *
from _tkinter import dooneevent
def go():
	print 'starting TK'
	global root
	root=Tk()
	while 1: # should use some TK-specific mechanism for determining
when the root is closed, don't know what...
		if not dooneevent( tkinter.DONT_WAIT ):
			uthread9.wait( 0.01 )
	print 'somehow exited loop'

def yo( finishedThread ):
	i=0
	for x in range(1000000):
		if x % 10000 == 0:
			print '.',
	finishedThread.exit()
	print 'exited mainloop'

uthread9.new(yo, uthread9.new(go) )
uthread9.run()

8<______________ wxPython mainloop (from larger codebase) _______
	def MainLoop(self):
		# This outer loop determines when to exit the application,
for
		# this example we let the main frame reset this flag when it
		# closes.
		self.mainloopThread = uthread9.new( self.__mainLoop )
		uthread9.new( self.StartMUTech )
		if not uthread9.microThreadsRunning():
			uthread9.run()
	def __mainLoop( self ):
		try:
			while 1:
				# This inner loop will process any GUI
events until there
				# are no more waiting.
				while self.Pending():
					uthread9.atomic( self.Dispatch )

				# Send idle events to idle handlers.  You
may want to throtle
				# this back a bit so there is not too much
CPU time spent in
				# the idle handlers.  For this example, I'll
just snooze a
				# little...
				uthread9.switchContext()
				uthread9.atomic( self.ProcessIdle )
				uthread9.wait( 0.01 ) # hack!
		except:
			traceback.print_exc()
	def OnExit( self, event ):
		print 'closing mutech'
		self.MUTech.close()
		print 'exiting'
		event.Skip()
		if self.mainloopThread:
			self.mainloopThread.exit()



__________________________________
 Mike C. Fletcher
 Designer, VR Plumber
 http://members.home.com/mcfletch




More information about the Python-list mailing list