Python - 简单的tkinter菜单
我需要你的帮助。
我想用tkinter写一个简单的菜单,但遇到了一些问题。我的菜单里有两个选项:“第一个”和“第二个”。当我点击第一个时,程序应该显示‘第一个’,然后当我点击第二个时,它应该显示‘第二个’,但第一个的内容还不能出现。
有没有人能帮我一下?谢谢。
我想要的效果是这样的
from tkinter import *
root = Tk()
def do_something():
# this function e.g. write 'The first'
pass
def do_something_other():
# this function e.g. write 'The second' (but 'The first' there will be not yet)
main_menu = Menu(root)
root["menu"] = main_menu
submenu1 = Menu(main_menu)
submenu1.add_command(label="Item1", command=do_something)
submenu1.add_command(label="Item2", command=do_something_other)
main_menu.add_cascade(label="Program", menu=submenu1)
我的目标是,点击选项1或选项2后,画布上的内容会发生变化。
1 个回答
0
你可以使用 Tkinter 的 Label
组件来在你的图形界面(GUI)中显示文字。这个 Label
组件有一个叫做 config
的方法,你可以用它来设置组件的选项,包括它的 text
属性。
下面是一个简单的例子。你需要根据你的菜单和具体需求来修改它:
root = Tk()
def callback(): # this function will access the
label.config(text='Updated Text') # config method of label to change it
label = Label(root, text='Original text') # make the label and set default text
btn = Button(root, text='Change text', command=callback) # make the button, which executes the callback func
label.pack()
btn.pack()
mainloop()