PythonQt练习

下面是一个简单登录界面的搭建

python 复制代码
import sys

# 从PySide6中导入必要的模块,用于创建GUI界面
from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QLineEdit
from QtCore import Qt

# 导入Qt Designer生成的UI文件
from TAJ import Ui_Form
from second import Ui_Form2


# 主窗口类,表示第一个登录页面
class MyWidget(Ui_Form, QWidget):
    def __init__(self):
        # 初始化QWidget并设置UI界面
        super().__init__()
        self.setupUi(self)

        # 通过对象名称查找子控件(按钮和文本框)
        self.btn1 = self.findChild(QPushButton, "log_on")  # 登录按钮
        self.btn2 = self.findChild(QPushButton, "cancel")  # 取消按钮
        self.userid = self.findChild(QLineEdit, "userid")  # 用户名输入框
        self.pwd = self.findChild(QLineEdit, "password")  # 密码输入框

        # 连接按钮点击事件到相应的函数
        self.btn1.clicked.connect(self.logon)  # 连接登录按钮点击事件到logon函数
        self.btn2.clicked.connect(self.exit)  # 连接取消按钮点击事件到exit函数

    def logon(self):
        # 处理登录操作的函数
        if self.userid.text() == "admin" and self.pwd.text() == "123456":  # 验证用户名和密码是否正确
            print("登录成功")  # 打印"登录成功"
            self.close()  # 关闭当前窗口
            self.second = Second()  # 创建第二个窗口的实例
            self.second.show()  # 显示第二个窗口
        else:
            print("登录失败")  # 打印"登录失败"
            self.userid.setText("")  # 清空用户名输入框
            self.pwd.setText("")  # 清空密码输入框

    def exit(self):
        # 处理取消按钮点击事件的函数
        print("取消登录")  # 打印"取消登录"
        self.close()  # 关闭当前窗口


# 第二个页面类(登录成功后显示)
class Second(Ui_Form2, QWidget):
    def __init__(self):
        # 初始化QWidget并设置UI界面
        super().__init__()
        self.setupUi(self)


# 程序主入口
if __name__ == "__main__":
    app = QApplication(sys.argv)  # 创建QApplication对象,程序必须有一个QApplication对象
    my_widget = MyWidget()  # 创建MyWidget类的实例
    my_widget.setWindowFlag(Qt.WindowType.FramelessWindowHint)  # 设置窗口为无边框
    my_widget.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)  # 设置窗口背景为透明
    my_widget.show()  # 显示主窗口
    sys.exit(app.exec())  # 启动事件循环,直到程序关闭

调用的主页面搭建如下

python 复制代码
# -*- coding: utf-8 -*-

################################################################################
## Form generated from reading UI file 'TomANDJerry.ui'
##
## Created by: Qt User Interface Compiler version 6.8.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QFrame, QGroupBox, QLabel,
    QLineEdit, QPushButton, QRadioButton, QSizePolicy,
    QWidget)
import res_rc

class Ui_Form(object):
    def setupUi(self, Form):
        if not Form.objectName():
            Form.setObjectName(u"Form")
        Form.resize(634, 558)
        Form.setStyleSheet(u"*{\n"
"background-color: rgb(255, 255, 255);\n"
"}\n"
"QFrame#frame{\n"
"	border-image: url(:/\u732b\u9f20/\u732b\u548c\u8001\u9f20.jpg);\n"
"    border-radius: 30px;\n"
"}\n"
"#frame_2 {\n"
"	background-color: rgba(85, 170, 255, 80);\n"
"border-radius: 30px;\n"
"}\n"
"QLabel#backgr{\n"
"    border-radius:30px;\n"
"    background-color: rgba(83, 83, 83, 120);\n"
"}\n"
"#Title{\n"
"    background:transparent; /*\u5b8c\u5168\u900f\u660e*/\n"
"    \n"
"    font: 15pt \"\u7b49\u7ebf\";\n"
"    \n"
"    color: rgba(255, 255, 255, 130);\n"
"}\n"
"#Tom {\n"
"	border-image: url(:/\u732b\u9f20/\u732b.jpg);\n"
"}\n"
"#Jerry{\n"
"	border-image: url(:/\u732b\u9f20/\u9f20.jpg);\n"
"}\n"
"#user{\n"
"	background:transparent; /*\u5b8c\u5168\u900f\u660e*/\n"
"	border-image: url(:/\u732b\u9f20/\u7528\u6237.png);\n"
"}\n"
"#passwd{\n"
"	border-image: url(:/\u732b\u9f20/\u5bc6\u7801.jpg);\n"
"}\n"
"QLineEdit{\n"
"    background:transparent; /*\u5b8c\u5168\u900f\u660e*/\n"
"    border:none;\n"
"    border-bottom:1px solid rgba(255"
                        ", 255, 255, 130);\n"
"    color: rgba(0, 0, 0, 130);\n"
"    \n"
"    font: 18pt \"\u7b49\u7ebf\";\n"
"\n"
"}\n"
"QRadioButton{\n"
"	background:transparent; /*\u5b8c\u5168\u900f\u660e*/\n"
"}\n"
"QGroupBox{\n"
"	background:transparent; /*\u5b8c\u5168\u900f\u660e*/\n"
"	border:none;\n"
"}\n"
"QPushButton{\n"
"    \n"
"    background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(35, 66, 202, 255), stop:1 rgba(255, 255, 255, 255));\n"
"    border-radius:5px;\n"
"    font: 13pt \"\u7b49\u7ebf\";\n"
"    \n"
"    color: rgba(255, 255, 255, 130);\n"
"}\n"
"\n"
"QPushButton:hover{ /*\u9f20\u6807\u79fb\u52a8*/\n"
"    \n"
"    background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(75, 66, 202, 255), stop:1 rgba(255, 255, 255, 255));\n"
"    border-radius:5px;\n"
"    font: 13pt \"\u7b49\u7ebf\";\n"
"    \n"
"    color: rgba(255, 255, 255, 130);\n"
"}\n"
"\n"
"QPushButton:pressed{ /*\u9f20\u6807\u6309\u4e0b*/\n"
"    \n"
"    background-color: qlineargradient(spread:p"
                        "ad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(35, 66, 202, 255), stop:1 rgba(255, 0, 0, 255));\n"
"    border-radius:5px;\n"
"    font: 13pt \"\u7b49\u7ebf\";\n"
"    \n"
"    color: rgba(255, 255, 255, 130);\n"
"\n"
"    padding-top:5px;\n"
"    padding-left:5px;\n"
"}\n"
"")
        self.frame = QFrame(Form)
        self.frame.setObjectName(u"frame")
        self.frame.setGeometry(QRect(110, 50, 441, 421))
        self.frame.setFrameShape(QFrame.Shape.StyledPanel)
        self.frame.setFrameShadow(QFrame.Shadow.Raised)
        self.frame_2 = QFrame(self.frame)
        self.frame_2.setObjectName(u"frame_2")
        self.frame_2.setGeometry(QRect(0, 0, 441, 421))
        self.frame_2.setFrameShape(QFrame.Shape.StyledPanel)
        self.frame_2.setFrameShadow(QFrame.Shadow.Raised)
        self.backgr = QLabel(self.frame_2)
        self.backgr.setObjectName(u"backgr")
        self.backgr.setGeometry(QRect(50, 30, 351, 351))
        font = QFont()
        font.setPointSize(15)
        self.backgr.setFont(font)
        self.backgr.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.Title = QLabel(self.frame_2)
        self.Title.setObjectName(u"Title")
        self.Title.setGeometry(QRect(200, 80, 71, 31))
        font1 = QFont()
        font1.setFamilies([u"\u7b49\u7ebf"])
        font1.setPointSize(15)
        font1.setBold(False)
        font1.setItalic(False)
        self.Title.setFont(font1)
        self.Title.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.userid = QLineEdit(self.frame_2)
        self.userid.setObjectName(u"userid")
        self.userid.setGeometry(QRect(190, 180, 113, 20))
        self.password = QLineEdit(self.frame_2)
        self.password.setObjectName(u"password")
        self.password.setGeometry(QRect(190, 230, 113, 20))
        self.password.setEchoMode(QLineEdit.EchoMode.Password)
        self.log_on = QPushButton(self.frame_2)
        self.log_on.setObjectName(u"log_on")
        self.log_on.setGeometry(QRect(140, 310, 81, 31))
        self.user = QLabel(self.frame_2)
        self.user.setObjectName(u"user")
        self.user.setGeometry(QRect(150, 170, 41, 31))
        self.passwd = QLabel(self.frame_2)
        self.passwd.setObjectName(u"passwd")
        self.passwd.setGeometry(QRect(160, 220, 31, 31))
        self.groupBox = QGroupBox(self.frame_2)
        self.groupBox.setObjectName(u"groupBox")
        self.groupBox.setEnabled(True)
        self.groupBox.setGeometry(QRect(120, 250, 241, 51))
        font2 = QFont()
        font2.setKerning(True)
        self.groupBox.setFont(font2)
        self.rbtn2 = QRadioButton(self.groupBox)
        self.rbtn2.setObjectName(u"rbtn2")
        self.rbtn2.setGeometry(QRect(170, 30, 16, 20))
        self.rbtn2.setChecked(False)
        self.rbtn2.setAutoRepeat(True)
        self.rbtn1 = QRadioButton(self.groupBox)
        self.rbtn1.setObjectName(u"rbtn1")
        self.rbtn1.setGeometry(QRect(30, 30, 16, 20))
        self.rbtn1.setChecked(True)
        self.rbtn1.setAutoRepeat(False)
        self.Tom = QLabel(self.groupBox)
        self.Tom.setObjectName(u"Tom")
        self.Tom.setGeometry(QRect(50, 20, 41, 31))
        self.Jerry = QLabel(self.groupBox)
        self.Jerry.setObjectName(u"Jerry")
        self.Jerry.setGeometry(QRect(190, 20, 41, 31))
        self.cancel = QPushButton(self.frame_2)
        self.cancel.setObjectName(u"cancel")
        self.cancel.setGeometry(QRect(290, 310, 81, 31))

        self.retranslateUi(Form)

        QMetaObject.connectSlotsByName(Form)
    # setupUi

    def retranslateUi(self, Form):
        Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None))
        self.backgr.setText("")
        self.Title.setText(QCoreApplication.translate("Form", u"log in ", None))
        self.userid.setPlaceholderText(QCoreApplication.translate("Form", u"Username", None))
        self.password.setPlaceholderText(QCoreApplication.translate("Form", u"Password", None))
        self.log_on.setText(QCoreApplication.translate("Form", u"\u767b\u5f55", None))
        self.user.setText("")
        self.passwd.setText("")
        self.groupBox.setTitle("")
        self.rbtn2.setText("")
        self.rbtn1.setText("")
        self.Tom.setText("")
        self.Jerry.setText("")
        self.cancel.setText(QCoreApplication.translate("Form", u"\u53d6\u6d88", None))
    # retranslateUi

