您现在的位置是:首页 >技术教程 >纯手写pyqt界面网站首页技术教程

纯手写pyqt界面

weixin_49577420 2024-06-17 11:25:15
简介纯手写pyqt界面
from PyQt5.Qt import *
import logging





class Ui(QWidget):
    def __init__(self):
        super().__init__()
        self.ui()
    def ui(self):
        self.label_list = [self.__creat_label() for _ in range(10)]  #创建10个标签
        self.btn_list = [self.__creat_buttons(objname=f'btn-{i}',text=f'button-{i}') for i in range(6)] #创建6个按钮

        self.mylayout()

    def mylayout(self):
        layout = QVBoxLayout()
        layout.addLayout(self.__head_layout())
        layout.addLayout(self.__body_layout())
        layout.addLayout(self.__foot_layout())
        self.setLayout(layout)

    def __creat_label(self):
        label=QLabel()
        label.setFixedSize(30,30)
        label.setStyleSheet('background-color:gray;border-radius:15px;')
        return label

    def __creat_buttons(self,objname:str,text:str):
        btn=QPushButton()
        btn.setObjectName(objname)
        btn.setText(text)
        btn.resize(50,30)
        return btn

    def __head_layout(self):
        layout=QHBoxLayout()
        for i in range(5):
            layout.addWidget(self.label_list[i])
        return layout

    def __body_layout(self):
        layout=QHBoxLayout()
        for i in range(5,10):
            layout.addWidget(self.label_list[i])
        return layout

    def __foot_layout(self):
        layout=QHBoxLayout()
        for i in range(6):
            layout.addSpacerItem(QSpacerItem(20,230))
            layout.addWidget(self.btn_list[i])
            layout.addSpacerItem(QSpacerItem(50,230))

        return layout

    def change_label_color(self,a):
        if a == 1:
            self.label_list[0].setStyleSheet('background-color:green;border-radius:15px;')
        else:
            self.label_list[0].setStyleSheet('background-color:red;border-radius:15px;')


class Window(Ui,QWidget):
    '''
    这里完成逻辑代码
    '''
    def __init__(self):
        super().__init__()
        #slots
        self.btn_list[0].clicked.connect(self.slot_btn1)

        self.timer=QTimer(self)
        self.timer.timeout.connect(self.time_event)

    def slot_btn1(self):
        # print('hello')
        self.timer.start(200)

    def time_event(self):
        import random
        a=random.randint(0,1)
        self.change_label_color(a)



if __name__=="__main__":
    import sys
    app=QApplication(sys.argv)
    win=Window()
    win.show()
    sys.exit(app.exec_())
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。