PythonProjects/QT/PyQt/3. QMainWindow主要内容.py
2024-03-01 14:06:22 +08:00

55 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!D:\Python311\python.exe
# -*- coding: utf-8 -*-
# @Time : 2024/2/26 14:01
# @Author : DC_DC
"""
QMainWindow是QT框架中的一个类用于创建主窗口应用程序提供一个具有一般应用程序框架的主窗口包括菜单栏、工具栏、状态栏和中央工作区域
使用这个类可以管理主窗口
1. 主窗口标题
2. 主窗口标签
3. 菜单栏
4. 工具栏
5. 状态栏
"""
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
class MyMainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 设置主窗口标题
self.setWindowTitle('My Main Window')
# 添加标签到中央工作区域
central_widget = QLabel("Hello, QMainWindow!")
self.setCentralWidget(central_widget)
# 添加菜单栏
menubar = self.menuBar()
file_menu = menubar.addMenu('File')
file_menu.addAction('Open')
file_menu.addAction('Save')
# 添加工具栏
toolbar = self.addToolBar('Tools')
toolbar.addAction('Cut')
toolbar.addAction('Copy')
toolbar.addAction('Paste')
# 添加状态栏
statusbar = self.statusBar()
statusbar.showMessage('Ready')
if __name__ == '__main__':
app = QApplication([]) # 这种写法和下面的写法一致
"""
为什么要实例化这个类呢?
1. 管理应用程序的事件循环
2. 管理应用程序的全局状态
3处理命令行参数
记住创建一个QApplication对象是整个PyQT5应用程序的入口点
"""
# app = QApplication(sys.argv if sys.argv else [])
window = MyMainWindow()
window.show()
app.exec_()