登录成功的页面目前留白,没有搭建任何内容

python 复制代码
# -*- coding: utf-8 -*-

################################################################################
## Form generated from reading UI file 'second.ui'
##
## Created by: Qt User Interface Compiler version 6.8.1
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
    QMetaObject, QObject, QPoint, QRect,
    QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
    QFont, QFontDatabase, QGradient, QIcon,
    QImage, QKeySequence, QLinearGradient, QPainter,
    QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QSizePolicy, QWidget)

class Ui_Form2(object):
    def setupUi(self, Form):
        if not Form.objectName():
            Form.setObjectName(u"Form")
        Form.resize(400, 300)

        self.retranslateUi(Form)

        QMetaObject.connectSlotsByName(Form)
    # setupUi

    def retranslateUi(self, Form):
        Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None))
    # retranslateUi

结果展示:

登录成功跳转的页面

相关推荐
用户83562907805114 小时前
Python 实现 PDF 文件加密与解密方法
后端·python
用户83562907805114 小时前
使用 Python 冻结与拆分 Excel 窗格教程
后端·python
你好潘先生1 天前
别再记命令了,用 yeero do 说句人话就能跑脚本,而且不烧 token
服务器·python·命令行
Agent_大师1 天前
WebSocket 行情重连成功,K线缺口不会自动消失
python
荣码1 天前
LLM结构化输出:让AI返回JSON而不是废话,我踩了4个坑
java·python
copyer_xyf1 天前
FastAPI 如何连接 MySQL
后端·python
apocelipes2 天前
常用编程语言和库的正则表达式性能对比
c语言·c++·python·性能优化·golang·开发工具和环境
用户8356290780512 天前
使用 Python 在 PDF 中创建与管理书签
后端·python
MeixianAgent2 天前
Python 回测数据入口怎么验?历史 K 线入库前先做 5 个检查
后端·python
咕白m6252 天前
用 Python 实现一键批量查找与替换 Excel 数据
后端·python