本文主要是介绍PyQt5 窗口继承QMainWindow和继承QWidget的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
主窗口
- 继承QMainWindow
- 继承QWidget
- 主窗口居中显示
- 关闭主窗口
继承QMainWindow
- mainwidget = QWidget()
- mainwidget.setLayout(mainlayout) # 设置布局
- self.setCentralWidget(mainwidget) # 设置中心部件,作为主界面窗口
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QHBoxLayout, QPushButton
from PyQt5.QtGui import QIcon# 继承QMainWindow
class MainWindow(QMainWindow):def __init__(self, parent=None):super(MainWindow, self).__init__(parent)self.initUI()def initUI(self):self.resize(400, 300)self.status = self.statusBar()self.status.showMessage("这里是状态栏显示", 5000) # 状态栏显示 文本 5秒self.setWindowTitle("继承QMainWindow窗口") # 设置窗口标题# self.setWindowIcon(QIcon("./pyqt5/close.png")) # 设置窗口图标# 创建主界面布局 并 添加两个按钮mainlayout = QHBoxLayout()confirmBtn = QPushButton("确认")cancelBtn = QPushButton("取消")confirmBtn.clicked.connect(self.onConfirmBtnClick)cancelBtn.clicked.connect(self.onCancelBtnClick)mainlayout.addWidget(confirmBtn)mainlayout.addWidget(cancelBtn)# 创建主界面窗口并设置为中心窗口mainwidget = QWidget()mainwidget.setLayout(mainlayout)self.setCentralWidget(mainwidget)def onConfirmBtnClick(self):print('确认按钮按下')def onCancelBtnClick(self):print("取消按钮按下")if __name__ == "__main__":app = QApplication(sys.argv)app.setWindowIcon(QIcon("./pyqt5/close.png")) # 设置窗口图标win = MainWindow()win.show()sys.exit(app.exec_())
继承QWidget
继承QWidget调用self.setLayout(mainlayout)添加布局即可向主界面添加控件。
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton# 继承QWidget
class MyWindow(QWidget):def __init__(self, parent=None):super(MyWindow, self).__init__(parent)self.initUI()def initUI(self):self.setGeometry(100, 100, 350, 250)self.setWindowTitle("继承QWidget窗口")mainlayout = QHBoxLayout()mainlayout.addWidget(QPushButton("开始"))mainlayout.addWidget(QPushButton("停止"))self.setLayout(mainlayout)if __name__ == "__main__":app = QApplication(sys.argv)win = MyWindow()win.show()sys.exit(app.exec_())
主窗口居中显示
import sys
from PyQt5.QtWidgets import QDesktopWidget, QMainWindow, QApplicationclass MainWindow(QMainWindow):def __init__(self, parent=None):super(MainWindow, self).__init__(parent)self.setWindowTitle("主窗口居中显示实例")self.resize(800, 480)self.center()def center(self):screen = QDesktopWidget().screenGeometry()size = self.geometry()self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2)if __name__ == "__main__":app = QApplication(sys.argv)win = MainWindow()win.show()sys.exit(app.exec_())
关闭主窗口
import sys
from PyQt5.QtWidgets import QDesktopWidget, QMainWindow, QApplication, QPushButton, QHBoxLayout, QVBoxLayout, QWidget, QToolTip
from PyQt5.QtGui import QIcon, QPixmapclass WinFrom(QMainWindow):def __init__(self, parent=None):super(WinFrom, self).__init__(parent)self.setWindowTitle("关闭主窗口例子")self.setWindowIcon(QIcon("./pyqt5/close.png"))self.setFixedWidth(800)self.setFixedHeight(480)self.btn_close = QPushButton("关闭主窗口")self.btn_close.clicked.connect(self.onButtonClick)self.btn_close.setToolTip('关闭窗口<b>操作</b>') self.setToolTip('<b>这里是窗口</b>') layout = QHBoxLayout()layout.addWidget(self.btn_close)main_frame = QWidget()main_frame.setLayout(layout)self.setCentralWidget(main_frame) def onButtonClick(self):sender = self.sender()print(sender.text() + ' 被单击')qApp = QApplication.instance()qApp.quit()if __name__ == "__main__":app = QApplication(sys.argv)win = WinFrom()win.show()sys.exit(app.exec_())
这篇关于PyQt5 窗口继承QMainWindow和继承QWidget的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!