pythonqt线程更新程序,使用单独的线程更新GUI项。

qt-thread-updater的Python项目详细描述


pythonqt线程更新程序使用单独的线程更新GUI项。在

这个库允许您从一个单独的线程高效地更新qtgui元素。Qt-GUI元素不是线程 安全。方法调用,如标签.setText不要在单独的线程中工作。这个图书馆解决了这个问题。在

公用事业

ThreadUpdater提供了几个实用程序来帮助更新小部件的值。在

  • call_latest - Call the given function with the most recent value in the main thread using the timer.
    • It is safe to call this many times with the same function.
    • If the given function is called multiple times it is only called once with the most recent value.
  • call_in_main - Call the given function in the main thread using the timer.
    • Every time you call this function the given function will be called in the main thread
    • If the given function is called multiple times it will be called every time in the main thread.
    • If this function is called too many times it could slow down the main event loop.
  • register_continuous - Register a function to be called every time the ThreadUpdater.update method is called.
    • The timeout variable (in seconds) indicates how often the registered functions will be called.
  • delay - Call a function after the given number of seconds has passed.
    • This will not be accurate. Accuracy can be improved by lowering the timeout to increase how often the timer runs.

线程更新程序示例

下面是一些通常如何使用ThreadUpdater的示例。在

简单线程示例

下面的示例告诉要运行的更新lbl.SETEXT文本在具有最新值的主线程中。在

importtimeimportthreadingfromqtpyimportQtWidgetsfromqt_thread_updaterimportget_updaterapp=QtWidgets.QApplication.instance()orQtWidgets.QApplication([])lbl=QtWidgets.QLabel("Latest Count: 0")lbl.resize(300,300)lbl.show()data={'counter':0}defrun(is_alive):is_alive.set()whileis_alive.is_set():text='Latest Count: {}'.format(data['counter'])get_updater().call_latest(lbl.setText,text)data['counter']+=1time.sleep(0.001)# Not needed (still good to have some delay to release the thread)alive=threading.Event()th=threading.Thread(target=run,args=(alive,))th.start()app.exec_()alive.clear()

持续更新示例

下面的示例每次线程更新程序.update()从计时器调用。 如果没有要更新标签的新数据,这可能效率低下。在

^{pr2}$

调用主要示例

下面的示例每次都调用append函数。它可能没有效率。在

importtimeimportthreadingfromqtpyimportQtWidgetsfromqt_thread_updaterimportget_updaterapp=QtWidgets.QApplication.instance()orQtWidgets.QApplication([])text_edit=QtWidgets.QTextEdit()text_edit.resize(300,300)text_edit.setReadOnly(True)text_edit.show()data={'counter':0}defrun(is_alive):is_alive.set()whileis_alive.is_set():text='Main Count: {}'.format(data['counter'])get_updater().call_in_main(text_edit.append,text)data['counter']+=1time.sleep(0.01)# Some delay/waiting is requiredalive=threading.Event()th=threading.Thread(target=run,args=(alive,))th.start()app.exec_()alive.clear()

延迟示例

下面的示例在经过X秒数后调用append函数。延迟功能不会 精确,但保证函数在X秒数后被调用。为了提高准确度 ThreadUpdater使其以更快的速度运行的超时时间更短。在

importtimeimportthreadingfromqtpyimportQtWidgetsfromqt_thread_updaterimportget_updaterapp=QtWidgets.QApplication.instance()orQtWidgets.QApplication([])text_edit=QtWidgets.QTextEdit()text_edit.resize(300,300)text_edit.setReadOnly(True)text_edit.show()now=time.time()defupdate_text(set_time):text_edit.append('Requested {:.04f} Updated {:.04f}'.format(set_time,time.time()-now))# Lower the timeout so it runs at a faster rate.get_updater().timeout=0# 0.0001  # Qt runs in millisecondsget_updater().delay(0.5,update_text,0.5)get_updater().delay(1,update_text,1)get_updater().delay(1.5,update_text,1.5)get_updater().delay(2,update_text,2)get_updater().delay(2.5,update_text,2.5)get_updater().delay(3,update_text,3)app.exec_()

