pyqt5 ui设计

python小项目 · 2023-05-02

思路

整体布局通过Pyqt5 designed来设计

按键事件(槽机制)通过将ui文件转化为py文件后加入

切页操作通过实例化两个页面,再先需要显示哪个,就加载页面到主窗口中

效果

代码

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(949, 674)
        MainWindow.setStyleSheet("#MainWindow{\n"
"    background-color:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(52, 48, 255, 255), stop:1 rgba(255, 255, 255, 255));\n"
"    border-radius:30px;\n"
"\n"
"}")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.frame = QtWidgets.QFrame(self.centralwidget)
        self.frame.setGeometry(QtCore.QRect(40, 30, 901, 641))
        self.frame.setAutoFillBackground(False)
        self.frame.setStyleSheet("#frame{\n"
"    background-color:rgb(113, 113, 168);\n"
"    border-radius:30px;\n"
"}")
        self.frame.setFrameShape(QtWidgets.QFrame.Panel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.pushButton = QtWidgets.QPushButton(self.frame)
        self.pushButton.setGeometry(QtCore.QRect(20, 170, 101, 51))
        font = QtGui.QFont()
        font.setFamily("Segoe UI Variable Small")
        self.pushButton.setFont(font)
        self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.OpenHandCursor))
        self.pushButton.setStyleSheet("#pushButton{\n"
"    background-color:rgb(108, 108, 161);\n"
"    border-radius:10px;\n"
"\n"
"}")
        self.pushButton.setCheckable(True)
        self.pushButton.setChecked(False)
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.frame)
        self.pushButton_2.setGeometry(QtCore.QRect(20, 240, 101, 51))
        self.pushButton_2.setStyleSheet("#pushButton_2{\n"
"    background-color:rgb(85, 85, 127);\n"
"    border-radius:10px;\n"
"\n"
"}")
        self.pushButton_2.setCheckable(False)
        self.pushButton_2.setAutoRepeat(False)
        self.pushButton_2.setAutoExclusive(False)
        self.pushButton_2.setAutoDefault(False)
        self.pushButton_2.setDefault(False)
        self.pushButton_2.setFlat(False)
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(self.frame)
        self.pushButton_3.setGeometry(QtCore.QRect(20, 310, 101, 51))
        self.pushButton_3.setStyleSheet("#pushButton_3{\n"
"    background-color:rgb(105, 105, 156);\n"
"    border-radius:10px;\n"
"\n"
"}")
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_4 = QtWidgets.QPushButton(self.frame)
        self.pushButton_4.setGeometry(QtCore.QRect(20, 380, 101, 51))
        self.pushButton_4.setStyleSheet("#pushButton_4{\n"
"    background-color:rgb(85, 85, 127);\n"
"    border-radius:10px;\n"
"\n"
"}")
        self.pushButton_4.setObjectName("pushButton_4")
        self.pushButton_5 = QtWidgets.QPushButton(self.frame)
        self.pushButton_5.setGeometry(QtCore.QRect(20, 450, 101, 51))
        self.pushButton_5.setStyleSheet("#pushButton_5{\n"
"    background-color:rgb(127, 127, 189);\n"
"    border-radius:10px;\n"
"\n"
"}")
        self.pushButton_5.setObjectName("pushButton_5")
        self.label_4 = QtWidgets.QLabel(self.frame)
        self.label_4.setGeometry(QtCore.QRect(0, 0, 141, 601))
        self.label_4.setStyleSheet("#label_4{\n"
"    background-color:rgb(163, 163, 243);\n"
"    border-radius:30px;\n"
"}")
        self.label_4.setFrameShape(QtWidgets.QFrame.Box)
        self.label_4.setFrameShadow(QtWidgets.QFrame.Raised)
        self.label_4.setText("")
        self.label_4.setObjectName("label_4")
        self.frame_2 = QtWidgets.QFrame(self.frame)
        self.frame_2.setGeometry(QtCore.QRect(-1, 530, 901, 111))
        self.frame_2.setStyleSheet("#frame_2{\n"
"    background-color:rgb(170, 85, 255);\n"
"    border-radius:30px;\n"
"\n"
"}")
        self.frame_2.setFrameShape(QtWidgets.QFrame.Box)
        self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame_2.setObjectName("frame_2")
        self.label_2 = QtWidgets.QLabel(self.frame)
        self.label_2.setGeometry(QtCore.QRect(30, 250, 31, 31))
        self.label_2.setText("")
        self.label_2.setPixmap(QtGui.QPixmap("resource/电影.png"))
        self.label_2.setScaledContents(True)
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.frame)
        self.label_3.setGeometry(QtCore.QRect(30, 320, 31, 31))
        self.label_3.setText("")
        self.label_3.setPixmap(QtGui.QPixmap("resource/音乐.png"))
        self.label_3.setScaledContents(True)
        self.label_3.setObjectName("label_3")
        self.label_5 = QtWidgets.QLabel(self.frame)
        self.label_5.setGeometry(QtCore.QRect(30, 390, 31, 31))
        self.label_5.setText("")
        self.label_5.setPixmap(QtGui.QPixmap("resource/笔记.png"))
        self.label_5.setScaledContents(True)
        self.label_5.setObjectName("label_5")
        self.label_7 = QtWidgets.QLabel(self.frame)
        self.label_7.setGeometry(QtCore.QRect(30, 460, 31, 31))
        self.label_7.setText("")
        self.label_7.setPixmap(QtGui.QPixmap("resource/记单词.png"))
        self.label_7.setScaledContents(True)
        self.label_7.setObjectName("label_7")
        self.label_9 = QtWidgets.QLabel(self.frame)
        self.label_9.setGeometry(QtCore.QRect(400, 150, 131, 41))
        font = QtGui.QFont()
        font.setPointSize(13)
        self.label_9.setFont(font)
        self.label_9.setAutoFillBackground(False)
        self.label_9.setObjectName("label_9")
        self.toolButton = QtWidgets.QToolButton(self.frame)
        self.toolButton.setGeometry(QtCore.QRect(680, 160, 61, 31))
        self.toolButton.setStyleSheet("#toolButton{\n"
"    background-color:rgb(105, 105, 156);\n"
"    border-radius:10px;\n"
"\n"
"}")
        self.toolButton.setText("")
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("resource/开关.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        icon.addPixmap(QtGui.QPixmap("resource/开关-开.png"), QtGui.QIcon.Disabled, QtGui.QIcon.Off)
        icon.addPixmap(QtGui.QPixmap("resource/开关-开.png"), QtGui.QIcon.Active, QtGui.QIcon.On)
        self.toolButton.setIcon(icon)
        self.toolButton.setIconSize(QtCore.QSize(50, 33))
        self.toolButton.setCheckable(False)
        self.toolButton.setAutoRepeat(False)
        self.toolButton.setAutoExclusive(False)
        self.toolButton.setAutoRaise(True)
        self.toolButton.setObjectName("toolButton")
        self.label_8 = QtWidgets.QLabel(self.frame)
        self.label_8.setGeometry(QtCore.QRect(350, 300, 71, 61))
        self.label_8.setText("")
        self.label_8.setPixmap(QtGui.QPixmap("resource/加速云服务器.png"))
        self.label_8.setScaledContents(True)
        self.label_8.setObjectName("label_8")
        self.label = QtWidgets.QLabel(self.frame)
        self.label.setGeometry(QtCore.QRect(30, 180, 31, 31))
        self.label.setText("")
        self.label.setPixmap(QtGui.QPixmap("加速.png"))
        self.label.setScaledContents(True)
        self.label.setObjectName("label")
        self.label_6 = QtWidgets.QLabel(self.frame)
        self.label_6.setGeometry(QtCore.QRect(30, 60, 51, 51))
        self.label_6.setText("")
        self.label_6.setPixmap(QtGui.QPixmap("resource/Snipaste_2023-05-02_17-54-52.jpg"))
        self.label_6.setScaledContents(True)
        self.label_6.setObjectName("label_6")
        self.label_10 = QtWidgets.QLabel(self.frame)
        self.label_10.setGeometry(QtCore.QRect(30, 60, 51, 51))
        self.label_10.setText("")
        self.label_10.setPixmap(QtGui.QPixmap("resource/加速.png"))
        self.label_10.setScaledContents(True)
        self.label_10.setObjectName("label_10")
        self.label_11 = QtWidgets.QLabel(self.frame)
        self.label_11.setGeometry(QtCore.QRect(350, 290, 71, 81))
        self.label_11.setText("")
        self.label_11.setPixmap(QtGui.QPixmap("resource/加速.png"))
        self.label_11.setScaledContents(True)
        self.label_11.setObjectName("label_11")
        self.label_12 = QtWidgets.QLabel(self.frame)
        self.label_12.setGeometry(QtCore.QRect(360, 160, 181, 21))
        self.label_12.setStyleSheet("#label_12{\n"
"    background-color:rgb(170, 170, 255);\n"
"    border-radius:10px;\n"
"\n"
"}")
        self.label_12.setText("")
        self.label_12.setObjectName("label_12")
        self.label_13 = QtWidgets.QLabel(self.frame)
        self.label_13.setEnabled(True)
        self.label_13.setGeometry(QtCore.QRect(680, 290, 71, 81))
        self.label_13.setText("")
        self.label_13.setPixmap(QtGui.QPixmap("resource/加速.png"))
        self.label_13.setScaledContents(True)
        self.label_13.setObjectName("label_13")
        self.label_14 = QtWidgets.QLabel(self.frame)
        self.label_14.setGeometry(QtCore.QRect(680, 300, 71, 61))
        self.label_14.setText("")
        self.label_14.setPixmap(QtGui.QPixmap("resource/加速云服务器.png"))
        self.label_14.setScaledContents(True)
        self.label_14.setObjectName("label_14")
        self.label_15 = QtWidgets.QLabel(self.frame)
        self.label_15.setGeometry(QtCore.QRect(370, 160, 21, 21))
        self.label_15.setText("")
        self.label_15.setPixmap(QtGui.QPixmap("resource/闪电.png"))
        self.label_15.setScaledContents(True)
        self.label_15.setObjectName("label_15")
        self.label_16 = QtWidgets.QLabel(self.frame)
        self.label_16.setGeometry(QtCore.QRect(400, 280, 151, 81))
        self.label_16.setText("")
        self.label_16.setPixmap(QtGui.QPixmap("resource/reshot-icon-pack-electric-circuits-line-green-black-icons-JVC2RTX4SZ/reshot-icon-ac-supply-PDSQUTXWKN.svg"))
        self.label_16.setScaledContents(True)
        self.label_16.setObjectName("label_16")
        self.label_17 = QtWidgets.QLabel(self.frame)
        self.label_17.setGeometry(QtCore.QRect(510, 280, 121, 81))
        self.label_17.setText("")
        self.label_17.setPixmap(QtGui.QPixmap("resource/reshot-icon-pack-electric-circuits-line-green-black-icons-JVC2RTX4SZ/reshot-icon-ac-supply-PDSQUTXWKN.svg"))
        self.label_17.setScaledContents(True)
        self.label_17.setObjectName("label_17")
        self.label_18 = QtWidgets.QLabel(self.frame)
        self.label_18.setGeometry(QtCore.QRect(600, 280, 111, 81))
        self.label_18.setText("")
        self.label_18.setPixmap(QtGui.QPixmap("resource/reshot-icon-pack-electric-circuits-line-green-black-icons-JVC2RTX4SZ/reshot-icon-ac-supply-PDSQUTXWKN.svg"))
        self.label_18.setScaledContents(True)
        self.label_18.setObjectName("label_18")
        self.label_19 = QtWidgets.QLabel(self.frame)
        self.label_19.setGeometry(QtCore.QRect(370, 300, 361, 81))
        self.label_19.setText("")
        self.label_19.setPixmap(QtGui.QPixmap("resource/reshot-icon-pack-electric-circuits-line-green-black-icons-JVC2RTX4SZ/reshot-icon-four-cells-joined-YC8Z56KHVJ.svg"))
        self.label_19.setScaledContents(True)
        self.label_19.setObjectName("label_19")
        self.label_20 = QtWidgets.QLabel(self.frame)
        self.label_20.setGeometry(QtCore.QRect(520, 220, 54, 51))
        self.label_20.setText("")
        self.label_20.setPixmap(QtGui.QPixmap("resource/dcdn 全站加速.png"))
        self.label_20.setScaledContents(True)
        self.label_20.setObjectName("label_20")
        self.label_19.raise_()
        self.label_16.raise_()
        self.label_18.raise_()
        self.label_11.raise_()
        self.label_12.raise_()
        self.toolButton.raise_()
        self.label_4.raise_()
        self.pushButton.raise_()
        self.pushButton_2.raise_()
        self.pushButton_3.raise_()
        self.pushButton_4.raise_()
        self.pushButton_5.raise_()
        self.frame_2.raise_()
        self.label_2.raise_()
        self.label_3.raise_()
        self.label_5.raise_()
        self.label_7.raise_()
        self.label_9.raise_()
        self.label_8.raise_()
        self.label.raise_()
        self.label_6.raise_()
        self.label_10.raise_()
        self.label_13.raise_()
        self.label_14.raise_()
        self.label_15.raise_()
        self.label_17.raise_()
        self.label_20.raise_()
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        self.pushButton.clicked.connect(self.turn) # 设置按键函数
        self.toolButton.released.connect(self.label_20.hide) # type: ignore
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "           代理"))
        self.pushButton_2.setText(_translate("MainWindow", "      电影"))
        self.pushButton_3.setText(_translate("MainWindow", "      音乐"))
        self.pushButton_4.setText(_translate("MainWindow", "      笔记"))
        self.pushButton_5.setText(_translate("MainWindow", "      单词"))
        self.label_9.setText(_translate("MainWindow", "关闭/开启代理"))
    def turn(self):
      two.setupUi(MainWindow)
      
if __name__ == '__main__':
    import sys
    from PyQt5.QtWidgets import QApplication, QMainWindow
    import ui # 导入添加的资源(根据实际情况填写文件名)
    import two# two为第二个页面
    app = QApplication(sys.argv)
    MainWindow = QMainWindow() 
    ui = Ui_MainWindow()
    two = two.Ui_MainWindow()#将two实例化
    ui.setupUi(MainWindow)
    
    MainWindow.show()
    sys.exit(app.exec_())
  1. 飞翔的 2023-07-22

    😀

  2. 猜猜我是谁 2023-07-21

  3. admin (作者)  2023-05-17

    Hello

Theme Jasmine by Kent Liao