为什么不添加菜单? [英] Why doesn't menu get added?

查看:61
本文介绍了为什么不添加菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我显然在这里遗漏了一些东西;为什么在这个小示例应用中没有添加文件菜单?

I'm clearly missing something here; why doesn't the File menu get added in this little example app?

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 问题.这是另一个示例应用:

Ok, it looks like this is actually a unicode issue. Here's another example app:

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')

This app doesn't have 'File' or 'Quit' or 'Exit' -- but it works if I comment out the from __future__ line, or surround the quoted strings like self.tr(str('foo')) instead of 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',但什么也不打印.

This should print 'foo', but prints nothing.

推荐答案

乍一看,您的代码似乎完全正常,并且在 windows 或 linux 上确实可以正常运行.这里的问题是在 OSX 上,操作系统在菜单上强制执行标准界面.而在其他操作系统上,菜单嵌套在您的应用程序下方,并且您的应用程序拥有它……在 OSX 上,操作系统拥有它.因此它显示在全局菜单区域中.

At first glance, your code seems perfectly normal, and it does function just as expected on windows or linux. The issue here is that on OSX, the operating system enforces a standard interface on the menu. Whereas on other operating systems the menu is nested right under your app, and your app owns it...on OSX, the operating system owns it. Hence it is shown in the global menu area.

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

That being said, OSX is filtering out some reserved keywords like "Quit" or "Exit". The reason for this is because the quit functionality is a standard that is automatically placed in your Application menu. When you run it as a basic python script, the menu will be called "Python". But if you bundle it into an app, it will be named accordingly for your bundled app.

此处的链接虽然不是确切的解释,但确实提到了OSX 上菜单的差异.

This link here, while not an exact explanation, does mention the differences for a menu on OSX.

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

For a quick example of fixing your menu, see what happens when you do:

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

OSX 不会过滤掉那个.但我认为最好遵循原生标准,使平台上的所有应用程序体验相同.您肯定会像现在一样包含退出"菜单操作,这样如果在 linux 或 Windows 上运行您的应用程序将是跨平台的,并且只期望 OSX 会为您重新定位它.

OSX will not filter out that one. But I suppose its better to follow the native standards which make all app experiences the same on the platform. You would definitely include the "Quit" menu action as you have it now, so that your app will be cross-platform if run on linux or windows and just expect that OSX will relocate it for you.

这篇关于为什么不添加菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