python pyqt5弹出子窗体

-- 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
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog
from PyQt5.QtGui import QPainter,QPixmap

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"))

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"))

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_())

你可能感兴趣的:(python pyqt5弹出子窗体)