1. 用qt designer编写主窗体,窗体类型是MainWindow,空白窗口上一个按钮。并转换成mainWindow.py

# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'f.ui'
#
# 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.setObjectName("MainWindow")
    MainWindow.resize(800, 600)
    self.centralwidget = QtWidgets.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")
    self.pushButton = QtWidgets.QPushButton(self.centralwidget)
    self.pushButton.setGeometry(QtCore.QRect(80, 90, 75, 23))
    self.pushButton.setObjectName("pushButton")
    MainWindow.setCentralWidget(self.centralwidget)
    self.retranslateUi(MainWindow)
    QtCore.QMetaObject.connectSlotsByName(MainWindow)
  def retranslateUi(self, MainWindow):
    _translate = QtCore.QCoreApplication.translate
    MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    self.pushButton.setText(_translate("MainWindow", "PushButton"))

2. 用qt designer编写子窗体,窗体类型是Dialog, 空白窗口上一个按钮。并转换成childWindow.py

from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
  def setupUi(self, Dialog):
    Dialog.setObjectName("Dialog")
    Dialog.resize(400, 300)
    self.pushButton = QtWidgets.QPushButton(Dialog)
    self.pushButton.setGeometry(QtCore.QRect(160, 100, 75, 23))
    self.pushButton.setObjectName("pushButton")
    Dialog.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)  #设置窗体总显示在最上面
    self.retranslateUi(Dialog)
    QtCore.QMetaObject.connectSlotsByName(Dialog)
  def retranslateUi(self, Dialog):
    _translate = QtCore.QCoreApplication.translate
    Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
    self.pushButton.setText(_translate("Dialog", "PushButton"))

3,编写调用程序,这个重点,

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog
from dust.mainWindow import *
from dust.childWindow import *

if __name__ == '__main__':
  app = QApplication(sys.argv)
  #实例化主窗口 
  main = QMainWindow() 
  main_ui = Ui_MainWindow()
  main_ui.setupUi(main )
  #实例化子窗口 
  child = QDialog()      
  child_ui = Ui_Dialog()
  child_ui.setupUi(child)
  
  #按钮绑定事件
  btn = main_ui.pushButton
  btn.clicked.connect( child.show ) 
  
  #显示
  main.show()
  sys.exit(app.exec_())

4. 上面的程序只是能显示了,要想添加自定义事件,还不行,加自定义事件,有一个办法是再封装一个类,主窗体和子窗体都如此。

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog
from dust.mainWindow import *
from dust.childWindow import *

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

class ChildWindow(QDialog, Ui_Dialog):
  def __init__(self):
    super(ChildWindow,self).__init__()
    self.setupUi(self)
    
    self.setWindowTitle('child window')
    
    self.pushButton.clicked.connect( self.btnClick) #按钮事件绑定

  def btnClick(self): #子窗体自定义事件
    self.close()

if __name__ == '__main__':
  app = QApplication(sys.argv)
   
  main = MyMainWindow()
   
  child = ChildWindow()  
  
  btn = main.pushButton    #主窗体按钮事件绑定
  btn.clicked.connect( child.show ) 
  
  main.show()
  sys.exit(app.exec_())

以上这篇pyqt5对用qt designer设计的窗体实现弹出子窗口的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?