(Python)PyQt5菜单栏的换行符

时间:2017-08-18 14:15:24

标签: python menu pyqt pyqt5

PyQt5菜单栏需要帮助:

我刚开始进入PyQt5,我正在为GUI应用程序创建一个菜单栏。下面是我目前为菜单制作的代码:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, qApp


class MenuDemo(QMainWindow):
    def __init__(self):
        super().__init__()

####################################################

        # This Creates the initial Menu Bar
        bar = self.menuBar()

        # This creates the Menu Bar tabs & names them
        file = bar.addMenu('File')
        edit = bar.addMenu('Edit')
        helpme = bar.addMenu('Help')

        # This creates the actions for 'File' section
        new_action = QAction('New File', self)
        new_action.setShortcut('Ctrl+N')

        open_action = QAction('Open File', self)
        open_action.setShortcut('Ctrl+O')

        save_action = QAction('Save', self)
        save_action.setShortcut('Ctrl+S')

        save_as_action = QAction('Save as...', self)
        save_as_action.setShortcut('Ctrl+Shift+S')

        quit_action = QAction('Quit', self)
        quit_action.setShortcut('Ctrl+Q')

        # This creates the actions for 'Edit' section
        undo_action = QAction('Undo', self)
        undo_action.setShortcut('Ctrl+Z')

        redo_action = QAction('Redo', self)
        redo_action.setShortcut('Ctrl+Y')

        # This creates the actions for "Help" section
        doc_action = QAction('Documentation', self)
        # No Shortcut Needed

        about_action = QAction('About', self)
        # No Shortcut Needed

####################################################

        # This adds actions to Menu Bar under 'File'  Tab
        file.addAction(new_action)
        file.addAction(open_action)
        file.addAction(save_action)
        file.addAction(save_as_action)
        file.addAction(quit_action)

        # This adds actions to Menu Bar under 'Edit'  Tab
        edit.addAction(undo_action)
        edit.addAction(redo_action)

        # This adds actions to Menu Bar under 'Help' Tab
        helpme.addAction(doc_action)
        helpme.addAction(about_action)

        # Events

        # This Sets the initial window size and title
        self.setWindowTitle('My Menu')
        self.resize(750, 500)

        self.show()

####################################################

def quit_trigger(self):
    pass


def selected(self, q):
    pass


app = QApplication(sys.argv)
menus = MenuDemo()
sys.exit(app.exec_())

问题:

我显示了菜单栏,一切似乎都运行良好,但有一件事让我烦恼。在菜单的“文件”选项卡下,有一个名为“退出”的选项。在“退出”选项之上,我需要某种换行符来从其他选项中分离出该选项。在Tkinter中,这是一项简单的任务,但我似乎无法找到如何使用PyQt5。非常感谢任何帮助!

P.S:菜单项目前只是按钮,它们还没有做任何事情。

1 个答案:

答案 0 :(得分:2)

QMenu的

addSeparator方法仅适用于那种场合:

# ...
file.addAction(save_as_action)
file.addSeparator()
file.addAction(quit_action)
# ...