SoFunction
Updated on 2024-12-20

Example of pyqt5's implementation of pop-up sub-windows for forms designed with qt designer.

1. Use qt designer to write the main form with form type MainWindow and a button on a blank window. And convert

# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file ''
#
# Created by: PyQt5 UI code generator 5.9
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
  def setupUi(self, MainWindow):
    ("MainWindow")
    (800, 600)
     = (MainWindow)
    ("centralwidget")
     = ()
    ((80, 90, 75, 23))
    ("pushButton")
    ()
    (MainWindow)
    (MainWindow)
  def retranslateUi(self, MainWindow):
    _translate = 
    (_translate("MainWindow", "MainWindow"))
    (_translate("MainWindow", "PushButton"))

2. Use qt designer to write a child form, the form type is Dialog, a button on a blank window. And convert it to

from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
  def setupUi(self, Dialog):
    ("Dialog")
    (400, 300)
     = (Dialog)
    ((160, 100, 75, 23))
    ("pushButton")
    ()  #Set the form to always display at the top
    (Dialog)
    (Dialog)
  def retranslateUi(self, Dialog):
    _translate = 
    (_translate("Dialog", "Dialog"))
    (_translate("Dialog", "PushButton"))

3, write the calling program, this focus.

import sys
from  import QApplication, QMainWindow, QDialog
from  import *
from  import *

if __name__ == '__main__':
  app = QApplication()
  # Instantiate the main window
  main = QMainWindow() 
  main_ui = Ui_MainWindow()
  main_ui.setupUi(main )
  # Instantiate child windows
  child = QDialog()      
  child_ui = Ui_Dialog()
  child_ui.setupUi(child)
  
  #Button Binding Events
  btn = main_ui.pushButton
  (  ) 
  
  #Display
  ()
  (app.exec_())

4. The above program is just able to display, to add custom events, not yet, add custom events, there is a way to encapsulate a class, the main form and child forms are so.

import sys
from  import QApplication, QMainWindow, QDialog
from  import *
from  import *

#mainWindow
class MyMainWindow(QMainWindow, Ui_MainWindow):
  def __init__(self):
    super(MyMainWindow,self).__init__()
    (self)
    
    (0, 0, 1024, 600)
    ('main window')
        
    
  def paintEvent(self, event):
    painter = QPainter(self)
    pixmap = QPixmap("./image/")
    ((),pixmap)
  
     
  def keyPressEvent(self, e):
     
    if () == Qt.Key_Escape:
      ()

class ChildWindow(QDialog, Ui_Dialog):
  def __init__(self):
    super(ChildWindow,self).__init__()
    (self)
    
    ('child window')
    
    ( ) # Button event binding

  def btnClick(self): # Subform customization events
    ()

if __name__ == '__main__':
  app = QApplication()
   
  main = MyMainWindow()
   
  child = ChildWindow()  
  
  btn =     # Main form button event binding
  (  ) 
  
  ()
  (app.exec_())

Above this pyqt5 on the use of qt designer design form to achieve the pop-up sub-window example is all I have shared with you, I hope to be able to give you a reference, and I hope that you will support me more.