【问题标题】:PySide QThread controlling QWebView from different class / QThreadPySide QThread 从不同的类/ QThread 控制 QWebView
【发布时间】:2018-03-09 23:42:02
【问题描述】:

我在互联网上看了又看。 我正在尝试在 PySide Python 中使用 QThread,通过单独的线程更改网站/控制 JS。在下面的代码中,我尝试在 5 秒后更改 QUrl,重新加载页面。 我需要使用 QObjectName 吗?如何定位在 MainWindow 中创建的 QWebView。 我做错了什么。

import time
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import *

class MainWindow(QMainWindow):

      def __init__(self, parent=None):
          QMainWindow.__init__(self,parent)

          web_window = QWebView()
          #default website at startup
          web_window.load(QUrl("http://www.google.com"))
          web_window.show()

          thread = Thread()
          thread.start()

          sys.exit(app.exec_())


class Thread(QThread):

      def __init__(self):
          QThread.__init__(self)
          print("Thread initialized")

      def run(self):
          print("Thread is running....")

          time.sleep(2) #in 2 seconds open different page
          web_window.load(QUrl("http://www.yahoo.com/"))

          #time.sleep(5) #in 5 seconds open different page
          web_window.load(QUrl("http://www.stackoverflow.com/"))

          #do more thread code here...


if __name__ == '__main__':
    app = QApplication(sys.argv)
    web = MainWindow()

不断报错:

Traceback(最近一次调用最后一次):文件 “C:\software\stackoverflow-sample.py”,第 33 行,运行中 web_window.load(QUrl("http://www.yahoo.com/")) NameError: 全局名称 'web_window' 未定义

【问题讨论】:

  • Qt 不支持主线程之外的任何 gui 操作。所以直接从非gui线程调用widget的方法是不安全的。如果您想定期重新加载或更改页面,请使用QTimer
  • 谢谢! :) 我用 SIGNAL / CONNECT 解决了这个问题。

标签: python python-2.7 pyqt pyside qthread


【解决方案1】:

全部解决。 这是不同的代码 - QThread 连接到主 GUI(重新加载网站或任何东西)。以下页面非常有帮助:https://nikolak.com/pyqt-threading-tutorial/

import sys, time
from PySide.QtGui import *
from PySide.QtCore import *

class ThreadSignal(QObject):
        sig = Signal(str)

class ThreadMain(QThread):
    def __init__(self, parent = None):
        QThread.__init__(self, parent)
        self.send2thread_signal = ThreadSignal()

    def run(self):
        cc=1
        while (cc > 0):
            cc = cc + 1
            self.send2thread_signal.sig.emit( 'Message #: %s' % cc )

            time.sleep(0.5)

            if (cc == 10):
                print("Time to kill thread")
                cc=-1


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self,parent)
        print("GUI: Startup & Init")

        self.thread = ThreadMain()
        self.thread.start()
        self.thread.started.connect(self.gui_hello)
        self.thread.finished.connect(self.finished)
        self.thread.send2thread_signal.sig.connect(self.threadcommunication) #thread communication


    def gui_hello(self):
        print("GUI: Thread started." )

    def finished(self):
        print("GUI: Thread finished.")

    def threadcommunication(self,message_from_thread):
        print("GUI: Message from QThread: %s" % message_from_thread )


if __name__=='__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

【讨论】:

    猜你喜欢
    • 2014-03-25
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    • 2015-06-20
    相关资源
    最近更新 更多