Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Pyqt5 code not working
Forum Updated to NodeBB v4.3 + New Features

Pyqt5 code not working

Scheduled Pinned Locked Moved Unsolved Qt for Python
4 Posts 2 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tchachkii
    wrote on 5 Dec 2018, 10:35 last edited by
    #1

    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

    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_())
    
    1 Reply Last reply
    0
    • E Offline
      E Offline
      ewerybody
      wrote on 6 Dec 2018, 12:56 last edited by
      #2

      Hi welcome! Pyqt5? This is PySide2/Qt for Python! :D
      but well the code should work anyway:

      First off: When you want a child window you need to pass the parent somehow. I'd make the NewUserPressed a member function of the main window so you have self at hand and then pass the according object to it:

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

      of course you also need to call this like this now:

          self.NewUser.clicked.connect(self.NewUserPressed)
      

      Then you need to accept the parent to your new instance and pass it to the base class init.
      Now when you subclass from QWidget which is what you do with class CreateUser(QWidget) ...: you need to call the init function of the base class as well! Easy:

      class CreateUser(QWidget):
          def __init__(self, parent):
          super(CreateUser, self).__init__(parent)
      
      1 Reply Last reply
      1
      • E Offline
        E Offline
        ewerybody
        wrote on 6 Dec 2018, 13:32 last edited by
        #3

        While this ^ would make your code working already I'd still point to some other issues:

        You start off with QWidget instance already and put another one right in there? Without transforming it? That can be shortened with initializing your base class in the beginning too:

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

        Same goes for the subwindow! But as its a QWidget too Qt will try to embedd it into the current windows composition right away. I'd make it a QDialog!

        class CreateUser(QDialog):
            def __init__(self, parent):
                super(CreateUser, self).__init__(parent)
        

        This can also be Esc-aped away nicely. And has Signals like accepted and rejected 👍

        Now self.layout is a member function of QWidget! So If you do self.layout = QGridLayout() you override the function with a variable pointing to the layout instance. Of course we're in Python and everything can be monkeypatched and overridden this is fine. But better practice is to keep these intact when there is no need to override. And if you just add to the in the layout in this one function you don't even need to make it a member variable:

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

        You can still access the layout later via self.layout() because you did not override :)
        When you pass in self you can even skip the setLayout() later on!

        ... well this is nitpicking but ... 🤷‍♂️

        make use of DRY principle:

            for item in ['*Username', '*Team name', '*First name',
                         '*Last name', '*Password', 'Email address']:
                layout.addRow(QLabel(item + ':'), QLineEdit())
        

        get familiar with pep8 and make variables and functions lower case:

            ...
            self.setup_ui()
            self.show()
        
        def setup_ui(self):
            ...
            self.new_user = QPushButton("New user")
            self.new_user.clicked.connect(self.new_user_pressed)
            layout.addWidget(self.new_user)
        
        def new_user_pressed(self):
             ...
        

        in general: try to make your code snippet as short as possible cooking it down to the very part thats not working.
        cheers:
        eRiC

        T 1 Reply Last reply 11 Dec 2018, 09:16
        1
        • E ewerybody
          6 Dec 2018, 13:32

          While this ^ would make your code working already I'd still point to some other issues:

          You start off with QWidget instance already and put another one right in there? Without transforming it? That can be shortened with initializing your base class in the beginning too:

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

          Same goes for the subwindow! But as its a QWidget too Qt will try to embedd it into the current windows composition right away. I'd make it a QDialog!

          class CreateUser(QDialog):
              def __init__(self, parent):
                  super(CreateUser, self).__init__(parent)
          

          This can also be Esc-aped away nicely. And has Signals like accepted and rejected 👍

          Now self.layout is a member function of QWidget! So If you do self.layout = QGridLayout() you override the function with a variable pointing to the layout instance. Of course we're in Python and everything can be monkeypatched and overridden this is fine. But better practice is to keep these intact when there is no need to override. And if you just add to the in the layout in this one function you don't even need to make it a member variable:

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

          You can still access the layout later via self.layout() because you did not override :)
          When you pass in self you can even skip the setLayout() later on!

          ... well this is nitpicking but ... 🤷‍♂️

          make use of DRY principle:

              for item in ['*Username', '*Team name', '*First name',
                           '*Last name', '*Password', 'Email address']:
                  layout.addRow(QLabel(item + ':'), QLineEdit())
          

          get familiar with pep8 and make variables and functions lower case:

              ...
              self.setup_ui()
              self.show()
          
          def setup_ui(self):
              ...
              self.new_user = QPushButton("New user")
              self.new_user.clicked.connect(self.new_user_pressed)
              layout.addWidget(self.new_user)
          
          def new_user_pressed(self):
               ...
          

          in general: try to make your code snippet as short as possible cooking it down to the very part thats not working.
          cheers:
          eRiC

          T Offline
          T Offline
          tchachkii
          wrote on 11 Dec 2018, 09:16 last edited by
          #4

          @ewerybody thank you so much! apologies for the late reply, I didn't get the notifications. I'll make those changes and see how it works, thank you so much, I would never have been able to make those changes myself

          1 Reply Last reply
          1

          1/4

          5 Dec 2018, 10:35

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved