问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

使用PySide6实现Qt界面跳转:信号和槽机制的应用

创作时间:
作者:
@小白创作中心

使用PySide6实现Qt界面跳转:信号和槽机制的应用

引用
CSDN
1.
https://m.blog.csdn.net/Zq13140504/article/details/146214001

本文将通过一个具体的案例,演示如何使用PySide6实现Qt界面跳转。我们将创建两个窗口,当用户在第一个窗口中输入正确的用户名和密码时,程序将跳转到第二个窗口。这个案例将帮助你理解PySide6中的信号和槽机制,以及如何在多个窗口之间进行交互。

import sys
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QWidget, QApplication, QPushButton, QLineEdit
from Form import Ui_Form
from second import Ui_second

class MyWidget(QWidget, Ui_Form):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        # 获取按钮并连接信号槽
        self.pushButton: QPushButton = self.findChild(QPushButton, "pushButton")# 获取登录按钮
        self.pushButton.clicked.connect(self.btn_slot)
        self.useNameEdit: QLineEdit = self.findChild(QLineEdit, "lineEdit") # 获取用户名编辑框
        self.passWordEdit: QLineEdit = self.findChild(QLineEdit, "lineEdit_2") # 获取密码编辑框

    def btn_slot(self):
        if myWidget.useNameEdit.text() == 'admin' and myWidget.passWordEdit.text() == '123456':
            # 关闭当前窗口
            self.close()
            # 显示第二个窗口
            self.second_window.show()
        else:
            #提示用户名密码错误弹窗:
            print('用户名或密码错误')
            myWidget.useNameEdit.clear()
            myWidget.passWordEdit.clear()

class SecondWindow(QWidget, Ui_second):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.setWindowFlag(Qt.WindowType.FramelessWindowHint)
        self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)

if __name__ == '__main__':
    # 创建应用程序对象
    app = QApplication(sys.argv)
    # 实例化窗口
    myWidget = MyWidget()
    second_window = SecondWindow()
    myWidget.second_window = second_window # 将第二个窗口作为属性
    # 设置无边框和透明背景
    myWidget.setWindowFlag(Qt.WindowType.FramelessWindowHint)
    myWidget.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
    # 显示窗口
    myWidget.show()
    # 进入消息循环
    sys.exit(app.exec())


© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号