且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么不添加菜单?

更新时间:2023-11-29 23:12:16

乍一看,您的代码似乎完全正常,并且在 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.