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 use the Python Tkinter Menu control

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

Share

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

Most people do not understand the knowledge points of this article "how to use Python Tkinter Menu controls", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use Python Tkinter Menu controls" article.

Menu control (menu control) can be said to be the "essence" of GUI. It groups a series of commands in a visual way, and many programs execute commands (that is, functions) under each group. When the menu is opened, these commands are "explicitly" displayed, making it easy for users to choose, such as the interface for notepad files (.txt file types) in Windows systems:

Figure 1:Menu menu interface

Through the use of menu controls (Menu), we can fully save the limited window area, make our interface more concise and elegant, and avoid bloated and cluttered.

The Tkinter Menu control provides three types of menus, namely: topleve (home directory menu), pull-down (drop-down menu), and pop-up (pop-up menu, or shortcut menu).

The following table lists the relevant methods used to create menus, as follows:

Method description add_cascade (* * options) adds a parent menu and connects a specified submenu to the parent menu through the menu parameter to create a drop-down menu. Add_checkbutton (* * options) add a menu item for multiple-choice buttons add_command (* * options) add a normal command menu item add_radiobutton (* * options) add a menu item for a radio button add_separator (* * options) add a split line add (add (itemType, options)) add menu items, where itemType parameters can be the following: "command", "cascade"

"checkbutton", "radiobutton", "separator", and use the options option to set

Menu other properties.

In addition to the above methods, the Menu control also provides other ways to manipulate menu items, such as deleting menu items, getting menu items, setting specified menu items, and so on, as shown in the following table:

Delete (index1, index2=None) 1. Delete all menu items for index1 ~ index2 (included)

two。 If the index2 parameter is omitted, delete the menu item entrycget (index, option) pointed to by index1 to get the value of an option of the specified menu item entryconfig (index, * * options) set the option of the specified menu item index (index) returns the ordinal insert (index, itemType, * * options) of the option corresponding to the index parameter to insert the menu item of the specified type to the location specified by the index parameter. The type can be

Is: "command", "cascade", "checkbutton", "radiobutton"

Or one of "separator", or you can use the insert_ type () form to

For example, insert_cascade (index, * * options).. Invoke (index) calls the method post (x, y) associated with the menu item specified by index to display the pop-up menu type (index) at the specified location to get the index parameter specified menu item type unpost () remove pop-up menu yposition (index) returns the vertical offset position of the menu item specified by the index parameter

The following is a brief introduction to the options parameter of the Menu control, as follows:

Property description accelerator1. Set the shortcut key for the menu item. The shortcut key will be displayed on the right side of the menu item. For example, accelerator = "Ctrl+O" indicates opening.

two。 Note that this option does not automatically connect shortcut keys to menu items, but must be bound by keystrokes to implement the callback function label defines the text menu within the menu item when command selects the menu item. this property is used in conjunction with the add_cascade () method to add a submenu item selectcolor that specifies the color state that defines the status of the menu item when the menu item is displayed as a radio button or a multi-selection button It can be normal, active or disabledonvalue/offvalue1. By default, the variable option is set to 1 to indicate the selected state, otherwise set to 0. You can customize the value of the unchecked state by setting the value of offvalue/onvalue.

2.tearoff1. If this option is True, an optional separator line is displayed above the menu item

two。 Note: the separator line will separate this menu item into a new window underline sets which character in the menu item should have an underscore value1. Set the value of the button menu item

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 selected variable when the menu item is a radio button or a multi-choice button, the associated variable creates the home directory menu

The main directory menu is also called the "top-level menu". Other submenus such as drop-down menus need to be based on the "top-level menu". The following example creates a program similar to the notepad interface, with the following code:

From tkinter import * import tkinter. Messagebox# creates the main window win = Tk () win.config (bg='#87CEEB') win.title ("C language Chinese net") win.geometry ('450x350 / 300 / 200') win.iconbitmap (' C:/Users/Administrator/Desktop/ C Chinese net logo.ico') # binds an execution function When you click on a menu item, a message dialog box def menuCommand (): tkinter.messagebox.showinfo ("main menu bar", "you are using the main menu bar") # creates a home directory menu, also known as top-level menu main_menu = Menu (win) # add command menu items Use add_command () to implement main_menu.add_command (label= "File", command=menuCommand) main_menu.add_command (label= "Edit", command=menuCommand) main_menu.add_command (label= "format", command=menuCommand) main_menu.add_command (label= "View", command=menuCommand) main_menu.add_command (label= "help", command=menuCommand) # display menu win.config (menu=main_menu) win.mainloop ()

The running result of the program is as follows:

Figure 2:Menu control creates the main menu

