【问题标题】:What is the correct way to read strings or send strings to a qtextbrowser from another class using python and pyqt gui?使用python和pyqt gui从另一个类读取字符串或将字符串发送到qtextbrowser的正确方法是什么?
【发布时间】:2017-07-19 00:37:58
【问题描述】:

我正在尝试将一个类中的字符串发送到另一个类中的 qtextbrowser,我的 gui 是在 pyqt 中构建的,而我在 python 2.7 中编写的代码。

这是我的测试代码:

class print_text():
    def __init__(self):
        text1 = "Acesta este un text de proba"
        self.classMyWindow = MyWindow()
        self.classMyWindow.statusText_updater("Merge ok ")

class systemValues(QThread):
    def __init__(self):
        QThread.__init__(self)

    def __del__(self):
        self.wait()

    def cpuRunValue(self):
        text1 = "Acesta este un text de proba"
        self.classMyWindow = MyWindow()
        self.classMyWindow.statusText_updater("Merge ok ")

    def run(self):
        self.cpuRunValue()

class MyWindow(QtGui.QMainWindow):
    def __init__(self):
        #QtGui.QDialog.__init__(self)
        super(MyWindow, self).__init__()
        file_path = os.path.abspath("im-manager.ui")
        uic.loadUi(file_path, self)
        self.myThread = systemValues()
        self.myThread.start()

   def statusText_updater(self,text):
       time_print = time.strftime("%d/%m/%Y-%H:%M:%S")
       read1 = self.status.toPlainText()
       self.status.setText(text+" >>> "+time_print+" \n"+ read1+" ") 

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    # app.exec_()
    sys.exit(app.exec_())

我收到此错误:

QPixmap: It is not safe to use pixmaps outside the GUI thread

从另一个类读取字符串或将字符串发送到 qtextbrowser 的正确方法是什么?

我需要这个,因为我的应用需要在不同的线程上读取一些 cpu 和 ram 值,以防止我的应用冻结并在工作完成时显示一条短信。

UI File

【问题讨论】:

  • 你在哪里定义了 statusText_updater?
  • 对不起,我的错误。忘记了这个定义。我更新了代码@eyllanesc
  • MyWindow 是 QMainWindow 还是 QDialog?,您使用的 im-manager.ui 模板是什么?
  • QMainWindow,可以看这里class MyWindow(QtGui.QMainWindow)
  • QtGui.QDialog.__init__(self) super(MyWindow, self).__init__()?

标签: python python-2.7 pyqt pyqt4


【解决方案1】:

要在窗口中显示来自 QThread 的数据,不必创建实例,因为您创建的实例与原始窗口不同。

要将线程中的数据显示到 GUI,您必须通过信号来完成,并将其连接到每次发出信号时更新数据的插槽。

在这种情况下,我们将创建 newMessage 信号并发出它:

newMessage  = QtCore.pyqtSignal(str)
#emit signal
self.newMessage.emit("Merge ok ")

然后我们将它连接到 statusText_updater 插槽

self.myThread.newMessage.connect(self.statusText_updater)

完整代码:

class systemValues(QtCore.QThread):
    newMessage  = QtCore.pyqtSignal(str)
    def __init__(self, parent=None):
        QtCore.QThread.__init__(self, parent)

    def __del__(self):
        self.wait()

    def cpuRunValue(self):
        text1 = "Acesta este un text de proba"
        self.newMessage.emit("Merge ok ")


    def run(self):
        self.cpuRunValue()

class MyWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent=parent)
        file_path = os.path.abspath("im-manager.ui")
        uic.loadUi(file_path, self)
        self.myThread = systemValues()
        self.myThread.newMessage.connect(self.statusText_updater)
        self.myThread.start()

    def statusText_updater(self,text):
        time = datetime.datetime.now()
        time_print = time.strftime("%d/%m/%Y-%H:%M:%S")
        read1 = self.status.toPlainText()
        self.status.setText(text+" >>> "+time_print+" \n"+ read1+" ")

因为现在你的QThread类只会显示一次消息,如果你想让消息不时显示你必须修改run方法:

def run(self):
    while True:
        self.cpuRunValue()
        time.sleep(1)

【讨论】:

  • 当你想从class MyWindow读取一个值时是一样的还是过程不同?我想成为“双向”。 ??
  • 是的,您必须在class Window 中创建一个信号并将其连接到一个更新class systemValues 属性的插槽。
  • 在 pyqt 中,发送数据的自然方式是通过信号和槽。
【解决方案2】:

发生此错误是因为您的线程,而不仅仅是因为您有多个类。每当线程化时,您应该始终使用信号和插槽来传达您想要触发的 GUI 线程内的更改。查看此链接以获取全面的示例: http://pythoncentral.io/pysidepyqt-tutorial-creating-your-own-signals-and-slots/

【讨论】:

    猜你喜欢
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多