Python tkinter--Chapter 16 Menu (Menu) menu item options

16.3 Menu Item Options
Options when creating menu items, such as defining shortcut keys and so on.

options describe
accelerator Define shortcut keys. For example accelerator='^x'. At the same time, it is also necessary to bind the keyboard input to realize the function of the shortcut key.
activebackground The background color when the mouse is over.
activeforeground Text color on mouseover
background background color. The default is the system specified color
bitmap Display bitmap images in menu items
columnbreak Start displaying menu items in a new column.
command Callback function for menu items
compound When graphics and text are mixed, the way to place text.
font set font
foreground Defines the color of the menu item text
image Display pictures in gif and other formats in menu items
label display text information
menu Only valid when adding a cascading menu. It is to associate the lower-level submenu with the upper-level parent menu
offvalue When the checkbutton is not selected, the default value is 0. It can be modified to other values ​​by setting this parameter
onvalue When the checkbutton is selected, the default value is 1. It can be modified to other values ​​by setting this parameter
selectcolor Set the color of the checkbutton or radiobutton logo
selectimage When the checkbutton or radiobutton uses image as the prompt information, it can be set to use a different image than the unselected image when it is selected.
state The state of the menu item.
underline Set the underline. The method of use is underline=index. index is the position of the character, and an underline will appear under the character at this position.
value The value of the radiobutton menu item. A radiobutton is a set of menu items. The same group of radiobuttons uses the same variable, but with different values.
variable Variable associated with checkbutton or radiobutton.
16.3.1 accelerator
Define shortcut keys. Need to bind keyboard input. Note that binding characters are case sensitive.
import tkinter as tk
from tkinter import ttk

root=tk.Tk()
root.geometry('320x240')
b1=tk.Label()
b1.pack()
def open_file(*args):
    b1['text']='打开文件'
    print(*args)
def close_file():
    b1['text']='关闭文件'
    
menubar = tk.Menu(root)
filemenu=tk.Menu(menubar)

filemenu.add_command(label='打开文件',
                     command=open_file,accelerator='^o')
filemenu.add_command(label='关闭文件',command=close_file)
menubar.add_cascade(label='文件', menu=filemenu)
menubar.add_command(label='退出',command=root.destroy)

root.bind('<Control-o>',open_file)
root.config(menu=menubar)
root.mainloop()

16.3.2 activebackground
The background color when the mouse is over. See 16.1.1
16.3.3 activeforeground
text color when the mouse is over. See 16.1.3
16.3.4 background
The background color of the menu item. See 16.1.4
16.3.5 bitmap
to display bitmap pictures. takes precedence over text. However, image files in this format are rarely used. This method is not recommended for use in programs.

import tkinter as tk
from tkinter import ttk

root=tk.Tk()
root.geometry('320x240')
b1=tk.Label()
b1.pack()
def open_file(*args):
    b1['text']='打开文件'
    print(*args)
def close_file():
    b1['text']='关闭文件'
    
menubar = tk.Menu(root)
filemenu=tk.Menu(menubar)

filemenu.add_command(label='打开文件',
                     command=open_file,bitmap='info')
filemenu.add_command(label='关闭文件',command=close_file)
menubar.add_cascade(label='文件', menu=filemenu)
menubar.add_command(label='退出',command=root.destroy)

root.bind('<Control-o>',open_file)

root.config(menu=menubar)
root.mainloop()

Result:
insert image description here
16.3.6 columnbreak
Normally, the menu items are displayed in one column, but after setting columnbreak=True, a new column will be displayed to display the following menu items.

import tkinter as tk
from tkinter import ttk

root=tk.Tk()
root.geometry('320x240')
b1=tk.Label()
b1.pack()
def open_file(*args):
    b1['text']='打开文件'
    print(*args)
def close_file():
    b1['text']='关闭文件'
def saveas():
    b1['text']='另存为'

menubar = tk.Menu(root)
filemenu=tk.Menu(menubar)

filemenu.add_command(label='打开文件',
                     command=open_file,columnbreak=True)
filemenu.add_command(label='关闭文件',
                     command=close_file,columnbreak=True)
filemenu.add_command(label='另存为',command=saveas)
menubar.add_cascade(label='文件', menu=filemenu)
menubar.add_command(label='退出',command=root.destroy)

root.config(menu=menubar)
root.mainloop()

Result:
insert image description here
16.3.7
The processing function of the command menu item has been explained earlier.
16.3.8 Compound
Defines the method of mixing graphics and text. See the description in Chapter 4 Label.
16.3.9 font
Set the display font of the menu item. See instructions in 16.1.8.
16.3.10 foreground
defines the color of the menu item text. See 16.1.9.
16.3.11 hidemargin
By default, a certain amount of blank space is left on the left and right sides of the menu item. If hidemargin=True, these left blank spaces will be removed.

import tkinter as tk
from tkinter import ttk

root=tk.Tk()
root.geometry('320x240')
b1=tk.Label()
b1.pack()
def open_file(*args):
    b1['text']='打开文件'
    print(*args)
def close_file():
    b1['text']='关闭文件'
    
menubar = tk.Menu(root)
filemenu=tk.Menu(menubar,tearoff=False)

filemenu.add_command(label='打开文件',
                     command=open_file,hidemargin=True)
filemenu.add_command(label='关闭文件',
                     command=close_file,hidemargin=True)
menubar.add_cascade(label='文件', menu=filemenu)
menubar.add_command(label='退出',command=root.destroy)

root.bind('<Control-o>',open_file)
root.config(menu=menubar)
root.mainloop()

Result:
insert image description here
insert image description here
Description: The menu items in the same column must all set hidemargin=True in order to remove the reserved blank space.
16.3.12 image
Display pictures in .gif format on the menu bar.
The code is as follows:

p=tk.PhotoImage(file='a.gif')
filemenu.add_command(label='打开文件',command=open_file,image=p)

insert image description here
16.3.13 label
displays the text information of the menu item.
16.3.14 menu
is valid when setting cascade menu. It is to associate the lower-level submenu with the upper-level parent menu.
16.3.15 offvalue and onvalue
set the value of the checkbutton when it is not selected or selected. By default, the unselected value is 0, and the selected value is 1. offvalue corresponds to the value when it is not selected, and onvalue represents the value when it is selected. See the description of the Checkbutton control.
16.3.16 selectcolor
Set the logo color of checkbutton or radiobutton. See 16.1.12 for details.
16.3.17 selectimage
In the case of checkbutton or radiobutton, if the state of the menu item is selected, you can use selectimage to set different images to be displayed to distinguish it from the unselected state.
16.3.18 state
Set the state of the menu item. Can be tk.NORMAL, tk.DISABLED or tk.ACTIVE.
16.3.19 underline
set the underline under the specified character. underline is an index value, and this index is the position of the character. The function of the underscore is generally to remind the user what the shortcut key of the menu item is.
The usage is underline=index.
16.3.20 value
The value of the radiobutton menu item. A radiobutton is a set of menu items. The same group of radiobuttons uses the same variable, but with different values.
16.3.21 variable
The variable associated with checkbutton or radiobutton can get the status of the two menu items.

Guess you like

Origin blog.csdn.net/weixin_42272768/article/details/100809120