SoFunction
Updated on 2024-12-20

The pyqt5 method of jumping to an interface using buttons

summary

Carry out the button for the interface to jump, I'm here I introduce two kinds, one is not using the Qtdesigner code, the other is to use the Qtdesigner code

Code 1

import sys
from  import QMainWindow, QPushButton, QApplication
 
 
class First(QMainWindow):
 
  def __init__(self):
    super().__init__()
 
    ()
 
  def initUI(self):
     = QPushButton("Button", self)
    (30, 50)
    
 
 
    (300, 300, 500, 500)
    ('Event sender')
    ()
 
class Second(QMainWindow):
  def __init__(self):
    super().__init__()
 
    ()
 
  def initUI(self):
    (300, 300, 290, 150)
    ('Get sender')
 
 
if __name__ == '__main__':
  app = QApplication()
  a = First()
  b = Second()
  ()
  ()
  (app.exec_())

Code 2

The following code uses the Qtdesigner code

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from  import QApplication, QMainWindow, QWidget, QLabel,\
                     QPushButton, QLineEdit, QMenuBar, QStatusBar
from  import *
 
class FirstWindow(QWidget):
 
  def __init__(self, parent=None):
    # super This usage calls the constructor of the parent class.
    # parent=None means there is no parent widget by default, if you specify a parent widget, then call it.
    super(FirstWindow, self).__init__(parent)
    (500, 500, 500, 500)
    ('Display')
 
     = QPushButton(self)
    ('Calibration')
    (150, 50)
 
 
# The following section is the code for Qtdesigner
 
class Ui_MainWindow(object):
  def setupUi(self, MainWindow):
    ("MainWindow")
    (624, 479)
     = (MainWindow)
    ("centralwidget")
     = ()
    ((190, 130, 51, 31))
    ("label")
     = ()
    ((330, 260, 75, 23))
    ("pushButton")
     = ()
    ((280, 130, 151, 21))
    ("lineEdit")
    self.label_2 = ()
    self.label_2.setGeometry((190, 180, 51, 31))
    self.label_2.setObjectName("label_2")
    self.lineEdit_2 = ()
    self.lineEdit_2.setGeometry((280, 190, 151, 21))
    self.lineEdit_2.setObjectName("lineEdit_2")
    self.pushButton_2 = ()
    self.pushButton_2.setGeometry((200, 260, 75, 23))
    self.pushButton_2.setObjectName("pushButton_2")
    ()
     = (MainWindow)
    ((0, 0, 624, 23))
    ("menubar")
    ()
     = (MainWindow)
    ("statusbar")
    ()
 
    (MainWindow)
    (MainWindow)
 
  def retranslateUi(self, MainWindow):
    _translate = 
    (_translate("MainWindow", "MainWindow"))
    (_translate("MainWindow", " Long"))
    (_translate("MainWindow", "OK."))
    self.label_2.setText(_translate("MainWindow", " Broad."))
    self.pushButton_2.setText(_translate("MainWindow", "Cancel."))
 
if __name__ == "__main__":
  app = QApplication()
  MainWindow = QMainWindow()
  ui = Ui_MainWindow()
  (MainWindow)
 
  ex = FirstWindow()
  ()
  ()
 
  (app.exec_())
 

The above this pyqt5 using buttons for interface jump method is all I have shared with you, I hope to give you a reference, and I hope you support me more.