PyQt5 使用

`
from PyQt5 import QtCore
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import QWidget, QRadioButton, QToolButton, QLabel
from core.engine import EnvironmentClear

"""
运行程序会开启两个窗口,窗口一和窗口二
窗口一的按钮点击时触发的槽函数有:
打开窗口二
运行主程序(环境清洁性检查)
隐藏窗口一

运行主程序的时候会通过信号触发槽函数来修改窗口二中选项的按钮,并在结束时展示耗时
运行主程序时的耗时操作需要QThread线程中运行,否则窗口会出现未响应的情况
"""

class FirstThread(QThread):

def __init__(self, parent=None, run_singal=None, sendto_second_signal=None, worker=None):
    super(FirstThread, self).__init__(parent)
    self.identity = None
    self.run_singal = run_singal
    self.sendto_second_signal = sendto_second_signal
    self.worker = worker
    self.worker.singal = run_singal

def run(self):
    self.worker.run()

class FirstWindow(QWidget):
close_signal = pyqtSignal()
run_signal = pyqtSignal(str)
sendto_second_signal = pyqtSignal(object)

def __init__(self):
    super(FirstWindow, self).__init__()

    self.run_thread = FirstThread(run_singal=self.run_signal,
                                  sendto_second_signal=self.sendto_second_signal,
                                  worker=EnvironmentClear(self.run_signal, self.sendto_second_signal))
    self.resize(800, 600)
    self.btn = QToolButton(self)
    self.btn.setText("click")
    self.btn.move(300, 200)
    self.btn.resize(100, 50)
    self.initUI()


def initUI(self):
    self.run_signal.connect(self.singal_evevt)
    self.sendto_second_signal.connect(self.sendto_second)

    self.second = SecondWindow.instance()
    self.btn.clicked.connect(self.second.handle_click)  # 展示第二个窗口
    self.btn.clicked.connect(self.hide)  # 隐藏第一个窗口
    self.btn.clicked.connect(self.work)
    self.close_signal.connect(self.close)

def closeEvent(self, event):
    self.close_signal.emit()
    self.close()

def work(self):
    self.run_thread.start()

def singal_evevt(self, str):
    print(f"窗口一接受到信号: {str}")

def sendto_second(self, str):
    try:
        self.second.second_signal.emit(str)
    except Exception as e:
        print(e)

class SecondWindow(QWidget):
second_signal = pyqtSignal(object)

@classmethod
def instance(cls, *args, **kwargs):
    if not hasattr(SecondWindow, "_instance"):
        SecondWindow._instance = SecondWindow(*args, **kwargs)
    return SecondWindow._instance

def __init__(self, parent=None):
    super(SecondWindow, self).__init__(parent)
    self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)

    self.resize(400, 400)
    self.move(1500, 600)
    self.setUpUi()
    self.second_signal.connect(self.select_button)
    self.btn.clicked.connect(self.handle_close)

def setUpUi(self):
    self.rb1 = QRadioButton(self, text="第一步")
    self.rb2 = QRadioButton(self, text="第二步")
    self.rb3 = QRadioButton(self, text="第三步")
    self.rb4 = QRadioButton(self, text="第四步")

    self.rb1.move(30, 40)
    self.rb2.move(30, 70)
    self.rb3.move(30, 100)
    self.rb4.move(30, 130)

    self.rb1.setAutoExclusive(False)
    self.rb2.setAutoExclusive(False)
    self.rb3.setAutoExclusive(False)
    self.rb4.setAutoExclusive(False)

    self.rb1.setChecked(False)
    self.rb2.setChecked(False)
    self.rb3.setChecked(False)
    self.rb4.setChecked(False)

    self.lable = QLabel(self)
    self.lable.resize(150, 50)  # 第一个参数为宽度,第二个为高度
    self.lable.setText("进行中")
    self.lable.move(30, 150)

    self.btn = QToolButton(self)
    self.btn.setText("点击关闭窗口")
    self.btn.move(30, 190)
    self.btn.resize(100, 50)
    self.btn.setVisible(False)

def handle_click(self):
    if not self.isVisible():
        self.show()

def handle_close(self):
    self.close()

def select_button(self, _str):
    if isinstance(_str, str):
        print(f"窗口二接受到第{_str}项任务已完成信号。")

        if _str == "1":
            self.rb1.setChecked(True)
            self.rb1.repaint()
        elif _str == "2":
            self.rb2.setChecked(True)
            self.rb2.repaint()
        elif _str == "3":
            self.rb3.setChecked(True)
            self.rb3.repaint()
        else:
            self.rb4.setChecked(True)
            self.rb4.repaint()

    else:
        print(f"窗口二接受到所有任务已完成信号。")
        self.lable.setText(f"已完成,耗时: {_str}s")
        self.lable.repaint()
        self.move(500, 500)
        self.btn.setVisible(True)

`

上一篇:ECMAScript6之Set结构和Map结构


下一篇:Binder学习笔记(六)—— binder服务端是如何组织addService数据的