Results 1 to 2 of 2

Thread: PyQt5 code not working

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    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 10:35.

Similar Threads

  1. Replies: 2
    Last Post: 16th April 2015, 22:34
  2. Replies: 0
    Last Post: 11th April 2014, 15:01
  3. Replies: 1
    Last Post: 28th March 2014, 02:53
  4. Replies: 2
    Last Post: 2nd August 2011, 15:05
  5. Change Qt version and code stop working
    By rmagro in forum Qt Programming
    Replies: 6
    Last Post: 28th September 2010, 20: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.