本文介绍了如何替换“关于Tkinter"OSX 上的 Python 菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是 OSX 应用程序菜单,其中包含关于和首选项菜单项(以及其他菜单项).

I am referring to the OSX application menu, which contains the About and Preference menu items (along with others).

对于知道正确搜索词的人来说,这可能是另一个简单的方法,但是在花费数小时在 IDLE 中跟踪代码并在网上搜索之后,我还不太能把这些点联系起来.

Perhaps this will be another easy one for someone who knows the right search terms, but after spending hours tracing code in IDLE and searching the net, I haven't quite been able to connect the dots.

我正在尝试替换 Python 中的标准关于"菜单.IDLE 至少部分地做到了这一点;该菜单仍然命名为关于 Python",但它显示了 IDLE About 窗口.当从 Wing IDE(在 X11 下)运行时, idle 不显示其关于窗口,并且出于某种原因 IDLE 不想调试 idle.py...

I'm trying to replace the standard About menu in Python. IDLE does this at least part-way; the menu is still named "About Python" but it displays the IDLE About window. When run from Wing IDE (under X11) idle doesn't display its About window, and for some reason IDLE doesn't want to debug idle.py...

我已经能够用About MyProgramName"替换About Python",但我要么得到通常的tk About"窗口,要么根本没有About"窗口.IDLE 定义了一个虚拟事件来将控制权传递给它的 About 窗口,我一直在纠结如何定义一个连接到菜单选择的虚拟事件.

I've been able to replace "About Python" with "About MyProgramName", but I either get the usual "tk About" window, or I get no About window at all. IDLE defines a virtual event to pass control to its About window, and I'm stuck on how to define a virtual event that connects to the menu selection.

所以,我有 root.bind('<<about-myprogram>>', about_dialog),但我如何连接它?tk.add_event() 需要一个序列...

So, I have root.bind('<<about-myprogram>>', about_dialog), but how do I connect it up? tk.add_event() needs a sequence...

有什么建议吗?

推荐答案

我一直在寻找一个关于如何制作关于和首选项菜单项的完整示例,但没有找到,所以我自己做了一个.已在 Mac OS 10.4.11 和 Mac OS 10.6.8 上进行测试.

I was looking for a complete example on how to make the About and Preferences menu items, but didn't find any, so I made my own. This was tested on Mac OS 10.4.11 and Mac OS 10.6.8.

from Tkinter import *
from tkMessageBox import *

def do_about_dialog():
    tk_version = window.tk.call('info', 'patchlevel')
    showinfo(message= app_name + "\nThe answer to all your problems.\n\nTK version: " + tk_version)

def do_preferences():
    showinfo(message="Preferences window")

def do_button():
    print("You pushed my button")

def main():
    global app_name
    app_name = "Chocolate Rain"
    global window
    window = Tk()
    window.title("Main")

    # find out which version of Tk we are using
    tk_version = window.tk.call('info', 'patchlevel')
    tk_version = tk_version.replace('.', '')
    tk_version = tk_version[0:2]
    tk_version = int(tk_version)

    menubar = Menu(window)
    app_menu = Menu(menubar, name='apple')
    menubar.add_cascade(menu=app_menu)

    app_menu.add_command(label='About ' + app_name, command=do_about_dialog)
    app_menu.add_separator()

    if tk_version < 85:
       app_menu.add_command(label="Preferences...", command=do_preferences)
    else:
        # Tk 8.5 and up provides the Preferences menu item
        window.createcommand('tk::mac::ShowPreferences', do_preferences)

    window.config(menu=menubar) # sets the window to use this menubar

    my_button = Button(window, text="Push", command=do_button)
    my_button.grid(row=0, column=0, padx=50, pady=30)

    mainloop()

if __name__ == "__main__":
    main()

这篇关于如何替换“关于Tkinter"OSX 上的 Python 菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 14:28