简体   繁体   中英

PyQt, how can I connect button?

I have a login form and I want to connect the button to another window. So here is my code but when I run it, it shows the login window but after I clicked the button the next window only shows up for a moment. I tried multiple times after watching here and there but it didn't work out.

class Login(QMainWindow):
      def __init__(self):
          super().__init__()

          self.setGeometry(750, 350, 400, 225)
          self.setWindowTitle('Login')

          #stuff right here

          self.tombol = QPushButton('Login', self)
          self.tombol.setGeometry(225, 150, 101, 23)
          self.tombol.clicked.connect(Main)
          self.show()

 class Main(QMainWindow):
      def __init__(self):
          super().__init__()

          self.setGeometry(750, 350, 400, 150)
          self.setWindowTitle('Hasil Pilkada')

          #stuff right here too

          self.show()

The problem is simple: Main is created with a limited scope, so a moment later it will be eliminated, so the window is seen in a single moment.

The objective could be rewritten as: when the button is pressed then the other window must be shown, for the user it is indifferent if it is created before the window or not but only shown.

class Login(QMainWindow):
      def __init__(self):
          super().__init__()

          

          self.setGeometry(750, 350, 400, 225)
          self.setWindowTitle('Login')

          #stuff right here

          self.tombol = QPushButton('Login', self)
          self.tombol.setGeometry(225, 150, 101, 23)
          
          self.show()

 class Main(QMainWindow):
      def __init__(self):
          super().__init__()

          self.setGeometry(750, 350, 400, 150)
          self.setWindowTitle('Hasil Pilkada')

          #stuff right here too

          

Another way where if the window is created and shown:

class Login(QMainWindow):
      def __init__(self):
          super().__init__()

          self.setGeometry(750, 350, 400, 225)
          self.setWindowTitle('Login')

          #stuff right here

          self.tombol = QPushButton('Login', self)
          self.tombol.setGeometry(225, 150, 101, 23)
          
          self.show()

    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM