pyQT 线程与UI通信

from PyQt5 import QtWidgets,QtGui,QtCore
from PyQt5.Qt import QThread,pyqtSignal


import sys
import time
class Thd1(QThread):
    sig = pyqtSignal();
    def __init__(self):
        super().__init__();
        pass
    def run(self):
        a = [1, 2, 3, 4, 5];
        for i in a:
            print(i);
            time.sleep(1);
        self.sig.emit();

class Thd2(QThread):

    def __init__(self):
        super().__init__();
        pass
    def run(self):
        a = ['a','b','c','d','e'];
        for i in a:
            print(i);
            time.sleep(1);




class UI(QtWidgets.QWidget):

    def SetUi(self):
        self.setGeometry(300, 300, 300, 220)
        self.btn1 = QtWidgets.QPushButton("btn1",self);
        self.btn2 = QtWidgets.QPushButton("btn2", self);

        self.btn1.clicked.connect(self.click_btn1);
        self.btn2.clicked.connect(self.click_btn2);

        self.Vlayout = QtWidgets.QVBoxLayout();
        self.Hlayout = QtWidgets.QHBoxLayout();
        self.Hlayout.addStretch(1);
        self.Hlayout.addWidget(self.btn1)
        self.Hlayout.addWidget(self.btn2)

        self.Vlayout.addStretch(1);
        self.Vlayout.addLayout(self.Hlayout);
        self.setLayout(self.Vlayout);

        self.show();

    def click_btn1(self):
        self.btn1.setEnabled(False)
        self.th1 = Thd1();
        self.th1.sig.connect(self.enableBtn1)
        self.th1.start()

    def click_btn2(self):
        self.th2 = Thd2();
        self.th2.start()
    def enableBtn1(self):
        self.btn1.setEnabled(True);
if __name__ == '__main__':
    app = QtWidgets.QApplication([])

    ui = UI();
    ui.SetUi();
    sys.exit(app.exec_());
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值