简体   繁体   English

根据python + tkinter中菜单的选择更新的菜单按钮

[英]Menubutton that updates depending on the selection in the menu in python + tkinter

How can I get the menubutton text to update according to my selection in the menu? 如何根据菜单中的选择更新菜单按钮文本? If I select opt1 I would like that the menubutton text updated to "opt1" after releasing/exiting the menu. 如果我选择opt1,我希望在释放/退出菜单后将菜单按钮文本更新为“ opt1”。 Below is a simple example. 下面是一个简单的示例。

#!/usr/bin/python
#
from Tkinter import *

root = Tk()


def opt1():
    print "opt 1 selected "
    return;
def opt2():
    print "opt 2 selected "
    return;
def opt3():
    print "opt 3 selected "
    return;

elementTypeBtn = Menubutton(root, relief='raised', text='select sth')
elementTypeBtn.grid(row=0, column=0)
elementTypeBtn.menu = Menu(elementTypeBtn)
elementTypeBtn.menu.add_command(label='opt1', underline=0, command=opt1)
elementTypeBtn.menu.add_command(label='opt2', underline=0, command=opt2)
elementTypeBtn.menu.add_command(label='opt3', underline=0, command=opt3)
elementTypeBtn['menu'] = elementTypeBtn.menu

root.mainloop()

Similar to how you update any attribute of any widget, you call the config (or configure ) method that is common to all widgets: 与更新任何窗口小部件的任何属性的方式类似,您可以调用所有窗口小部件通用的config (或configure )方法:

def opt1():
    elementTypeBtn.configure(text="opt 1 selected")
    return;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM