我用PYQT5做的第一个实用的上位机项目(四)

继续建设自定义的部件,这次是项目选择器(类似于QRadioButton)。

我用PYQT5做的第一个实用的上位机项目(四)_第1张图片

MySelectorBox.py:

# 自定义的多选框
from PyQt5.QtWidgets import QGroupBox
from PyQt5.Qt import pyqtSignal


class MySelectorBox(QGroupBox):

    def __init__(self, parent=None):
        super().__init__(parent)
        self.controls_clicked_style = None
        self.controls_enter_style = None
        self.start_on = 0
        self.controls_normal_style = None
        self.states_off_style = None
        self.states_on_style = None
        self.controls = []
        self.states = []

    def init(self):  # 一个典型的初始化范例,需要在部件初始化的时候运行一次

        # self.controls =[form0.selector1.control0, form0.selector1.control1]    # 这两行代码要更换成应用中实际的项目
        # self.states = [form0.selector1.state0, form0.selector1_state1]    # 这两行代码要更换成应用中实际的项目
        self.controls_normal_style = 'color: #e6e6e6;background-color: rgba(255, 255, 255, 0);border-radius:2px;border:0px solid #1e2557;'  # 默认控制器常态style
        self.controls_enter_style = 'color: #e6e6e6;background-color: rgba(255, 255, 255, 0);border-radius:2px;border:1px solid #e6e6e6;'  # 默认控制器鼠标悬停style
        self.controls_clicked_style = 'color: #e6e6e6;background-color: rgba(255, 255, 255, 0);border-radius:2px;border:1px solid #039806;'  # 默认控制器鼠标点击style

        self.states_on_style = 'border:2px solid  rgb(43, 78, 140);background-color:#4b5075;border-radius:2px;'  # 默认on状态style
        self.states_off_style = 'border:2px solid  rgb(43, 78, 140);background-color:#039806;border-radius:2px;'  # 默认off状态style

        self.control_init()
        self.state_init()

    def state_init(self):  # 状态指示灯的初始化
        for i in range(len(self.states)):
            if i == self.start_on:
                self.states[i].setStyleSheet(self.states_on_style)
            else:
                self.states[i].setStyleSheet(self.states_off_style)

    def control_init(self):  # 控制器的初始化
        for i in self.controls:
            i.style_normal = self.controls_normal_style
            i.style_enter = self.controls_enter_style
            i.style_clicked = self.controls_clicked_style

 main.py:

from sys import exit, argv
from PyQt5.QtCore import Qt, QCoreApplication, pyqtSignal
from PyQt5.QtWidgets import QApplication
from PyQt5 import uic
import buttons  # 按钮的内嵌图片资源


