【问题标题】:I need help making a menu bar in PyQt5我需要帮助在 PyQt5 中制作菜单栏
【发布时间】:2014-01-24 13:56:58
【问题描述】:

几天来,我一直在尝试在我的程序中实现一个菜单栏,但我似乎无法运行它。我希望有人查看我的代码并给我一个模板来制作菜单栏。

class MainWindow(QMainWindow):
    def __init__(self, databaseFilePath, userFilePath):
        super(MainWindow,self).__init__()
        self.moviesFilePath = moviesFilePath
        self.currentUserFilePath = currentUserFilePath
        self.createWindow()

    def changeFilePath(self):
        self.currentUserFilePath = functions_classes.changeFP()
        functions_classes.storeFP(self.currentUserFilePath, 1)

    def createWindow(self):
        self.setWindowTitle('Movies')
        #Menu Bar
        fileMenuBar = QMenuBar().addMenu('File')

方法 changeFilePath 是我希望在从菜单栏 File 调用名为“更改用户数据库位置”的菜单选项时调用的方法。我已经读过行动是关键,但是当我尝试实施它们时,它们都没有奏效。

【问题讨论】:

    标签: python pyqt pyqt5


    【解决方案1】:

    QMainWindow 类已经有一个menu-bar

    所以你只需要add a menu 到它,然后add an action 到那个菜单,就像这样:

        def createUI(self):
            ...
            menu = self.menuBar().addMenu('File')
            action = menu.addAction('Change File Path')
            action.triggered.connect(self.changeFilePath)
    

    编辑

    这是一个基于您的示例类的完整工作示例:

    from PyQt5 import QtWidgets
    
    class MainWindow(QtWidgets.QMainWindow):
        def __init__(self, databaseFilePath, userFilePath):
            super(MainWindow,self).__init__()
            self.databaseFilePath = databaseFilePath
            self.userFilePath = userFilePath
            self.createUI()
    
        def changeFilePath(self):
            print('changeFilePath')
            # self.userFilePath = functions_classes.changeFilePath()
            # functions_classes.storeFilePath(self.userFilePath, 1)
    
        def createUI(self):
            self.setWindowTitle('Equipment Manager 0.3')
            menu = self.menuBar().addMenu('File')
            action = menu.addAction('Change File Path')
            action.triggered.connect(self.changeFilePath)   
    
    if __name__ == '__main__':
    
        import sys
        app = QtWidgets.QApplication(sys.argv)
        window = MainWindow('some/path', 'some/other/path')
        window.show()
        window.setGeometry(500, 300, 300, 300)
        sys.exit(app.exec_())
    

    【讨论】:

    • 对于所提供的两种方法,当代码运行时,python shell 看起来像是在尝试构建并提升窗口,但随后又重新启动。
    • @user1921942。我不太明白你的评论,但我添加了一个工作示例来展示如何运行代码。
    • 我不知道我第一次做错了什么,但在你的编辑之后我得到了它的工作谢谢!
    【解决方案2】:

    添加带有可用项目的菜单栏的逻辑是这样的

    def createUI(self):
            self.setWindowTitle('Equipment Manager 0.3')
            #Menu Bar
            fileMenuBar = QMenuBar(self)
            menuFile = QMenu(fileMenuBar)
            actionChangePath = QAction(tr("Change Path"), self)
            fileMenuBar.addMenu(menuFile)
            menuFile.addAction(actionChangePath)
    

    然后你只需要将动作actionChangePath 连接到信号triggered() 就像

    connect(actionChangePath,SIGNAL("triggered()"), changeFilePath)
    

    可能有一些更好的解决方案(但你为什么不使用设计器?),但是这个应该可以工作

    【讨论】:

    • 对于所提供的两种方法,当代码运行时,python shell 看起来像是在尝试构建并提升窗口,但随后又重新启动。
    • 我没有使用设计器的原因是因为我不知道如何将文件转换为GUI程序。
    • 使用pyuic转换文件,生成一个.py文件,可以导入到你的主脚本中
    猜你喜欢
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 2020-06-10
    • 2017-07-09
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    相关资源
    最近更新 更多