欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PYQT5開(kāi)啟多個(gè)線(xiàn)程和窗口,多線(xiàn)程與多窗口的交互實(shí)例

 更新時(shí)間:2019年12月13日 15:20:19   作者:liqkjm  
今天小編就為大家分享一篇PYQT5開(kāi)啟多個(gè)線(xiàn)程和窗口,多線(xiàn)程與多窗口的交互實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

每點(diǎn)擊一次按鈕,彈出一個(gè)對(duì)話(huà)框(子窗口),同時(shí)開(kāi)啟一個(gè)子線(xiàn)程來(lái)執(zhí)行任務(wù)并更新對(duì)話(huà)框內(nèi)容,關(guān)閉對(duì)話(huà)框則關(guān)閉對(duì)應(yīng)子線(xiàn)程

1. 建立一個(gè)簡(jiǎn)單的主界面和一個(gè)自定義對(duì)話(huà)框

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
 def setupUi(self, MainWindow):
  MainWindow.setObjectName("MainWindow")
  MainWindow.resize(327, 303)
  self.centralwidget = QtWidgets.QWidget(MainWindow)
  self.centralwidget.setObjectName("centralwidget")
  self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
  self.gridLayout.setObjectName("gridLayout")
  spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
  self.gridLayout.addItem(spacerItem, 0, 0, 1, 1)
  self.pushButton = QtWidgets.QPushButton(self.centralwidget)
  self.pushButton.setObjectName("pushButton")
  self.gridLayout.addWidget(self.pushButton, 0, 1, 1, 1)
  spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
  self.gridLayout.addItem(spacerItem1, 0, 2, 1, 1)
  MainWindow.setCentralWidget(self.centralwidget)
  self.menubar = QtWidgets.QMenuBar(MainWindow)
  self.menubar.setGeometry(QtCore.QRect(0, 0, 327, 23))
  self.menubar.setObjectName("menubar")
  MainWindow.setMenuBar(self.menubar)
  self.statusbar = QtWidgets.QStatusBar(MainWindow)
  self.statusbar.setObjectName("statusbar")
  MainWindow.setStatusBar(self.statusbar)

  self.retranslateUi(MainWindow)
  self.pushButton.clicked.connect(MainWindow.open_dialog)
  QtCore.QMetaObject.connectSlotsByName(MainWindow)

 def retranslateUi(self, MainWindow):
  _translate = QtCore.QCoreApplication.translate
  MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
  self.pushButton.setText(_translate("MainWindow", "多線(xiàn)程彈窗"))


class Ui_Dialog(object):
 def setupUi(self, Dialog):
  Dialog.setObjectName("Dialog")
  Dialog.resize(369, 128)
  self.gridLayout = QtWidgets.QGridLayout(Dialog)
  self.gridLayout.setObjectName("gridLayout")
  self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
  self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
  self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
  self.buttonBox.setObjectName("buttonBox")
  self.gridLayout.addWidget(self.buttonBox, 1, 0, 1, 1)
  self.progressBar = QtWidgets.QProgressBar(Dialog)
  self.progressBar.setProperty("value", 24)
  self.progressBar.setObjectName("progressBar")
  self.gridLayout.addWidget(self.progressBar, 0, 0, 1, 1)

  self.retranslateUi(Dialog)
  self.buttonBox.accepted.connect(Dialog.accept)
  self.buttonBox.rejected.connect(Dialog.reject)
  QtCore.QMetaObject.connectSlotsByName(Dialog)

 def retranslateUi(self, Dialog):
  _translate = QtCore.QCoreApplication.translate
  Dialog.setWindowTitle(_translate("Dialog", "Dialog"))

2. 每點(diǎn)擊一次按鈕,打開(kāi)一個(gè)彈窗

class DialogWindow(QDialog, Ui_Dialog):
 def __init__(self, parent=None):
  super(DialogWindow, self).__init__(parent)
  self.setupUi(self)


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
 def __init__(self, parent=None):
  super(MainWindow, self).__init__(parent)
  self.setupUi(self)

 def open_dialog(self):
  dialog = DialogWindow(self)
  dialog.show()


if __name__ == "__main__":
 app = QtWidgets.QApplication(sys.argv)
 mainWindow = MainWindow()
 mainWindow.show()
 sys.exit(app.exec_())

3. 打開(kāi)彈窗的同時(shí),打開(kāi)一個(gè)子線(xiàn)程,更新對(duì)話(huà)框中的進(jìn)度條

在子線(xiàn)程定義信號(hào),關(guān)聯(lián)對(duì)話(huà)框更新進(jìn)度條的槽函數(shù)

class DialogWindow(QDialog, Ui_Dialog):
 def __init__(self, parent=None):
  super(DialogWindow, self).__init__(parent)
  self.setupUi(self)

 def update_progressbar(self, p_int):
  self.progressBar.setValue(p_int) # 更新進(jìn)度條


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
 def __init__(self, parent=None):
  super(MainWindow, self).__init__(parent)
  self.setupUi(self)
  self.count = 0

 def open_dialog(self):
  dialog = DialogWindow(self)
  dialog.show()
  self.thread = RunThread(self.count)
  self.count += 1
  self.thread.update_pb.connect(dialog.update_progressbar) # 關(guān)聯(lián)
  self.thread.start()


class RunThread(QThread):
 update_pb = pyqtSignal(int) # 定義更新進(jìn)度條的信號(hào)

 def __init__(self, count):
  super().__init__()
  self.count = count

 def run(self):
  for i in range(100):
   print('thread%s' % self.count, i, QThread().currentThreadId())
   self.update_pb.emit(i)
   time.sleep(1)
  pass


if __name__ == "__main__":
 app = QtWidgets.QApplication(sys.argv)
 mainWindow = MainWindow()
 mainWindow.show()
 sys.exit(app.exec_())

4. 關(guān)閉對(duì)話(huà)框,則關(guān)閉對(duì)應(yīng)子線(xiàn)程

在對(duì)話(huà)框中添加自定義信號(hào),并重寫(xiě)關(guān)閉事件,在關(guān)閉窗口時(shí)發(fā)送關(guān)閉子線(xiàn)程的信號(hào)

class DialogWindow(QDialog, Ui_Dialog):
 stop_thread = pyqtSignal() # 定義關(guān)閉子線(xiàn)程的信號(hào)

 def __init__(self, parent=None):
  super(DialogWindow, self).__init__(parent)
  self.setupUi(self)

 def update_progressbar(self, p_int):
  self.progressBar.setValue(p_int)

 def closeEvent(self, event):
  self.stop_thread.emit()
  pass


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
 def __init__(self, parent=None):
  super(MainWindow, self).__init__(parent)
  self.setupUi(self)
  self.count = 0

 def open_dialog(self):
  dialog = DialogWindow(self)
  dialog.show()
  self.thread = RunThread(self.count)
  self.count += 1
  self.thread.update_pb.connect(dialog.update_progressbar)
  dialog.stop_thread.connect(self.thread.terminate)
  self.thread.start()


class RunThread(QThread):
 update_pb = pyqtSignal(int)

 def __init__(self, count):
  super().__init__()
  self.count = count

 def run(self):
  for i in range(1, 101):
   print('thread_%s' % self.count, i, QThread().currentThreadId())
   self.update_pb.emit(i)
   time.sleep(1)
  pass


if __name__ == "__main__":
 app = QtWidgets.QApplication(sys.argv)
 mainWindow = MainWindow()
 mainWindow.show()
 sys.exit(app.exec_())

5. 使用線(xiàn)程池QThreadPool管理子線(xiàn)程

使用QThreadPool, 線(xiàn)程需要繼承QRunnable,而QRunnable只是namespace,沒(méi)有繼承QT的信號(hào)機(jī)制,

所以需要另外繼承QObject來(lái)使用信號(hào),我這里直接在線(xiàn)程中使用封裝的信號(hào)向外部傳遞信息

class DialogWindow(QDialog, Ui_Dialog):
 stop_thread = pyqtSignal() # 定義關(guān)閉子線(xiàn)程的信號(hào)

 def __init__(self, parent=None):
  super(DialogWindow, self).__init__(parent)
  self.setupUi(self)

 def update_progressbar(self, p_int):
  self.progressBar.setValue(p_int)

 def closeEvent(self, event):
  self.stop_thread.emit()
  pass


class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
 def __init__(self, parent=None):
  super(MainWindow, self).__init__(parent)
  self.setupUi(self)
  self.count = 0
  self.pool = QThreadPool()
  self.pool.globalInstance()
  self.pool.setMaxThreadCount(10) # 設(shè)置最大線(xiàn)程數(shù)

 def open_dialog(self):
  dialog = DialogWindow(self)
  dialog.show()
  thread = RunThread(self.count)
  self.count += 1
  thread.signal.update_pb.connect(dialog.update_progressbar)
  # dialog.stop_thread.connect(thread.stop)
  # self.thread.start()
  self.pool.start(thread) # 線(xiàn)程池分配一個(gè)線(xiàn)程運(yùn)行該任務(wù)


class Signal(QObject):
 update_pb = pyqtSignal(int)


class RunThread(QRunnable):
 def __init__(self, count):
  super().__init__()
  self.count = count
  self.signal = Signal() # 信號(hào)

 def run(self):
  for i in range(1, 101):
   print('thread_%s' % self.count, i, QThread().currentThreadId())
   self.signal.update_pb.emit(i)
   time.sleep(1)


if __name__ == "__main__":
 app = QtWidgets.QApplication(sys.argv)
 mainWindow = MainWindow()
 mainWindow.show()
 sys.exit(app.exec_())

QThreadPool沒(méi)有釋放正在運(yùn)行的線(xiàn)程的方法

以上這篇PYQT5開(kāi)啟多個(gè)線(xiàn)程和窗口,多線(xiàn)程與多窗口的交互實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python模塊對(duì)Redis數(shù)據(jù)庫(kù)的連接與使用講解

    Python模塊對(duì)Redis數(shù)據(jù)庫(kù)的連接與使用講解

    這篇文章主要介紹了Python模塊對(duì)Redis數(shù)據(jù)庫(kù)的連接與使用,通過(guò)實(shí)例代碼給大家介紹了Python連接Redis數(shù)據(jù)庫(kù)方法,Python使用連接池連接Redis數(shù)據(jù)庫(kù)方法,感興趣的朋友跟隨小編一起看看吧
    2021-07-07
  • Python selenium爬取微信公眾號(hào)文章代碼詳解

    Python selenium爬取微信公眾號(hào)文章代碼詳解

    這篇文章主要介紹了Python selenium爬取微信公眾號(hào)歷史文章代碼詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Facebook開(kāi)源一站式服務(wù)python時(shí)序利器Kats詳解

    Facebook開(kāi)源一站式服務(wù)python時(shí)序利器Kats詳解

    這篇文章主要為答案及介紹了Facebook開(kāi)源一站式服務(wù)python時(shí)序利器Kats的功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-11-11
  • python+mongodb數(shù)據(jù)抓取詳細(xì)介紹

    python+mongodb數(shù)據(jù)抓取詳細(xì)介紹

    這篇文章主要介紹了python+mongodb數(shù)據(jù)抓取詳細(xì)介紹,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-10-10
  • Python 可愛(ài)的大小寫(xiě)

    Python 可愛(ài)的大小寫(xiě)

    和其他語(yǔ)言一樣,Python為string對(duì)象提供了轉(zhuǎn)換大小寫(xiě)的方法:upper() 和 lower()。還不止這些,Python還為我們提供了首字母大寫(xiě),其余小寫(xiě)的capitalize()方法,以及所有單詞首字母大寫(xiě),其余小寫(xiě)的title()方法。
    2008-09-09
  • python單向循環(huán)鏈表原理與實(shí)現(xiàn)方法示例

    python單向循環(huán)鏈表原理與實(shí)現(xiàn)方法示例

    這篇文章主要介紹了python單向循環(huán)鏈表原理與實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Python單向循環(huán)鏈表概念、原理、定義及使用方法,需要的朋友可以參考下
    2019-12-12
  • python 3.7.0 下pillow安裝方法

    python 3.7.0 下pillow安裝方法

    這篇文章主要為大家詳細(xì)介紹了python 3.7.0 下pillow的安裝方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • python用match()函數(shù)爬數(shù)據(jù)方法詳解

    python用match()函數(shù)爬數(shù)據(jù)方法詳解

    在本篇文章里小編給大家整理了關(guān)于python用match()函數(shù)爬數(shù)據(jù)方法以及相關(guān)知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)下。
    2019-07-07
  • Python中的字符串判斷

    Python中的字符串判斷

    本文通過(guò)實(shí)例代碼介紹了Python中的字符串判斷,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-10-10
  • 解決pytorch讀取自制數(shù)據(jù)集出現(xiàn)過(guò)的問(wèn)題

    解決pytorch讀取自制數(shù)據(jù)集出現(xiàn)過(guò)的問(wèn)題

    這篇文章主要介紹了解決pytorch讀取自制數(shù)據(jù)集出現(xiàn)過(guò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05

最新評(píng)論