PDA

View Full Version : PyQt5 code not working



tchachkii
5th December 2018, 10:28
so I'm new to PyQt5 and have am trying to open a new window when a button is pressed. I know this should be simple and have managed to copy other examples to produce my code but it doesn't run - the second window opens then instantly closes. this should be a simple fix but I don't get where I've gone wrong, could anyone help me? thanks :o


from PyQt5.QtWidgets import *

class StartScreen(QWidget):
def __init__(self):
self.window = QWidget()
self.window.setGeometry(50, 50, 350, 200)
self.window.setWindowTitle("Fantasy F1")
self.UserOrNot()
self.window.show()

def UserOrNot(self):
self.layout = QGridLayout()
self.UserQuestion = QLabel("Are you an existing user or a new user?")
self.layout.addWidget(self.UserQuestion)

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

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

self.window.setLayout(self.layout)
# self.window.show()


def NewUserPressed():
print("button pressed")
main = CreateUser()

class CreateUser(QWidget):
def __init__(self):
self.window = QWidget()
self.window.setGeometry(50, 50, 350, 250)
self.window.setWindowTitle("Create a team")
self.CreateUserForm()


def CreateUserForm(self):
self.layout = QFormLayout()

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

self.OKButton = QPushButton("OK")
self.layout.addWidget(self.OKButton)

self.CancelButton = QPushButton("Cancel")
self.layout.addWidget(self.CancelButton)

self.window.setLayout(self.layout)
self.window.show()


if __name__ == '__main__':
import sys

app = QApplication(sys.argv)
main = StartScreen()


sys.exit(app.exec_())

d_stranz
5th December 2018, 16:06
I don't know enough Python (yet) to be certain, but the behavior you describe sounds like a scoping issue - that is, the variable holding the new widget you create only exists for the lifetime of the method where it is created and then goes out of scope, at which point the widget is destroyed. This happens in C++ Qt when a widget is declared as a local variable in a method; it is created and then immediately is destroyed when the method exits.

Another possible cause could be because you are replacing your main window with the new one. Qt normally will exit an app when the last top-level widget is destroyed. My understanding of Python is that when variables are reassigned, the object previously assigned to the variable goes out of scope and is scheduled for garbage collection. Try removing the assignment of CreateUser to main in NewUserPressed() and see what happens.