############################################
# 公共的函数
class PublicFunction:  # 公共函数
    @staticmethod
    def normal_button_init(button):  # 通常规格按钮的初始化
        button.isEntered = False  # 鼠标焦点是否在内
        button.setImage()  # 获取按钮上的图片
        button.style_normal = f'background-color:#4b5075;border-radius:8px;border:2px solid  rgb(43, 78, 140);color:#e6e6e6;image:{button.image};'  # 常态style
        button.style_enter = f'background-color:#4b5075;border-radius:8px;border:3px solid  #e6e6e6;color:#e6e6e6;image:{button.image};'  # 鼠标悬停后的style
        button.style_clicked = f'background-color:#4b5075;border:2px solid  #039806;border-radius:8px;color:#e6e6e6;image:{button.image};'  # 鼠标点击后的style
        button.style_on = f'background-color:#039806;border:4px solid  #e6e6e6;border-radius:8px;color:#e6e6e6;image:{button.image};'  # ON状态的style
        button.style_disable = f'background-color:#767676;border:2px solid  rgb(43, 78, 140);border-radius:8px;color:#c6c6c6;image:{button.image};'  # 禁用状态下的style

    @staticmethod
    def green_lamp_init(lamp):  # 绿色指示灯
        lamp.rad = str(round(lamp.width() / 2))
        lamp.off_color = '#767676'  # 默认off颜色
        lamp.on_color = '#039806'  # 默认on颜色
        lamp.border_color = '#2b4e8c'  # 默认的边框颜色

    @staticmethod
    def red_lamp_init(lamp):  # 红色指示灯
        lamp.rad = str(round(lamp.width() / 2))
        lamp.off_color = '#767676'  # 默认off颜色
        lamp.on_color = '#da1c1c'  # 默认on颜色
        lamp.border_color = '#2b4e8c'  # 默认的边框颜色

    @staticmethod
    def normal_label_init(label):  # 通常标签的初始化
        label.isEntered = False  # 鼠标焦点是否在内
        label.style_normal = 'background-color:#000d47;border:2px solid #4b5075;border-radius:8px;color:#f3da83;'  # 常态style
        label.style_enter = 'background-color:#4b5075;border-radius:8px;border:3px solid  #e6e6e6;color:#e6e6e6;'  # 鼠标悬停后的style
        label.style_clicked = 'background-color:#4b5075;border:2px solid  #039806;border-radius:8px;color:#e6e6e6;'  # 鼠标点击后的style

    @staticmethod
    def normal_selector_init(selector):  # 通常选择器的初始化
        selector.start_on = 0  # 启动时状态为on的状态编号
        selector.controls_normal_style = 'color: #e6e6e6;background-color: rgba(255, 255, 255, 0);border-radius:2px;border:0px solid rgba(255, 255, 255, 0);'  # 默认控制器常态style
        selector.controls_enter_style = 'color: #e6e6e6;background-color: rgba(255, 255, 255, 0);border-radius:2px;border:1px solid rgba(255, 255, 255, 0);'  # 默认控制器鼠标悬停style
        selector.controls_clicked_style = 'color: #e6e6e6;background-color: rgba(255, 255, 255, 0);border-radius:2px;border:1px solid #039806;'  # 默认控制器鼠标点击style
        selector.states_off_style = 'border:2px solid  #2b4e8c;background-color:#4b5075;border-radius:2px;'  # 默认off状态style
        selector.states_on_style = 'border:2px solid  #2b4e8c;background-color:#039806;border-radius:2px;'  # 默认on状态style
        selector.state_init()  # 初始化状态指示灯
        selector.control_init()  # 初始化控制器



############################################
# 画面的类,每个画面对应一个同名的首字母大写的类
class Form0:  # 画面form0的类(form0是主窗口)
    def __init__(self):
        self.items = [form0.btn_exit, form0.btn_setting, form0.btn_user, form0.btn_history,
                      form0.lamp_working_1, form0.label_setting_1, form0.selector_1]  # form0的所有部件
        self.Classes = [self.Btn_exit(), self.Btn_setting(), self.Btn_user(), self.Btn_history(),
                        self.Lamp_working_1(), self.Label_setting_1(), self.Selector_1()]  # self的所有部件的类

        form0.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)  # 关掉边框,始终最前
        form0.menuBar().setVisible(False)  # 关闭顶部菜单栏
        form0.statusBar().setVisible(False)  # 关闭底部状态栏

        form0.show()  # 主画面显示

    # 画面中的部件的类,每个部件对应一个同名的首字母大写的类
    class Btn_exit:  # “退出系统”按钮对应的类

        def __init__(self):
            PublicFunction.normal_button_init(form0.btn_exit)  # 引用通常规格按钮的初始化
            form0.btn_exit.Clicked.connect(self.clicked)

        def clicked(self):  # “退出系统”按钮点击后的操作
            for f in forms:  # 关闭所有窗口
                f.close()

    class Btn_setting:  # “参数设置”按钮对应的类

        def __init__(self):
            PublicFunction.normal_button_init(form0.btn_setting)  # 引用通常规格按钮的初始化

    class Btn_user:  # “用户管理”按钮对应的类

        def __init__(self):
            PublicFunction.normal_button_init(form0.btn_user)  # 引用通常规格按钮的初始化

    class Btn_history:  # “历史数据”按钮对应的类

        def __init__(self):
            PublicFunction.normal_button_init(form0.btn_history)  # 引用通常规格按钮的初始化

    class Lamp_working_1:  # “工作指示1”指示灯对应的类

        def __init__(self):
            PublicFunction.green_lamp_init(form0.lamp_working_1)  # 初始化为绿色指示灯
            form0.lamp_working_1.color_on_bool(True)  # 初始化为on状态

    class Label_setting_1:  # “设置流量”标签对应的类

        def __init__(self):
            PublicFunction.normal_label_init(form0.label_setting_1)  # 引用通常规格标签的初始化

            form0.label_setting_1.DoubleClicked.connect(self.double_clicked)

        def double_clicked(self):
            form_setting.move(form0.label_setting_1.pos())  # 将数据设置画面移动至标签所在位置
            form_setting.label_setting.setText(form0.label_setting_1.text())  # 将标签内容显示在设置画面的设置标签内
            form_setting.show()  # 显示设置画面
            Form_setting.Btn_OK.todo = self.todo1  # 定义数据设置画面“确定”按钮按下后执行的函数

        def todo1(self):  # 定义数据设置画面“确定”按钮按下后执行的函数
            form0.label_setting_1.setText(form_setting.label_setting.text())

    class Selector_1:
        def __init__(self):
            # form0.selector_1.start_on = 1
            form0.selector_1.controls = [form0.selector1_control0, form0.selector1_control1]  # 选择器们的集合
            form0.selector_1.states = [form0.selector1_state0, form0.selector1_state1]  # 状态指示灯的集合
            PublicFunction.normal_selector_init(form0.selector_1)  # 引用通常选择器的初始化

            form0.selector1_control0.LeftClicked.connect(lambda: self.left_clicked(0))
            form0.selector1_control1.LeftClicked.connect(lambda: self.left_clicked(1))

        def left_clicked(self, n):
            for i in range(len(form0.selector_1.states)):
                if i == n:
                    form0.selector_1.states[i].setStyleSheet(form0.selector_1.states_on_style)
                else:
                    form0.selector_1.states[i].setStyleSheet(form0.selector_1.states_off_style)





