Tkinter (10) 菜單部件 Menu

菜單部件的创建及其选项

菜單中的選項可以是本文, 圖片, 级联, 复选按钮 Checkbutton, 或一群的单选按钮 Radiobutton.

import tkinter as tk
parent = tk.Tk()
menu_widget = tk.Menu(parent, option, ...)
选项 说明
activebackground active 时的背景色
activeborderwidth active 时的边框宽度, 內定為 1 圖素
activeforeground active 时的前景色
bg or background 背景色
bd or borderwidth 框的寛度, 內定為 1 圖素
cursor 当鼠标移到部件时,所显示的鼠标图示
disabledforeground disabled 时的前景色
font 文本字体
fg or foreground 前景色
postcommand 每次菜單調用時, 都調用該程序
relief 花边样式,内定为 RAISE
selectcolor 被选择 checkbutton 或 radiobutton 的颜色
tearoff 有無下拉式選單, 第一項是否為下拉元素
tearoffcommand 下拉選單調用程序, 兩個參數為原父視窗ID以及新下拉式菜單的根視窗ID
title 另設置下拉式菜單的視窗標題

菜單項的類別 kind

  • ‘cascade’ 级联類菜單項
  • ‘checkbutton’ 复选按钮類菜單項
  • ‘command’ 一般命令菜單項
  • ‘radiobutton’ 单选按钮類菜單項
  • ‘separator’ 分隔線類菜單項

菜單方法及说明

方法 & 说明
add(kind, coption, …)
增加下一個菜單選項
add_cascade(coption, …)
同add(‘cascade’, coption, …)
add_checkbutton(coption, …)
同add(‘checkbutton’, coption, …)
add_command(coption, …)
同add(‘command’, coption, …)
add_radiobutton(coption, …)
同add(‘radiobutton’, coption, …)
add_separator()
同add(‘separator’, coption, …)
delete(index1, index2=None)
刪除第index1項(到含第index2項)的菜單項
entrycget(index, coption)
返回第index菜單項的coption值
entryconfigure(index, coption, …)
設置第index菜單項的coption值
index(i)
返回選擇索引的菜單項的index
insert_cascade(index, coption, …)
類似add(kind, coption, …), 但為插入方式
insert_checkbutton(index, coption, …)
類似add(kind, coption, …), 但為插入方式
insert_command(index, coption, …)
類似add(kind, coption, …), 但為插入方式
insert_radiobutton(index, coption, …)
類似add(kind, coption, …), 但為插入方式
insert_separator(index)
類似add(kind, coption, …), 但為插入方式
invoke(index)
類似點選該菜單項
post(x, y)
菜單顯示座標位置 (x, y)
type(index)
返回索引處菜單項的類型tk.CASCADE, tk.CHECKBUTTON, tk.COMMAND, tk.RADIOBUTTON, tk.SEPARATOR, or tk.TEAROFF
yposition(n)
返回第n個菜單項的對應像素y位置

菜單方法的 coption 選項

選項 说明
accelerator 僅顯示快捷鍵, 如 ‘^X’ 表示 ctrl-X, 實際上必須另外綁定按鍵
activebackground active 时的背景色
activeforeground active 时的前景色
background 背景色
bitmap 点阵图
columnbreak 菜單項是否往右延伸, 內定為否, 以下延伸
command 部件状态值改变调用的程序
compound 图与文本共用时,图相对于文本的位置. LEFT/RIGHT/TOP/BOTTOM/CENTER
font 文本字体
foreground 前景色,点阵图时为颜色值 1 的颜色
hidemargin 是否去掉左右的間距, 內定為否
image 图片
label 菜單項的文本
menu 级联類菜單項
offvalue checkbutton 复选按钮類菜單項的 off 值, 內定為 0
onvalue checkbutton 复选按钮類菜單項的 on 值, 內定為 1
selectcolor 设置checkbutton或者radiobutton的颜色
selectimage 设定显示不同的图片和未被选中的状态相区分
state 状态 NORMAL 内定 / ACTIVE/DISABLED
underline 设置下底线的位置,内定 -1 为无
value radiobutton菜单项的数值
variable checkbutton或者radiobutton的变量

范例视窗及代码

Tkinter (10) 菜單部件 Menu

import tkinter as tk

root = tk.Tk()
root.wm_title("Menu Demo")
font = ('Courier New', 16, 'bold')

main_menu = tk.Menu(root, font=font)
root.config(menu=main_menu)

menu_file = tk.Menu(main_menu, font=font, tearoff=0)
main_menu.add_cascade(label='File', font=font, menu=menu_file)
menu_file.add_command(label='New', accelerator='^N')
menu_file.add_command(label='Open', accelerator='^O')
menu_file.add_command(label='Files', accelerator='^F')

menu_edit = tk.Menu(main_menu, font=font, tearoff=0)
main_menu.add_cascade(label='Edit', font=font, menu=menu_edit)
menu_edit.add_command(label='Search', accelerator='^S')
menu_edit.add_command(label='Replace', accelerator='^R')
menu_edit.add_command(label='Copy', accelerator='^C')

menu_help = tk.Menu(main_menu, font=font, tearoff=0)
main_menu.add_cascade(label='Help', font=font, menu=menu_help)
menu_help.add_command(label='About', accelerator='^A')
menu_help.add_command(label='Version', accelerator='^V')
menu_help.add_command(label='Manual', accelerator='^M')

root.mainloop()
本作品采用《CC 协议》,转载必须注明作者和本文链接
Jason Yang
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!