小工具

我决定在这个库中包含一些有用的Qt小部件。在

  • QuickPlainTextEdit - Used to display fast streams of data
  • QuickTextEdit - Display fast streams of data with color.

快速明文编辑

快速显示来自单独线程的数据。在

importtimeimportthreadingfromqtpyimportQtWidgetsfromqt_thread_updater.widgets.quick_text_editimportQuickPlainTextEditapp=QtWidgets.QApplication.instance()orQtWidgets.QApplication([])text_edit=QuickPlainTextEdit()text_edit.resize(300,300)text_edit.show()data={'counter':0}defrun(is_alive):is_alive.set()whileis_alive.is_set():text='Main Count: {}\n'.format(data['counter'])text_edit.write(text)data['counter']+=1time.sleep(0.0001)# Some delay is usually required to let the Qt event loop run (not needed if IO used)alive=threading.Event()th=threading.Thread(target=run,args=(alive,))th.start()app.exec_()alive.clear()

快速文本编辑

使用颜色快速显示来自单独线程的数据。在

importtimeimportthreadingfromqtpyimportQtWidgetsfromqt_thread_updater.widgets.quick_text_editimportQuickTextEditapp=QtWidgets.QApplication.instance()orQtWidgets.QApplication([])text_edit=QuickTextEdit()text_edit.resize(300,300)text_edit.show()data={'counter':0}defrun(is_alive):is_alive.set()whileis_alive.is_set():text='Main Count: {}\n'.format(data['counter'])text_edit.write(text,'blue')data['counter']+=1time.sleep(0.0001)# Some delay is usually required to let the Qt event loop run (not needed if IO used)alive=threading.Event()th=threading.Thread(target=run,args=(alive,))th.start()app.exec_()alive.clear()
{id10}$

QuickTextEdit重定向

用颜色在QTextEdit中显示打印(stdout和stderr)。在

importsysimporttimeimportthreadingfromqtpyimportQtWidgetsfromqt_thread_updater.widgets.quick_text_editimportQuickTextEditapp=QtWidgets.QApplication.instance()orQtWidgets.QApplication([])text_edit=QuickTextEdit()text_edit.resize(300,300)text_edit.show()sys.stdout=text_edit.redirect(sys.__stdout__,color='blue')sys.stderr=text_edit.redirect(sys.__stderr__,color='red')data={'counter':0}defrun(is_alive):is_alive.set()whileis_alive.is_set():stdout_text='Main Count: {}'.format(data['counter'])# Print gives \n automaticallyerror_text='Error Count: {}'.format(data['counter'])# Print gives \n automatically# Print automatically give '\n' with the "end" keyword argument.print(stdout_text)# Print will write to sys.stdout where the rediect will write to text_edit and stdoutprint(error_text,file=sys.stderr)# Print to sys.stderr. Rediect will write to text_edit and stderrdata['counter']+=1# Some delay is usually desired. print/sys.__stdout__ uses IO which gives time for Qt's event loop.# time.sleep(0.0001)alive=threading.Event()th=threading.Thread(target=run,args=(alive,))th.start()app.exec_()alive.clear()

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

推荐PyPI第三方库


热门话题
java Spring启动启用HTTPS   actionscript 3 java中的这个[“var”+“name”]   java只匹配给定集合中一个字符的一个匹配项   java Hibernate:防止角色表中出现多个相同的条目   javajersey+Spring注入servlet请求   java HtmlEditor javafx失去焦点   java Apache Wicket AjaxRequestTarget ListView组件未刷新或更新   mysql java。无法将lang.String转换为java。sql。时间戳   java将巨大的整数文件(在一行中)拆分为具有内存限制的已排序块   安卓如何完全关闭proguard?   安装Eclipse和Android SDK后的java“无AVD可用”消息   java动态显示图像视图   java在Spring中还有哪些WebsocketClient实现?   java Glassfish需要很长时间才能重新启动   使用Java简单串行连接器将pc与arduino连接   java如何在camel文件组件配置中结合readLockCheckInterval和maxMessagesPerPoll?   单击Android时的java预览图像   java如何将字节数组转换为ByteArrayOutputStream