pyqt5学习之QMainwindow
QmMainwindow:用来创建窗口,窗口可以包含菜单栏,工具栏,状态栏,标题栏
设置状态栏
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import sys from PyQt5.QtWidgets import QMainWindow, QApplication from PyQt5.QtGui import QIcon class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() self.resize(400, 200) self.status = self.statusBar() # 创建状态栏 self.status.showMessage('这是状态栏提示', 5000) # 在左下方 #showMessage(message,int timeout = 0(mes)) self.setWindowTitle('QMainWindow') if __name__ == '__main__': app = QApplication(sys.argv) app.setWindowIcon(QIcon('dp.png')) # 给标题栏添加一个图片 form = MainWindow() form.show() sys.exit(app.exec_())
设置屏幕居中
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import sys from PyQt5.QtWidgets import QMainWindow, QApplication,QDesktopWidget from PyQt5.QtGui import QIcon # QDesktopWidget:描述显示屏的类 class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() self.setWindowTitle('主窗口放在屏幕中间') self.resize(370, 250) self.center() def center(self): screen = QDesktopWidget().screenGeometry() # 获得屏幕的大小 size = self.geometry() # 获得QMainwindow窗口的大小 self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2) if __name__ == '__main__': app = QApplication(sys.argv) app.setWindowIcon(QIcon('dp.png')) # 给标题栏添加一个图片 form = MainWindow() form.show() sys.exit(app.exec_())
关闭窗口
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import sys from PyQt5.QtWidgets import QMainWindow, QApplication,QHBoxLayout,QPushButton,QWidget from PyQt5.QtGui import QIcon # QDesktopWidget:描述显示屏的类 class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() self.setWindowTitle('关闭主窗口') self.btn = QPushButton('按钮') self.btn.clicked.connect(self.onbuttonclick) layout = QHBoxLayout() layout.addWidget(self.btn) main_frame = QWidget() main_frame.setLayout(layout) self.setCentralWidget(main_frame) def onbuttonclick(self): sender = self.sender() # sender是发送信号的对象,此处发送信号对象是按钮 print(sender.text() + '被按下了') qApp = QApplication.instance() # instance()获得当前的实例 qApp.quit() # 关闭窗口 if __name__ == '__main__': app = QApplication(sys.argv) app.setWindowIcon(QIcon('dp.png')) # 给标题栏添加一个图片 form = MainWindow() form.show() sys.exit(app.exec_())