PyQt5常用操作之使用多线程

参考代码

# -*- coding = utf-8 -*-
# @Time : 2022/9/6 16:35
# @Author : Arthur
# @FileName : 使用线程练习.py
# @Software : Pycharm
import sys
import time

from PyQt5 import uic
from PyQt5.QtCore import QThread
from PyQt5.QtWidgets import QApplication, QWidget


class my_thread(QThread):
    def __init__(self):
        super(my_thread, self).__init__()

    def run(self):
        for i in range(10):
            print('是my_thread线程正在执行...%d' % (i + 1))
            time.sleep(1)


class my_window(QWidget):
    def __init__(self):
        super(my_window, self).__init__()
        self.init_ui()

    def init_ui(self):
        self.ui = uic.loadUi('./ui/thread_sys.ui')
        btn1 = self.ui.pushButton
        btn2 = self.ui.pushButton_2
        edit = self.ui.lineEdit

        # 绑定按钮和槽函数
        btn1.clicked.connect(self.click_1)
        btn2.clicked.connect(self.click_2)

    def click_1(self):
        for i in range(10):
            print('是UI线程正在执行...%d' % (i + 1))
            time.sleep(1)

    def click_2(self):
        self.t = my_thread()  # 创建线程
        self.t.start()  # 开始线程


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = my_window()
    w.ui.show()
    app.exec_()
Like (0)
Donate 微信扫一扫 微信扫一扫
朋远方的头像朋远方
Previous 2022年9月6日 下午4:19
Next 2022年9月7日 上午8:50

相关推荐

发表回复

Please Login to Comment