如何在函数内部“打印”pyqt5?

我正在创建一个程序(用于网页抓取),我想创建一个地方来显示脚本正在做什么(因为它已经可以通过控制台上的打印来实现),但在 PyQt5 textEdit 中。(该脚本位于 python 文件的函数内部)


然而,当函数/脚本运行时,GUI 崩溃并且直到函数/脚本结束才显示任何内容。


我做了一个简化版本来帮助理解问题并使其更容易解决。如果它适用于这个,那么它可能也适用于另一个。


Python文件

print('iniciando programa...')

from PyQt5 import uic, QtWidgets

import time


def F_exemplof(): #function thats print index in 2 seconds

    for a in range(20):

        print('Print index: ' + str(a))

        W_exemplogui.textEdit.append('Print index: ' + str(a))

        time.sleep(2)



# Importing the GUI

app = QtWidgets.QApplication([])

W_exemplogui = uic.loadUi('testegui.ui')


# Buttons

W_exemplogui.pushButton.clicked.connect(F_exemplof)

#W_exemplogui.pushButton_2.clicked.connect()



# Show window

W_exemplogui.show()

app.exec()

测试GUI.ui

<?xml version="1.0" encoding="UTF-8"?>

<ui version="4.0">

 <class>MainWindow</class>

 <widget class="QMainWindow" name="MainWindow">

  <property name="geometry">

   <rect>

    <x>0</x>

    <y>0</y>

    <width>383</width>

    <height>304</height>

   </rect>

  </property>

  <property name="windowTitle">

   <string>MainWindow</string>

  </property>

  <widget class="QWidget" name="centralwidget">

   <layout class="QGridLayout" name="gridLayout" rowstretch="0">

    <item row="0" column="0">

     <widget class="QFrame" name="frame">

      <property name="styleSheet">

       <string notr="true">background-color: rgb(90, 90, 90);</string>

      </property>

      <property name="frameShape">

       <enum>QFrame::StyledPanel</enum>

      </property>

      <property name="frameShadow">

       <enum>QFrame::Raised</enum>

      </property>

      <layout class="QGridLayout" name="gridLayout_2">

       <item row="1" column="1">

        <widget class="QPushButton" name="pushButton">

         <property name="styleSheet">

          <string notr="true">color: rgb(255, 255, 255);</string>

         </property>

         <property name="text">

          <string>Iniciar</string>

         </property>


牧羊人nacy
浏览 3898回答 1
1回答

狐的传说

耗时的任务必须在辅助线程中执行,以避免阻塞 GUI,并且必须使用信号将信息发送到主线程。import threadingimport timefrom PyQt5 import uic, QtCore, QtWidgetsclass Task(QtCore.QObject):&nbsp; &nbsp; messageChanged = QtCore.pyqtSignal(str)&nbsp; &nbsp; def start(self):&nbsp; &nbsp; &nbsp; &nbsp; threading.Thread(target=self._execute, daemon=True).start()&nbsp; &nbsp; def _execute(self):&nbsp; &nbsp; &nbsp; &nbsp; for a in range(20):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Print index: " + str(a))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.messageChanged.emit("Print index: " + str(a))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.sleep(2)def main():&nbsp; &nbsp; app = QtWidgets.QApplication([])&nbsp; &nbsp; W_exemplogui = uic.loadUi("testegui.ui")&nbsp; &nbsp; task = Task()&nbsp; &nbsp; W_exemplogui.pushButton.clicked.connect(task.start)&nbsp; &nbsp; task.messageChanged.connect(W_exemplogui.textEdit.append)&nbsp; &nbsp; W_exemplogui.show()&nbsp; &nbsp; app.exec_()if __name__ == "__main__":&nbsp; &nbsp; main()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python