class Form_setting:  # 画面form_setting的类(form_setting是共用的数据设置窗口)

    def __init__(self):  # 画面form_setting的类的初始化

        form_setting.setWindowFlags(Qt.WindowStaysOnTopHint)  # 始终最前

        self.items = [form_setting.btn_OK, form_setting.btn_cancel,
                      form_setting.label_setting]  # Form_setting的所有部件
        self.Classes = [self.Btn_OK(), self.Btn_cancel(),
                        self.Label_setting()]  # Form_setting的所有部件的类

    class Btn_OK:  # “确定”按钮的类
        todo = None  # 这是一个共用的画面,todo是本画面确定键按下后将要执行的函数
        args = []  # todo函数的参数

        def __init__(self):
            PublicFunction.normal_button_init(form_setting.btn_OK)  # 引用通常规格按钮的初始化

            form_setting.btn_OK.Clicked.connect(self.clicked)
            form_setting.btn_OK.Clicked.connect(form_setting.close)

        def clicked(self):
            self.todo(*self.args)  # 点击OK后执行预置的函数

    class Btn_cancel:  # “取消”按钮的类

        def __init__(self):
            PublicFunction.normal_button_init(form_setting.btn_cancel)  # 引用通常规格按钮的初始化

            form_setting.btn_cancel.Clicked.connect(form_setting.close)

    class Label_setting:  # “设定值”标签的类

        def __init__(self):
            PublicFunction.normal_label_init(form_setting.label_setting)  # 引用通常规格标签的初始化


############################################
# 主函数
if __name__ == '__main__':
    app = QApplication(argv)
    QCoreApplication.setAttribute(Qt.AA_EnableHighDpiScaling)  # 解决显示比例不同时的部件变形问题

    forms = []  # 所有画面显示界面的集合

    form0 = uic.loadUi('../UIS/form0.ui')  # 生成并加载画面,form0是主画面
    forms.append(form0)  # 将画面加入集合
    Form0 = Form0()  # form0对应的类是Form0,将类Form0实体化

    form_setting = uic.loadUi('../UIS/设定值.ui')
    forms.append(form_setting)
    Form_setting = Form_setting()

    exit(app.exec_())

你可能感兴趣的:(qt,python,开发语言)