[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

Example 10-21. PP4E\Gui\Tools\threadtools-test-classes.py


tests thread callback queue, but uses class bound methods for action and callbacks


import time
from threadtools import threadChecker, startThread
from tkinter.scrolledtext import ScrolledText


class MyGUI:
def init(self, reps=3):
self.reps = reps # uses default Tk root
self.text = ScrolledText() # save widget as state
self.text.pack()
threadChecker(self.text) # start thread check loop
self.text.bind('', # 3.x need list for map, range ok
lambda event: list(map(self.onEvent, range(6))) )


def onEvent(self, i): # code that spawns thread
myname = 'thread-%s' % i
startThread(
action = self.threadaction,
args = (i, ),
context = (myname,),
onExit = self.threadexit,
onFail = self.threadfail,
onProgress = self.threadprogress)


thread's main action


def threadaction(self, id, progress): # what the thread does


Figure 10-12. Messages from queued callbacks


GUIs, Threads, and Queues | 645
Free download pdf