简体   繁体   English

PyQt5:如何通过单击按钮打开一个新对话框

[英]PyQt5: How to open a new Dialog with button click

import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.uic import loadUi

class LoginPage(QDialog):
    def __init__(self):
        super(LoginPage, self).__init__()
        loadUi('LoginPage.ui', self)

class RegisterPage(QDialog):
    def __init__(self):
        super(RegisterPage, self).__init__()
        loadUi('RegisterPage.ui', self)


class HomePage(QDialog):
    def __init__(self):
        super(HomePage, self).__init__()
        loadUi('HomePage.ui', self)
        #self.btnLoginPage.clicked.connect(self.executeLoginPage)
        #self.btnRegisterPage.clicked.connect(self.executeRegisterPage)

app = QApplication(sys.argv)
widget = HomePage()
widget.show()
sys.exit(app.exec_())

I have made 3 .ui files using qt designer . 我已经使用qt designer制作了3个.ui文件。

  1. HomePage.ui HomePage.ui
  2. LoginPage.ui LoginPage.ui
  3. RegisterPage.ui RegisterPage.ui

With this code, I can display the HomePage, which has 2 buttons. 使用此代码,我可以显示具有2个按钮的主页。 When I press a button, LoginPage or RegisterPage should open. 当我按下按钮时,应该打开LoginPage或RegisterPage。

This is where the problem comes, I do not know how do I display the other 2 Dialogs. 这是问题所在,我不知道如何显示其他两个对话框。 Any help will be appreciated 任何帮助将不胜感激

It's simple, in the slots you have to create the objects and show them: 很简单,您必须在插槽中创建对象并显示它们:

...
class HomePage(QDialog):
    def __init__(self):
        super(HomePage, self).__init__()
        loadUi('HomePage.ui', self)
        self.btnLoginPage.clicked.connect(self.executeLoginPage)
        self.btnRegisterPage.clicked.connect(self.executeRegisterPage)

    def executeLoginPage(self):
        login_page = LoginPage()
        login_page.exec_()

    def executeRegisterPage(self):
        register_page = RegisterPage()
        register_page.exec_()
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM