菜单是GUI程序的常用控件之一,本文说明Tkinter中如何使用菜单。还是先看示例视频:
视频中的小程序是在之前的Text控件示例程序的基础上修改而成的,功能基本一致,只是增加了菜单操作的功能。读者可以稍微注意一下Enable/Disable按钮和菜单项的联动情况。
首先是构建主菜单对象。代码一共分两步:一是以主窗口为参数构建一个菜单对象top_menu,二是用top_menu对象设定主窗口对象的menu属性。
需要注意的是,这里没有使用pack,grid等布局方法。
top_menu = Menu(root)
root.config(menu=top_menu)
接下来的代码为主菜单增加编辑(edit)和格式(format)两个下级菜单。
构建下级菜单也分两步:构建对象并添加到主菜单上。使用的参数应该基本不需要解释,tearoff除外。如果不设定tearoff为False的话,下级菜单的最上面会出现一条横线。代码中故意为edit_menu指定tearoff为False而format菜单使用缺省值,结果可以在菜单表示时看到。
edit_menu = Menu(top_menu, tearoff=False)
top_menu.add_cascade(label='Edit', menu=edit_menu)
format_menu = Menu(top_menu)
top_menu.add_cascade(label='Format', menu=format_menu)
接下来的代码稍长,其中有一部分是之前示例代码的内容,读者只要注意菜单相关的部分就好。
text_enable = IntVar()
text_enable.set(1)
# change state function.
def enable_text():
text_enable.set(1)
# change state button.
eb = Radiobutton(root,text="Enable", width=8, command=enable_text,
value=1, variable=text_enable)
eb.grid(row=1, column=0, sticky=E+W)
edit_menu.add_radiobutton(label='Enable', command=enable_text,
value=1, variable=text_enable)
# change state function.
def disable_text():
text_enable.set(0)
# change state button.
eb = Radiobutton(root,text="Disable", width=8, command=disable_text,
value=0, variable=text_enable)
eb.grid(row=1, column=1, sticky=E+W)
edit_menu.add_radiobutton(label='Disable', command=disable_text,
value=0, variable=text_enable)
代码首先构建一个text_enable变量,然后在按钮或者菜单操作时修改这个变量的值。同时在增加readiobutton菜单项时指定观察对象变量(variable)和显示为选中状态所需的变量值(value)。这种方式和Rediobutton控件的用法完全相同。
接下来我们希望修改Text控件的有效状态,这可以通过在上述代码中的enable_text/disable_text方法中增加修改Text控件的有效状态来实现,也可以采用下面的方法,即监视变量的状态变化:
def var_changed(*args):
if text_enable.get():
text.config(state='normal')
text.config(background='#a0ffa0')
else:
text.config(state='disabled')
text.config(background='#efefef')
# set variable observer.
text_enable.trace_variable('w', var_changed)
接下来是构建其他的普通菜单项的过程,读者可以结合按钮的代码来理解。形式稍有区别,内容完全相同。
# delete selection.
def delete_selection():
try:
sel_from = text.index(SEL_FIRST)
sel_to = text.index(SEL_LAST)
# delete the selection.
text.delete(sel_from, sel_to)
except TclError:
pass
# delete selection button.
db = Button(root,text="Delete", width = 8, command=delete_selection)
db.grid(row=1, column=2, sticky=E+W)
edit_menu.add_radiobutton(label='Delete', command=delete_selection)
# undo button
undo = Button(root, text='Undo', width = 8, command=lambda:text.edit_undo())
undo.grid(row=1, column = 3, sticky=E+W)
edit_menu.add_radiobutton(label='Undo', command=lambda:text.edit_undo())
#redo button
redo = Button(root, text='Redo', width = 8, command=lambda:text.edit_redo())
redo.grid(row=1, column = 4, sticky=E+W)
edit_menu.add_radiobutton(label='Redo', command=lambda:text.edit_redo())
# create fonts
fonts = [
Font(family='SimHei', size=20, weight=BOLD),
Font(family='SimHei', size=16),
Font(family='SimSun', size=12, weight=BOLD),
Font(family='SimSun', size=12)
]
# delete selection.
def format(index):
tag_name = 'Format' + str(index)
try:
sel_from = text.index(SEL_FIRST)
sel_to = text.index(SEL_LAST)
for name in text.tag_names():
text.tag_remove(name, sel_from, sel_to)
text.tag_add(tag_name, sel_from, sel_to)
# set format at first time.
range_count = len(text.tag_ranges(tag_name))
if range_count == 2:
text.tag_config(tag_name, font=fonts[index])
except TclError:
pass
# delete selection button.
for i in range(0, 4):
fb = Button(root,
text="Format" + str(i),
width = 8,
command=lambda v=i : format(v))
format_menu.add_command(label="Format" + str(i),
command=lambda v=i : format(v))
fb.grid(row=2, column=i, sticky=E+W)
# create text widget.
text = Text(root,
undo=True,
background="#a0ffa0", foreground="#000000",
height = 10)
text.grid(row=3 , column=0, columnspan=8)
完整代码可以从下面的链接下载:
https://github.com/xueweiguo/TkinterPrimer/blob/master/Sample/20%20.py
觉得本文有帮助?请分享给更多人。
阅读更多更新文章,请扫描下面二维码,关注微信公众号【面向对象思考】
转载:https://blog.csdn.net/craftsman1970/article/details/100994370