加捻是基于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信息,检查 PauseStop事件线程暂停直接在 功能。如果捕捉到事件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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java JNLP无法在浏览器中正确启动(与dtjava.js一起部署)   在执行下一个方法之前,java将等待线程执行结束   java如何将另一个LayoutManager应用于JComboBox?(多栏JComboBox尝试)   使用jPBC在java中实现双线性配对   java在使用@RequestMapping注释时获取请求的值(URL)   java如何控制流量   java如何获取IFC对象的绝对坐标?   java目标服务器无法使用htmlunit和tor响应异常   java需要帮助创建一个循环结构来运行我的程序   java有可能拥有一个Android APK并在应用程序中更改构建变体吗?   在Sphinx4中运行Ant的java   Java:从ArrayList获取子列表的有效方法   java如何使在循环内部创建的数组在循环外部工作?   apache poi通过java中的XSSF表从单元格读取日期值   安卓 java自己的SeqLock实现,避免spinlock会更好吗?   java的并发底层方法。util。同时发生的预定未来   java比较方法违反了它的一般约定,如何使它具有可传递性?   使用JAVA定向指定类的DB导出子类   一个方法中的java更改特定imageView