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

结果展示:

登录成功跳转的页面

相关推荐
ituff8 分钟前
微软认证考试又免费了
后端·python·flask
梁正雄1 小时前
2、Python流程控制
开发语言·python
Eric.Lee20212 小时前
ubuntu 安装 Miniconda
linux·运维·python·ubuntu·miniconda
无心水2 小时前
【Python实战进阶】1、Python高手养成指南:四阶段突破法从入门到架构师
开发语言·python·django·matplotlib·gil·python实战进阶·python工程化实战进阶
李剑一3 小时前
Python学习笔记1
python
Salt_07285 小时前
DAY 19 数组的常见操作和形状
人工智能·python·机器学习
无心水5 小时前
【Python实战进阶】2、Jupyter Notebook终极指南:为什么说不会Jupyter就等于不会Python?
python·jupyter·信息可视化·binder·google colab·python实战进阶·python工程化实战进阶
上班日常摸鱼6 小时前
Shell脚本基础教程:变量、条件判断、循环、函数实战(附案例)
python
无心水7 小时前
【Python实战进阶】5、Python字符串终极指南:从基础到高性能处理的完整秘籍
开发语言·网络·python·字符串·unicode·python实战进阶·python工业化实战进阶
2301_807583237 小时前
了解python,并编写第一个程序,常见的bug
linux·python