1

for a school project I'm trying to open a new window in PyQT5 after a button is pressed. my code doesn't have any classes yet, just 2 functions that open 2 separate windows in 2 files, but I'm trying to merge the two together. is there a way of doing this without classes or do I need to put the 2 windows in classes first?

I'm asking because all the solutions I've found so far all involve classes, but so far my code doesn't have any classes. do I need to make classes for each window to make it work or is there an alternate version that doesn't involve making classes (which is want I'm aiming for)?

my code is below.

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()
NotEvenTheRain
  • 113
  • 1
  • 10
  • See https://stackoverflow.com/questions/51819827/pyqt5-switch-between-windows – S. Nick Oct 30 '18 at 14:43
  • @S.Nick I had a look at the solution but I noticed that the code to start off with was already in classes - something that my code isn't. I was wondering if there is a way of opening a new window without sorting my code out into classes or if that simply is the best method? – NotEvenTheRain Oct 31 '18 at 15:32
  • 1
    @Grace A GUI is made to work with OOP, so avoiding it involves an inappropriate method since the GUI has a life cycle that just the class has but the functions do not. so you must learn to use classes if you do not know it :-) – eyllanesc Oct 31 '18 at 15:44
  • @eyllanesc thanks, I do know how to use classes (just about, we've just learnt about them) but I had started coding before then and was wondering if there was an alternate method but if not, then I'd just use them. thanks :) – NotEvenTheRain Nov 01 '18 at 11:23

0 Answers0