As shown in the figure above, a message dialog box pops up when you click any of the menu options in the home directory.

Create a drop-down menu

Pull-down menu is an important part of the main menu, and it is also an important interactive interface for users to select relevant commands, and the way to create a pull-down menu is also very simple, but we need to note that the pull-down menu is based on the established main menu (that is, the top-level menu), not on the main window, which must not be confused, otherwise the creation of the drop-down menu will fail.

Let's continue to emulate the relevant features of notepad to create a drop-down menu, with an example as follows:

# create a drop-down menu from tkinter import * import tkinter .messagebox # create the main window win = Tk () win.config (bg='#87CEEB') win.title ("C language Chinese net") win.geometry ('450x350 / 300 / 200') win.iconbitmap (' C:/Users/Administrator/Desktop/ C Chinese net logo.ico') # create an execution function Execute def menuCommand (): tkinter .messagebox .showinfo ("pull-down menu", "you are using the pull-down menu function") # create a home directory menu (top-level menu) mainmenu = Menu (win) # add a submenu of the "file" menu on the top-level menu without adding the splitter line filemenu = Menu (mainmenu, tearoff=False) # add the menu item of the "file" menu And use accelerator to set the shortcut key filemenu.add_command (label= "new", command=menuCommand,accelerator= "Ctrl+N") filemenu.add_command (label= "open", command=menuCommand,accelerator= "Ctrl+O") filemenu.add_command (label= "save", command=menuCommand,accelerator= "Ctrl+S") # to add a splitter line filemenu.add_separator () filemenu.add_command (label= "exit", command=win. Quit) # add the "file" option to the home directory menu, and bind mainmenu.add_cascade (label= "file", menu=filemenu) # to the drop-down menu through the menu parameter # set the main menu on the window win.config (menu=mainmenu) # bind the keyboard event, and the execution function win.bind ("", menuCommand) win will be triggered when the corresponding key on the keyboard is pressed. Bind (", menuCommand) win.bind (", menuCommand) win. Bind ("", menuCommand) win. Bind (", menuCommand) win.bind (", menuCommand) # displays the main window win.mainloop ()

The running results of the program are as follows:

Figure 3:Menu control drop-down menu

Create a pop-up menu bar

A pop-up menu bar, also known as a shortcut menu bar, for example, pops up a menu bar by clicking the right mouse button, which contains some commonly used tabs, such as copy, paste, etc., as follows: clicking the right mouse button in the notepad space will pop up a menu bar.

Figure 4: pop-up menu bar

The Tkinter Menu control can also implement the above functions, and it is not complex, as shown in the following example:

Import tkinter as tkroot = tk.Tk () root.config (bg='#8DB6CD') root.title ("C language Chinese net") root.geometry ('400x300') root.iconbitmap ('C:/Users/Administrator/Desktop/ C Chinese net logo.ico') def func (): print (' you executed the order through the pop-up menu') # create a pop-up menu menu = tk.Menu (root, tearoff=False) menu.add_command (label= "New") Command=func) menu.add_command (label= "copy", command=func) menu.add_command (label= "paste", command=func) menu.add_command (label= "cut", command=func) # define the event function def command (event): # use post () to display the pop-up menu menu.post (event.x_root, event.y_root) # bind the right mouse button at the specified location This is the mouse binding event # to click the right mouse button, 1 to indicate the left button, and 2 to click the middle pulley root.bind ("", command) root.mainloop ()

The running result of the program is as follows:

Figure 5:Menu control pop-up menu

Menu button control

Menubutton (menu Button Control) is a button associated with the Menu control, and the drop-down menu pops up automatically when we press the button. Menu buttons created by Menubutton can be freely placed anywhere in the window, thus improving the flexibility of the GUI interface.

Let's look at a set of simple usage examples:

From tkinter import * win=Tk () win.config (bg='#87CEEB') win.title ("C language Chinese net") win.geometry ('450x350 / 300 / 200') win.iconbitmap (' C:/Users/Administrator/Desktop/ C Chinese net logo.ico') # create a menu button menubtn=Menubutton (win, text=' Click to operate', relief='sunk') # set position (layout) menubtn.grid (padx=195, pady=105) # add menu, use tearoff parameter not to display split line filemenu=Menu (menubtn) Tearoff = False) filemenu.add_command (label=' New') filemenu.add_command (label=' Delete') filemenu.add_command (label=' copy') filemenu.add_command (label=' Save') # display menu Bind a menu command to a menu button object menubtn.config (menu=filemenu) win.mainloop ()

The running result of the program is as follows:

Figure 6:Menubutton menu button

The above is about the content of this article on "how to use Python Tkinter Menu controls". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

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