在按下/ without /类PyQT5后,打开新窗口

时间:2018-10-30 14:29:11

标签: python pyqt pyqt5

对于一个学校项目,我试图在按下按钮后在PyQT5中打开一个新窗口。我的代码还没有任何类,只有2个函数可以在2个文件中打开2个单独的窗口,但是我试图将两者合并在一起。有没有类的方法吗?还是我需要先将两个窗口放在类中?

我问是因为到目前为止找到的所有解决方案都涉及类,但是到目前为止我的代码没有任何类。我需要为每个窗口制作类以使其正常工作吗,还是有一个不涉及制作类的替代版本(这是我想要的目标)?

我的代码在下面。

import sys
from PyQt5.QtWidgets import *

app = QApplication(sys.argv)

window = QWidget()
##window.setGeometry(50, 50, 350, 200)
##window.setWindowTitle("Fantasy F1")

def ExistingUserPressed():
    print("Existing user pressed")

def NewUserPressed():
    print("New user pressed")

def UserOrNot():

    window.setGeometry(50, 50, 350, 200)
    window.setWindowTitle("Fantasy F1")

    layout = QGridLayout()

    UserQuestion = QLabel("Are you an existing user or a new user?")
    layout.addWidget(UserQuestion)


    ## creates a button for existing users
    ExistingUser = QPushButton("Existing user")
    ExistingUser.clicked.connect(ExistingUserPressed)
    layout.addWidget(ExistingUser)

    ## creates a button for new users
    NewUser = QPushButton("New user")
    NewUser.clicked.connect(NewUserPressed)
    NewUser.clicked.connect(CreateUserForm)
    layout.addWidget(NewUser)


    window.setLayout(layout)
    window.show()



def OKPressed():
    print("OK pressed")

def CancelPressed():
    print("Cancel pressed")

def CreateUserForm():

    window = QWidget()
    window.setGeometry(50, 50, 350, 250)
    window.setWindowTitle("Create a team")

    layout = QFormLayout()

    ## creates a form (age is spinbox to make sure user can only enter numbers)
    layout.addRow(QLabel("Username:"), QLineEdit())
    layout.addRow(QLabel("Team name:"), QLineEdit())
    layout.addRow(QLabel("First name:"), QLineEdit())
    layout.addRow(QLabel("Last name:"), QLineEdit())
    layout.addRow(QLabel("Password:"), QLineEdit())
    layout.addRow(QLabel("Email address:"), QLineEdit())
    layout.addRow(QLabel("Age:"), QSpinBox()) 

    #layout.addWidget(QPushButton("OK"))

    OKButton = QPushButton("OK")
    layout.addWidget(OKButton)
    OKButton.clicked.connect(OKPressed)


    CancelButton = QPushButton("Cancel")
    layout.addWidget(CancelButton)
    CancelButton.clicked.connect(CancelPressed)

    #layout.addWidget(QPushButton("Cancel"))

    window.setLayout(layout)

    window.show()


if __name__ == '__main__':
    user = UserOrNot()

0 个答案:

没有答案