Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Python Tkinter Menu components

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to understand Python Tkinter Menu components", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand Python Tkinter Menu components.

Catalogue

When to use Menu components?

Usage

Parameters.

Method

Add_cascade (* * options)

Add_checkbutton (* * options)

Add_command (* * options)

Add_radiobutton (* * options)

Add_separator (* * options)

Delete (index1, index2=None)

Entrycget (index, option)

Entryconfig (index, * * options)

Entryconfigure (index, * * options)

Index (index)

Insert (index, itemType, * * options)

Insert_cascade (index, * * options)

Insert_checkbutton (index, * * options)

Insert_command (index, * * options)

Insert_radiobutton (index, * * options)

Insert_separator (index, * * options)

Invoke (index)

Post (x, y)

Type (index)

Unpost ()

Yposition (index)

When to use Menu components?

Menu components are usually used to implement various menus on applications, but since this component is implemented in the underlying code, it is not recommended that you implement menu functions through buttons and other components.

Usage

To create a top-level menu, you need to create a menu instance first, and then use the add () method to add commands and other submenus:

Import tkinter as tk root = tk.Tk () def callback (): print ("~ called ~") # create a top-level menu menubar = tk.Menu (root) menubar.add_command (label = "Hello", command = callback) menubar.add_command (label = "Quit", command = root.quit) # display menu root.config (menu = menubar) root.mainloop ()

Create a drop-down menu (or other submenu) in much the same way, except that they eventually need to be added to the main menu (rather than the window):

Import tkinter as tk root = tk.Tk () def callback (): print ("~ called ~") # create a top-level menu menubar = tk.Menu (root) # create a drop-down menu "File" Then add it to the top-level menu filemenu = tk.Menu (menubar, tearoff=False) filemenu.add_command (label= "Open", command=callback) filemenu.add_command (label= "Save", command=callback) filemenu.add_separator () filemenu.add_command (label= "exit", command=root.quit) menubar.add_cascade (label= "File", menu=filemenu) # create another drop-down menu "Edit" Then add it to the top-level menu editmenu = tk.Menu (menubar, tearoff=False) editmenu.add_command (label= "cut", command=callback) editmenu.add_command (label= "copy", command=callback) editmenu.add_command (label= "paste", command=callback) menubar.add_cascade (label= "edit", menu=editmenu) # display menu root.config (menu=menubar) root.mainloop ()

The method of creating a pop-up menu is also consistent, but you need to display it explicitly using the post () method:

Import tkinter as tk root = tk.Tk () def callback (): print ("~ called ~") # create a pop-up menu menu = tk.Menu (root, tearoff=False) menu.add_command (label= "undo", command=callback) menu.add_command (label= "redo", command=callback) frame = tk.Frame (root, width=512, height=512) frame.pack () def popup (event): menu.post (event.x_root Event.y_root) # bind the right mouse button frame.bind ("", popup) root.mainloop () parameter

Menu (master=None, * options) (class)

Master-parent component

* * options-component options. The specific meaning and usage of each option are listed in the table below:

Option means activebackground setting background color when Menu is in "active" state (set by state option) activeborderwidth setting when Menu is in "active" state (set by state option) activeforeground setting when Menu is in "active" state (set by state option) foreground color background sets background color bg as background borderwidth specifies border width bd as cursor as borderwidth Mouse style when the mouse floats over the Menu disabledforeground specifies the foreground color when the Menu is in the "disabled" state font specifies the font of the text in Menu the foreground color of Menu is set fg is the same as foreground postcommand associates this option with a method This method is automatically called relief1 when the menu is opened. Specify the border styl

two。 The default is "flat"

3. You can also set "sunken", "raised", "groove" or "ridge" selectcolor to specify the color tearoff1 of the flag in the selection when the menu item is displayed as a radio button or a multi-selection button. By default, menus can be "torn off" (click-try on the IDLE menu)

two。 Set this option to Flase to turn off this feature tearoffcommand if you want to notify your program when the user "rips" your menu, then you can associate this option with a method, so when the user "rips" your menu, Tkinter will call your method with two parameters (one parameter is the ID of the current window, and the other is the window ID that hosts the torn menu) title by default The "torn" menu title is the name of its main menu, but you can also change the title method by changing the value of this item.

Add (type, * * options)

-- the type parameter specifies the type of menu to be added, which can be: "command", "cascade", "checkbutton", "radiobutton" or "separator"

-- you can also set the properties of the menu through the options option. The following table lists the options that options can use and their specific meanings:

Option means accelerator1. Displays the accelerator key (shortcut key) for this menu item

two。 For example, accelerator = "Ctrl+N"

3. This option displays only the Does not implement the function of accelerator keys (through key binding) activebackground sets the background color when the menu item is in the "active" state (set through the state option) activeforeground setting when the menu item is in the "active" state (set through the state option) the foreground color background sets the background color of the menu item bitmap specifies the bitmap columnbreak displayed on the menu item starts from the menu item Show command from one column to associate this option with a method This method compound1 is automatically called when the user clicks the menu item. Controls the mixing mode of text and images in menu items

two。 If this option is set to "center", the text is displayed on the image (text overlapping image)

3. If this option is set to "bottom", "left", "right" or "top", then the image is displayed next to the text (such as "bottom", then the image below the text font specifies whether the font foreground of the text sets the foreground color hidemargin displays the blank image1 next to the menu item. Specifies the picture displayed by the menu item

two。 The value should be PhotoImage,BitmapImage, or the compatible object label specifies the text menu1 that the menu item displays. This option is only used in menus of cascade type

two。 Used to specify its subordinate menu offvalue1. By default, the variable option is set to 1 to indicate the selected state, and vice versa to 0

two。 Setting the value of offvalue allows you to customize the value of the unchecked state onvalue1. By default, the variable option is set to 1 to indicate the selected state, and vice versa to 0

two。 Set the value of onvalue to customize the value of the selected state selectcolor specifies the color of the flag in the selection when the menu item is displayed as a radio button or multi-select button selectimage if you use a picture instead of text in the radio button or multi-select button menu, then set this option to specify the picture state1 that is displayed when the menu item is selected. Used with the text option to specify which character is underlined (for example, to represent keyboard shortcuts) underline1. Used to specify an underscore at a character of the menu item

two。 For example, if it is set to 1, it means that the menu item is underlined value1. Used to mark the value of the button when the menu item is a radio button

two。 All buttons in the same group should have different values

3. By comparing this value with the value of the variable option, you can determine which button the user has selected

4. If you don't understand the details in use, you can refer to the description of the Radiobutton component variable1. The variable associated with a menu item when it is a radio button or a multi-select button

two。 If you don't understand the details in use, you can refer to the description of Checkbutton and Radiobutton components add_cascade (* * options)

-- add a parent menu

-equivalent to add ("cascade", * * options)

Add_checkbutton (* * options)

Add a menu item with a multi-select button

-equivalent to add ("checkbutton", * * options)

Add_command (* * options)

Add a normal command menu item

-equivalent to add ("command", * * options)

Add_radiobutton (* * options)

-- add a menu item for a radio button

-equivalent to add ("radiobutton", * * options)

Add_separator (* * options)

-- add a split line

-equivalent to add ("separator", * * options)

Delete (index1, index2=None)

-- Delete all menu items of index1 ~ index2 (included)

-- if you omit the index2 parameter, delete the menu item pointed to by index1

Note: you cannot use this method for a menu that is "torn off"

Entrycget (index, option)

-- gets the value of an option for the specified menu item

Entryconfig (index, * * options)

-- set options for specifying menu items

-- for the parameters and specific meaning of the option, please refer to the add () method above

Entryconfigure (index, * * options)

-- same as entryconfig ()

Index (index)

Returns the sequence number of the option corresponding to the index parameter (for example, e.index ("end"))

Insert (index, itemType, * * options)

-inserts a menu item of the specified type to the location specified by the index parameter

-- the itemType parameter specifies the type of menu to be added, which can be: "command", "cascade", "checkbutton", "radiobutton" or "separator"

-- for the parameters and specific meaning of the option, please refer to the add () method above

Insert_cascade (index, * * options)

-- add a parent menu at the location specified by the index parameter

-equivalent to insert ("cascade", * * options)

Insert_checkbutton (index, * * options)

-- add a multi-select button at the location specified by the index parameter

-equivalent to insert ("checkbutton", * * options)

Insert_command (index, * * options)

-- add a normal command menu item at the location specified by the index parameter

-equivalent to insert ("command", * * options)

Insert_radiobutton (index, * * options)

-- add a radio button at the location specified by the index parameter

-equivalent to insert ("radiobutton", * * options)

Insert_separator (index, * * options)

-- add a splitter line at the location specified by the index parameter

-equivalent to insert ("separator", * * options)

Invoke (index)

-- call the method associated with the menu item specified by index

If it is a radio button, set the menu item to be selected

If it is a multi-select button, toggle the selected state of the menu item

Post (x, y)

-displays a pop-up menu at the specified location

-- refer to the example of creating a pop-up menu in [usage] above

Type (index)

-- get the type of menu item specified by the index parameter

-- the return value can be "command", "cascade", "checkbutton", "radiobutton" or "separator"

Unpost ()

-remove pop-up menus,

Yposition (index)

-- returns the vertical offset of the menu item specified by the index parameter

The purpose of this method is to allow you to accurately place the pop-up menu relative to the current mouse position

At this point, I believe you have a deeper understanding of "how to understand Python Tkinter Menu components". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report