繁体   English   中英

如何更改菜单级联 tkinter 中的文本

[英]how to change the text in menu cascade tkinter

self.root = root
self.root.resizable(width=FALSE, height=FALSE)
self.root.title("Client")
self.menu_bar = Menu(self.root)
self.file_menu = Menu(self.menu_bar, tearoff=0)
self.file_menu.add_command(label="Log in", command=self.show_log_in)
self.file_menu.add_command(label="Reconnect", command=self.reconnect)
self.file_menu.entryconfig(1, state=DISABLED)
self.file_menu.add_command(label="Exit", command=self.root.destroy)
self.menu_bar.add_cascade(label="File", menu=self.file_menu)

我正在尝试将“文件”级联中的文本更改为不同的单词。 那命令是什么? 我试过:

self.menu_bar.entryconfig(0, label="Different word")

但它不起作用。 (提出错误)。

你可以尝试这样的事情:

label_text = StringVar()
self.menu_bar.add_cascade(label=label_text, menu=self.file_menu)
self.label_text.set("File")

def change_label(self,text):
    self.label_text.set(text)

免责声明:我没有测试这段代码。

当您实例化按钮时,您需要有一个命令来调用将更改文本的函数。 例如:

btn3 = Tkinter.Button(root, text="Create Shortcut", bg=gridcolor7, state="normal",  
    width=btnw, height=btnh, command=lambda k='new text': arm_shortcut_command(k))  
btn3.grid(row=r,column=c)

def arm_shortcut_command(k):
    <code to change menu text>
    return

File cascade是 1 个索引条目。 这对我有用:

self.menu_bar.entryconfig(1, label="Different word")

您可以更改调用项标签上的任何项。 例如,从您的代码中,我将“登录”的名称更改为“不同的词”并将“重新连接”的状态更改为在 python3 中禁用。

self.file_menu.entryconfig("Log in",label="Different word")
self.file_menu.entryconfig("Reconnect",state=DISABLE)

暂无
暂无

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

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