加捻是基于GTK的加捻延迟线
twisting的Python项目详细描述
要求
为了有效地扭转,除了 GTK公司。那么我们必须在主目录的开头做以下的导入 应用程序的条目文件:
# twisted install from twisted.internet import gtk2reactor # install the gtk2 reactor gtk2reactor.install() # twisted import from twisted.internet import reactor
在使用传统的gtk主线程启动应用程序之前:
# gtk thread startgtk.main()
现在我们使用扭曲的反应堆启动后,如下所示:
# twisted reactor start reactor.run()
注意:必须在reactor启动之前创建主图形窗口 就像GTK一样。
框架概述
扭曲框架的gui部分提供以下功能:
- ^{tt1}$
- taskbox object: attach to a task to inform about the task progression
- progress window: contains all the task boxes that can be hidden
- progress box: can be use in a parent window to inform about the average progression
- ^{tt2}$
- add task: create a thread and a taskox attach to a working method
- pause/play task: automatically managed by the framework
- stop task: return a flag to the main loop
- remove a taskbox from the progress window (stopped automatically)
nb: each task correspond with a gui widget displaying the progress status of the task
- ^{tt3}$
- result callback: set when we add the task and call with a result by the twisted deferred thread when the worker loop ended
- error callback: set when we add the task and call by the twisted deferred thread on error
- all tasks finished callback: call by the progress window when all tasks are finished, can be used for message display, progress window hide or i don’t know…
框架最佳实践
这里我们将逐步解释框架的标准使用
- 1。创建进度窗口
进度窗口是一个单独的窗口,必须在反应堆之前创建 使用以下行启动或进入gtk工作流:
# progress window import from twisting import ProgressManager # Progress manager init ProgressManager(all_finish_callback=on_all_tasks_finished, ui=True)
- 2为更多进度信息设置进度条
进度条可以添加到每个位置,并将由 根据当前任务显示进度平均值的进度窗口 正在进行中为此,我们创建进度框并将其设置为 进度窗口:
# progress box import from twisting.gui_worker import ProgressManager import gtk # create the box progress_bar = gtk.ProgressBar() # add the box ProgressManager().set_parent(self.main_window, progress_bar)
注意:我们更喜欢将父级设置为进度窗口以使用 gtk的瞬态函数
- 3。添加新任务
我们使用add函数将任务添加到进度窗口。线和 任务框将由 进度窗口:
# add the task ProgressManager().add_task( id_, pretty_name, self.task_function, end_callback=self.end_callback, error_callback=self.error_callback)
一些细节:
- ^{tt4}$ corresponds to an unique id for the task
- ^{tt5}$ will be displayed in the task box widget
- ^{tt6}$ is the function that will be threaded using twisted and that should manage the task loop depending of the twisting framework api
- ^{tt7}$ is the result callback for twisted receiving the task_function result whatever it is
- ^{tt8}$ is the error callback for twisted receiving a twisted object failure in parameter
- 4。线程任务循环函数
循环函数在参数中接收相应的任务框。然后我们 必须通知任务框迭代次数以初始化好的 进度比:
# set the number of iterations taskbox.set_max_pulse(100)
为隐藏线程管理的最大值,我们将使用^ {TT9}$ 由框架给出应该导入一个follow:
# state_machine function import from twisting import state_machine
对于每个任务迭代,此方法将更新progess信息,检查 Pause或Stop事件线程暂停直接在 功能。如果捕捉到事件Stop,则函数返回True,它 表示线程函数必须退出。必须使用调用方法 要轮询任务框事件队列的任务框实例:
# IN THE LOOP # check for stop or pause events if state_machine(taskbox): # we quit return stop_result
最后我们调用taskbox finish方法抛出一个扭曲的线程来停止 任务框中的进度并显示结束消息。然后我们回来 将由twisted发送到结果回调的工作结果:
# call the finish action throw the parent event queue reactor.callFromThread(taskbox.finish) # we finish return end_result