您好,登錄后才能下訂單哦!
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設計的窗體實現彈出子窗口的示例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。