Python/Tkinter/Cython/PyInstaller:MacOS 中菜单栏不可单击

问题描述 投票:0回答:1

当我从 MacOS 终端使用 Python 解释器运行以下测试程序时,它按预期工作。当我 Cython 它并使用 PyInstaller 创建可执行文件,并从终端运行它时,应用程序菜单栏变得不可单击。在 Windows 上使用相同进程创建的等效程序也可以正常运行。我正在使用 Python 3.8.2、Cython 3.0.0、PyInstaller 5.13.0。

无论我创建单文件还是多文件可执行文件,都会发生此问题;当作为 .app 文件运行时,菜单甚至不存在(也就是说,我看到 Finder 菜单)。

作为参考,我使用了本文引用的文档中建议的一些技术: Tkinter 菜单栏不显示在 Mac OS 上

有什么想法吗?谢谢。

更新:看来,如果我从该应用程序中单击到屏幕的另一部分,然后单击返回,菜单就会启用。不过我认为它们应该从一开始就启用。使用 .app 文件时菜单根本不出现的问题尚未解决。

import tkinter as tk
from tkinter import *
import sys
import tkinter.messagebox as mb
import os.path
    
def show_dialog(e=None, title='Just some dialog'):
    dlog = tk.Toplevel(root)
    dlog.geometry('300x75')
    dlog.title(title)
    dlog.bind('<Escape>', lambda e:dlog.destroy())
    dlog.focus_set()

def showinfo(e=None):
    my_path = os.path.dirname(os.path.abspath(__file__))
    mb.showinfo('Test', my_path)

def create_menus():
    menu_bar = Menu(root)
    appmenu = Menu(menu_bar, name="apple")
    menu_bar.add_cascade(menu=appmenu)
    appmenu.add_command(label="About Me", command=showinfo)
    appmenu.add_separator()
    global file_menu
    global edit_menu
    
    file_menu = Menu(menu_bar, tearoff=0)
    edit_menu = Menu(menu_bar, tearoff=0)
    window_menu = Menu(menu_bar, name="window")
    menu_bar.add_cascade(menu=window_menu, label="Window")
    
    file_menu.add_command(
        label='Open...',
        command=lambda:show_dialog(title='Open'),
        accelerator="Command+O"
    )
    
    file_menu.add_command(
        label='Exit',
        command=sys.exit,
        accelerator="Command+Q"
    )
    
    menu_bar.add_cascade(
        label="File",
        menu=file_menu,
        underline=0
    )
    
    edit_menu.add_command(
        label='Find...',
        command=lambda:show_dialog(title='Find'),
        accelerator="Command+F"
    )
    
    menu_bar.add_cascade(
        label="Edit",
        menu=edit_menu,
        underline=0
    )
    
    root['menu'] = menu_bar

def main():
    global root 
    root = tk.Tk()
    root.title("Simple Editor")
   
    create_menus()
    
    input_text = tk.Text(root, height=10, width=30 )
    input_text.pack(expand=True, fill=tk.BOTH)
   
    root.bind('<Command-o>', lambda e:show_dialog(title='Open'))
    root.bind('<Command-f>', lambda e:show_dialog(title='Find'))
    root.bind('<Command-q>', lambda e:sys.exit)
    root.bind('<Command-i>', showinfo)
    root.mainloop()

if __name__ == "__main__":
    main()
python macos tkinter pyinstaller cython
1个回答
0
投票

解决了运行.app时菜单不出现的问题。 PyInstaller 生成的 Info.plist 文件的

LSBackgroundOnly
键设置为
true
;将其重置为
false
使菜单可用。

© www.soinside.com 2019 - 2024. All rights reserved.