Tkinter (11) 菜单按钮部件 Menubutton

菜单按钮部件的创建及其选项

import tkinter as tk
parent = tk.Tk()
menu_widget = tk.Menubutton(parent, option, ...)
选项 说明
activebackground active 时的背景色
activeforeground active 时的前景色
anchor 部件定位点,内定为 CENTER
bg or background 背景色
bitmap 点阵图
bd or borderwidth 框的寛度,内定为 2 图素
compound 图与文本共用时,图相对于文本的位置.
cursor 当鼠标移到部件时,所显示的鼠标图示
direction 菜单相对于按钮置, LEFT/RIGHT/‘above’
disabledforeground disabled 时的前景色
fg or foreground 前景色
font 文本字体
height 行数
highlightbackground 非聚焦时的聚焦颜色
highlightcolor 聚焦时的聚焦颜色
highlightthickness 聚焦厚度,内定 1, 0 则无
image 图片
justify 多行文本的对齐方式,LEFT/RIGHT/CENTER
menu 菜单对像
padx 内部水平点素间隔, 内定为 1 图素
pady 内部垂直点素间隔, 内定为 1 图素
relief 花边样式,内定为 RAISED
state NORMAL, DISABLED
takefocus TAB 键在部件是否会循环焦点 0/1
text 部件字符串
textvariable 文本字串变量
underline 设置下底线的位置,内定 -1 为无
width 文本寛度
wraplength 文本分行寛度,单位为点素

范例视窗及代码

Tkinter (11) 菜单按钮部件 Menubutton

import tkinter as tk

root = tk.Tk()
root.wm_title("Menubutton Demo")

font = ('Courier New', 20, 'bold')
menu_button = tk.Menubutton(root, text='Menubutton', font=font, relief=tk.RAISED)
menu_button.grid(row=0, column=0)
label = tk.Label(root, text='', width=20, height=5)
label.grid(row=1, column=0)

menu = tk.Menu(menu_button, font=font, tearoff=0)
menu_button['menu'] = menu

var1 = tk.IntVar()
var2 = tk.IntVar()

menu.add_checkbutton(label='Menu_1', variable=var1)
menu.add_checkbutton(label='Menu_2', variable=var2)

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

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