[pyqt5 interface tool development-7] window development - menu bar window QMainWindow

Table of contents

0x00 Preface:

1. Call the menu of the parent class

2. Add options in the menu


0x00 Preface:

QWedget

The parent class of controls and windows, with a high degree of freedom (nothing), no division of menus, toolbars, status bars, main windows and other areas

QMainWindow

It is a subclass of QWwidget, including menu bar, toolbar, status bar, title bar, etc., and the middle part is the main window area

QDialog

Base class for dialog windows



1. Call the menu of the parent class

The same structure as the previous Qwedget (not too much introduction)

You can refer to the comments in the code

import sys
from PyQt5.QtWidgets import QApplication,  QLabel, QMainWindow


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        lable = QLabel("文字内容")          # 最后会设置相关的显示方法
        lable.setStyleSheet("font-size:30px;color:red")

        # 调用父类的menuBar,对菜单栏进行操作
        menu = self.menuBar()
        # mac需要额外配置像windows一样显示菜单栏
        # menu.setNativeMenuBar(False)

"""
————————————————————————

中间将添加具体的菜单选项

————————————————————————
"""


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow()
    w.setWindowTitle("QMainWindow")
    w.show()
    sys.exit(app.exec_())



2. Add options in the menu

Add some submenu options inside

import sys
from PyQt5.QtWidgets import QApplication,  QLabel, QMainWindow


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        lable = QLabel("文字内容")          # 最后会设置相关的显示方法
        lable.setStyleSheet("font-size:30px;color:red")

        # 调用父类的menuBar,对菜单栏进行操作
        menu = self.menuBar()
        # mac需要额外配置像windows一样显示菜单栏
        # menu.setNativeMenuBar(False)


#——————————————————————————————————
# 如下为菜单中添加相关内容

        # 向菜单栏添加内容1
        file_menu = menu.addMenu("文件")
        # 内容子键的添加
        file_menu.addAction("新建")
        file_menu.addAction("打开")
        file_menu.addAction("保存")


        # 向菜单栏添加内容2
        edit_menu = menu.addMenu("编辑")
        # 内容子键的添加
        edit_menu.addAction("剪切")
        edit_menu.addAction("复制")
        edit_menu.addAction("粘贴")


# ——————————————————————————————————


        # 设置内容的显示
        self.setCentralWidget(lable)



if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow()
    w.setWindowTitle("QMainWindow")
    w.show()
    sys.exit(app.exec_())

The running effect is shown in the figure below:

Guess you like

Origin blog.csdn.net/qq_53079406/article/details/132555882