簡體   English   中英

PySide QThread從不同的類/ QThread控制QWebView

[英]PySide QThread controlling QWebView from different class / QThread

我看了遍整個互聯網。 我試圖在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()

不斷出錯:

追溯(最近一次通話):文件“ C:\\ software \\ stackoverflow-sample.py”,行33,在運行web_window.load(QUrl(“ http://www.yahoo.com/ ”))中)NameError:全局未定義名稱“ web_window”

全部解決。 這是帶有不同代碼的-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_())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM