国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5多線程中信號(hào)與槽的詳細(xì)使用方法與實(shí)例

瀏覽:4日期:2022-08-03 13:03:01
PyQt5簡(jiǎn)單多線程信號(hào)與槽的使用

最簡(jiǎn)單的多線程使用方法是利用QThread函數(shù),展示QThread函數(shù)和信號(hào)簡(jiǎn)單結(jié)合的方法

import sysfrom PyQt5.QtCore import *from PyQt5.QtWidgets import *class Main(QWidget): def __init__( self, parent=None ): super(Main, self).__init__(parent) #創(chuàng)建一個(gè)線程實(shí)例并設(shè)置名稱(chēng) 變量 信號(hào)與槽 self.thread = MyThread() self.thread.setIdentity(’thread1’) self.thread.sinOut.connect(self.outText) self.thread.setVal(6) #打印輸出文本 def outText( self, text ): print(text)class MyThread(QThread): #自定義信號(hào)參數(shù)為str類(lèi)型 sinOut = pyqtSignal(str) def __init__( self, parent=None ): super(MyThread, self).__init__(parent) #初始化名稱(chēng)為空 self.identity = None def setIdentity( self, text ): #設(shè)置多線程名稱(chēng) self.identity=text def setVal( self, val ): #接受數(shù)據(jù),運(yùn)行多線程 self.times = int(val) self.run() def run( self ): #當(dāng)次數(shù)大于0以及名稱(chēng)不為空時(shí)執(zhí)行代碼 while self.times>0 and self.identity: #發(fā)射信號(hào),觸發(fā)打印函數(shù),次數(shù)-1 self.sinOut.emit(self.identity+’==>’+str(self.times)) self.times-=1if __name__ == ’__main__’: app=QApplication(sys.argv) main=Main() main.show() sys.exit(app.exec_())

運(yùn)行如下

python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5多線程中信號(hào)與槽的詳細(xì)使用方法與實(shí)例

主線程與子線程的使用

有時(shí)候在開(kāi)發(fā)程序時(shí)會(huì)經(jīng)常執(zhí)行一些耗時(shí)的操作,這樣就會(huì)導(dǎo)致界面卡頓,這也是多線程的應(yīng)用范圍之一,這樣我們就可以創(chuàng)建多線程,使用主線程更新界面,使用子線程后臺(tái)處理數(shù)據(jù),最后將結(jié)果顯示在界面上

import sys,timefrom PyQt5.QtCore import *from PyQt5.QtWidgets import *class BackQthread(QThread): #自定義信號(hào)為str參數(shù)類(lèi)型 update_date=pyqtSignal(str) def run( self ): while True: #獲得當(dāng)前系統(tǒng)時(shí)間 data=QDateTime.currentDateTime() #設(shè)置時(shí)間顯示格式 curTime=data.toString(’yyyy-MM-dd hh:mm:ss dddd’) #發(fā)射信號(hào) self.update_date.emit(str(curTime)) #睡眠一秒 time.sleep(1)class window(QDialog): def __init__(self): super(window, self).__init__() #設(shè)置標(biāo)題與初始大小 self.setWindowTitle(’PyQt5界面實(shí)時(shí)更新的例子’) self.resize(400,100) #實(shí)例化文本輸入框及其初始大小 self.input=QLineEdit(self) self.input.resize(400,100) self.initUI() def initUI( self ): #實(shí)例化對(duì)象 self.backend=BackQthread() #信號(hào)連接到界面顯示槽函數(shù) self.backend.update_date.connect(self.handleDisplay) #多線程開(kāi)始 self.backend.start() def handleDisplay( self,data ): #設(shè)置單行文本框的文本 self.input.setText(data)if __name__ == ’__main__’: app=QApplication(sys.argv) win=window() win.show() sys.exit(app.exec_())

運(yùn)行程序,效果如下

python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5多線程中信號(hào)與槽的詳細(xì)使用方法與實(shí)例

本文主要講解了PyQt5多線程中信號(hào)與槽的詳細(xì)使用方法與實(shí)例,更多關(guān)于PyQt5信號(hào)與槽的知識(shí)請(qǐng)查看下面的相關(guān)鏈接

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 九江市| 蓝田县| 疏附县| 英德市| 佳木斯市| 建瓯市| 湘西| 临夏市| 临沭县| 云龙县| 乐平市| 陆河县| 汪清县| 台江县| 库伦旗| 龙泉市| 湟中县| 四平市| 独山县| 屏东市| 邯郸市| 个旧市| 白朗县| 延津县| 京山县| 常山县| 鄂托克旗| 讷河市| 磐石市| 黄大仙区| 新乡市| 高碑店市| 太康县| 昭苏县| 文水县| 万山特区| 宜良县| 彭山县| 巩留县| 东源县| 兴义市|