使用 python 和 pyqt gui 从另一个 class 读取字符串或将字符串发送到 qtextbrowser 的正确方法是什么?
What is the correct way to read strings or send strings to a qtextbrowser from another class using python and pyqt gui?
我正在尝试将字符串从一个 class 发送到位于另一个 class 的 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
从另一个 class 读取字符串或将字符串发送到 qtextbrowser 的正确方法是什么?
我需要这个,因为我的应用程序需要读取一些 cpu 和不同线程上的 ram 值,以防止我的应用程序冻结并在工作完成时显示一条文本消息。
发生此错误是因为您的线程,而不仅仅是因为您有多个 类。无论何时线程化,您都应该始终使用信号和槽来传达您想要触发的 GUI 线程内的更改。查看此 link 以获得全面的示例:
http://pythoncentral.io/pysidepyqt-tutorial-creating-your-own-signals-and-slots/
要在 window 中显示来自 QThread 的数据,无需创建实例,因为您正在创建的实例将与原始实例不同 window。
要从线程向 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
class只会显示一次消息,如果你想让消息不时显示你必须修改运行方法:
def run(self):
while True:
self.cpuRunValue()
time.sleep(1)
我正在尝试将字符串从一个 class 发送到位于另一个 class 的 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
从另一个 class 读取字符串或将字符串发送到 qtextbrowser 的正确方法是什么?
我需要这个,因为我的应用程序需要读取一些 cpu 和不同线程上的 ram 值,以防止我的应用程序冻结并在工作完成时显示一条文本消息。
发生此错误是因为您的线程,而不仅仅是因为您有多个 类。无论何时线程化,您都应该始终使用信号和槽来传达您想要触发的 GUI 线程内的更改。查看此 link 以获得全面的示例: http://pythoncentral.io/pysidepyqt-tutorial-creating-your-own-signals-and-slots/
要在 window 中显示来自 QThread 的数据,无需创建实例,因为您正在创建的实例将与原始实例不同 window。
要从线程向 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
class只会显示一次消息,如果你想让消息不时显示你必须修改运行方法:
def run(self):
while True:
self.cpuRunValue()
time.sleep(1)