代码之家  ›  专栏  ›  技术社区  ›  Daniel

为什么不添加菜单?

  •  4
  • Daniel  · 技术社区  · 14 年前

    我显然错过了一些东西;为什么不在这个小示例应用程序中添加“文件”菜单?

    import sys
    from PySide.QtGui import *
    
    class Window(QMainWindow):
        def __init__(self):
            super(Window, self).__init__()
            self.setWindowTitle('Test')
            layout = QHBoxLayout()
            self.widget = QWidget()
            self.widget.setLayout(layout)
            self.setCentralWidget(self.widget)
            self.exitAction = QAction('Exit', self, shortcut=QKeySequence.Quit, triggered=self.close)
            self.fileMenu = self.menuBar().addMenu('File')
            self.fileMenu.addAction(self.exitAction)
    
    app = QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())
    

    编辑:

    好的,看起来这实际上是一个unicode问题。 下面是另一个应用程序示例:

    from __future__ import unicode_literals, print_function, division
    import sys
    from PySide.QtCore import *
    from PySide.QtGui import *
    
    class Window(QMainWindow):
        def __init__(self):
            super(Window, self).__init__()
            self.dummyAction = QAction(self.tr('dummy'), self, triggered=self.dummy)
            self.gameMenu = self.menuBar().addMenu(self.tr('ddddummy'))
            print (self.tr('dummy'))
            self.gameMenu.addAction(self.dummyAction)
            layout = QHBoxLayout()
            self.widget = QWidget()
            self.widget.setLayout(layout)
            self.setCentralWidget(self.widget)
    
        def dummy(self):
            pass
    
    locale = QLocale.system().name()
    qtTranslator = QTranslator()
    app = QApplication(sys.argv)
    if qtTranslator.load('qt_' + locale, ':/'):
        app.installTranslator(qtTranslator)
    w = Window()
    w.show()
    sys.exit(app.exec_())
    

    此应用程序没有“文件”、“退出”或“退出”,但它 作品 如果我评论 from __future__ 行,或围绕引用的字符串,如 self.tr(str('foo')) 而不是 self.tr('foo')

    编辑2:

    from __future__ import unicode_literals
    import sys
    from PySide.QtGui import *
    
    class Window(QMainWindow):
        def __init__(self):
            super(Window, self).__init__()
            print self.tr('foo')
    
    app = QApplication(sys.argv)
    Window().show()
    sys.exit(app.exec_())
    

    这应该打印“foo”,但不打印任何内容。

    2 回复  |  直到 13 年前
        1
  •  6
  •   jdi    14 年前

    乍一看,您的代码似乎非常正常,而且它在windows或linux上的功能与预期的一样。这里的问题是,在OSX上,操作系统在菜单上强制使用标准界面。而在其他操作系统上,菜单嵌套在你的应用程序下,你的应用拥有它……在OSX上,操作系统拥有它。因此,它显示在全局菜单区域。

    话虽如此,OSX正在过滤掉一些保留的关键字,如“退出”或“退出”。原因是退出功能是自动放置在应用程序菜单中的标准功能。当您将其作为基本的python脚本运行时,菜单将被称为“python”。但如果你将其捆绑到一个应用程序中,它将根据你的捆绑应用程序进行相应的命名。

    link here ,虽然不是一个确切的解释,但确实提到了OSX上菜单的差异。

    关于修复菜单的快速示例,请查看执行以下操作时会发生什么:

        self.exitAction = QAction('Kwit', self)
    

    OSX不会过滤掉那个。但我认为最好遵循本地标准,使平台上的所有应用程序体验相同。你肯定会像现在这样包含“退出”菜单操作,这样如果你的应用程序在linux或windows上运行,它将是跨平台的,只需要OSX会为你重新定位它。

        2
  •  0
  •   kalefranz    13 年前

    我遇到这条线索是因为我正在努力解决类似的问题。以下是我的发现。。。

    关于您的编辑2: 如果替换行,代码将正确打印“foo”

    Window().show()
    

    对于线路

    w = Window()
    w.show()
    

    正如您在原始代码中所做的那样。显然,构造函数上的返回类型导致链接成为python中的一个问题?

    我能够通过评论出 from __future__ 线否则,下面的代码在OSX(Mountain Lion 10.8.3 with brewing python)中可以正常工作。具体来说,下面的代码将“About”操作放在OS X创建的“Python”应用程序菜单下,还创建了一个包含“Website”操作的“Help”菜单。

    import sys
    from PySide.QtGui import QApplication,QMainWindow, QWidget, QAction
    
    class MainWindow(QMainWindow):
        def __init__(self, parent=None):
            super(MainWindow, self).__init__(parent)
            self.create_menus()
            self.create_main_frame()
    
        def create_menus(self):
            self.aboutAction = QAction('About', self, triggered=self.on_about)
            self.websiteAction = QAction('Website', self, triggered=self.on_website)
            self.help_menu = self.menuBar().addMenu('Help')
            self.help_menu.addAction(self.aboutAction)
            self.help_menu.addAction(self.websiteAction)
    
        def create_main_frame(self):
            self.mainWidget = QWidget()
            self.setCentralWidget(self.mainWidget)
    
        def on_website(self):
            pass
    
        def on_about(self):
            pass
    
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())
    

    我应该提请大家注意这条线索帮助我发现的一个重要问题。我看到了很多关于OSX的建议,这些建议表明您应该创建 menu_bar = QMenuBar() 独立于 QMainWindow 然后与绑定 self.setMenuBar(menu_bar) 哪里 self 代表 Q主窗口 。事实上,这对我来说并不起作用。相反,起作用的是直接从QMainWindow类本身获取菜单栏引用。例如,和上面一样,在添加菜单时,使用 self.help_menu = self.menuBar().addMenu('Help') 如上所述。