Tkinter (11) MenuButton

Time:2021-5-4

Creation of menu button parts and its options

import tkinter as tk
parent = tk.Tk()
menu_widget = tk.Menubutton(parent, option, ...)
option explain
activebackground Background color when active
activeforeground Foreground color when active
anchor The positioning point of the component is internally defined as center
bg or background Background color
bitmap Lattice diagram
bd or borderwidth The degree of a box is determined to be 2 pixels
compound When a graph is shared with text, the position of the graph relative to the text
cursor The mouse icon that is displayed when the mouse moves to the part
direction The menu is relative to the button, left / right /’above ‘
disabledforeground Foreground color when disabled
fg or foreground Foreground
font Text font
height Number of rows
highlightbackground Focus color when not in focus
highlightcolor Focus color when focusing
highlightthickness If the focus thickness is set at 1, 0, there is no change
image picture
justify Alignment of multiline text, left / right / Center
menu Menu object
padx The interval of inner horizontal points is 1 pixel
pady The inner vertical pixel interval is defined as 1 pixel
relief Lace style, internally designated as raised
state NORMAL, DISABLED
takefocus Will tab key cycle focus 0 / 1 in the part
text Part string
textvariable Text string variable
underline Set the position of the bottom line, and set – 1 as none
width Text dimension
wraplength Text line width, in pixels

Example windows and code

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()

This work adoptsCC agreementReprint must indicate the author and the link of this article

Jason Yang