Results 1 to 2 of 2

Thread: PyQt5 code not working

  1. #1
    Join Date
    Dec 2018
    Posts
    1
    Platforms
    Windows

    Default PyQt5 code not working

    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

    Qt Code:
    1. from PyQt5.QtWidgets import *
    2.  
    3. class StartScreen(QWidget):
    4. def __init__(self):
    5. self.window = QWidget()
    6. self.window.setGeometry(50, 50, 350, 200)
    7. self.window.setWindowTitle("Fantasy F1")
    8. self.UserOrNot()
    9. self.window.show()
    10.  
    11. def UserOrNot(self):
    12. self.layout = QGridLayout()
    13. self.UserQuestion = QLabel("Are you an existing user or a new user?")
    14. self.layout.addWidget(self.UserQuestion)
    15.  
    16. ## creates a button for existing users
    17. self.ExistingUser = QPushButton("Existing user")
    18. self.layout.addWidget(self.ExistingUser)
    19.  
    20. ## creates a button for new users
    21. self.NewUser = QPushButton("New user")
    22. self.NewUser.clicked.connect(NewUserPressed)
    23. self.layout.addWidget(self.NewUser)
    24.  
    25. self.window.setLayout(self.layout)
    26. # self.window.show()
    27.  
    28.  
    29. def NewUserPressed():
    30. print("button pressed")
    31. main = CreateUser()
    32.  
    33. class CreateUser(QWidget):
    34. def __init__(self):
    35. self.window = QWidget()
    36. self.window.setGeometry(50, 50, 350, 250)
    37. self.window.setWindowTitle("Create a team")
    38. self.CreateUserForm()
    39.  
    40.  
    41. def CreateUserForm(self):
    42. self.layout = QFormLayout()
    43.  
    44. ## creates a form (age is spinbox to make sure user can only enter numbers)
    45. self.layout.addRow(QLabel("*Username:"), QLineEdit())
    46. self.layout.addRow(QLabel("*Team name:"), QLineEdit())
    47. self.layout.addRow(QLabel("*First name:"), QLineEdit())
    48. self.layout.addRow(QLabel("*Last name:"), QLineEdit())
    49. self.layout.addRow(QLabel("*Password:"), QLineEdit())
    50. self.layout.addRow(QLabel("Email address:"), QLineEdit())
    51. self.layout.addRow(QLabel("Age:"), QSpinBox())
    52. self.layout.addRow(QLabel("Those with a * must be filled in."))
    53.  
    54. self.OKButton = QPushButton("OK")
    55. self.layout.addWidget(self.OKButton)
    56.  
    57. self.CancelButton = QPushButton("Cancel")
    58. self.layout.addWidget(self.CancelButton)
    59.  
    60. self.window.setLayout(self.layout)
    61. self.window.show()
    62.  
    63.  
    64. if __name__ == '__main__':
    65. import sys
    66.  
    67. app = QApplication(sys.argv)
    68. main = StartScreen()
    69.  
    70.  
    71. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 
    Last edited by tchachkii; 5th December 2018 at 11:35.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: PyQt5 code not working

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 2
    Last Post: 16th April 2015, 23:34
  2. Replies: 0
    Last Post: 11th April 2014, 16:01
  3. Replies: 1
    Last Post: 28th March 2014, 03:53
  4. Replies: 2
    Last Post: 2nd August 2011, 16:05
  5. Change Qt version and code stop working
    By rmagro in forum Qt Programming
    Replies: 6
    Last Post: 28th September 2010, 21:02

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.