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 Tkinter option menus and menu buttons

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how to use Tkinter options menu and menu buttons". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian to study and learn "how to use Tkinter options menu and menu buttons" together.

In addition to the standard menu, there may be situations where variations of the menu are required. Tkinter provides two common menu-like controls: Option Menu and Menubutton. First look at the action demonstration video, this article mainly describes the effective control of Text OptionMenu and Format control menu button Format implementation method.

OptionMenu action has two characteristics: one is to display a drop-down list for the user to choose, and the other is to display the selection result on the control. The associated codes are as follows:

text_enable = StringVar()text_enable.set('Enable ')

enable_menu = OptionMenu(root, text_enable, 'Enable ', 'Disable')enable_menu.grid(row = 0, column = 0, sticky=E+W)

The code first constructs a StringVar variable text_enable, which is used to store the selection result. Next, build an OptionMenu control and specify the variables it controls as text_enable and the content of each option. When the user selects an option, the contents of that option are set to the text_enable variable.

Next, set the watchdog function for text_enable to control the active state of the Text control:

def var_changed(*args): if text_enable.get() == 'Enable ': text.config(state='normal') text.config(background='#a0ffa0') else: text.config(state='disabled') text.config(background='#efefef')# set variable observer.text_enable.trace_variable('w', var_changed)

A menu button is first and foremost a button, and its construction process is no different from that of a button:

menu_button = Menubutton(root, text='Format', relief=RAISED)menu_button.grid(row=0, column=4, sticky=E+W)

Next, build a normal menu control and specify menu items:

format_menu = Menu(menu_button, tearoff=0)menu_button.config(menu=format_menu)

for i in range(0, 4): format_menu.add_command(label="Format" + str(i), command=lambda v=i : format(v))

A menu button is more like a button. Although it can also represent a drop-down menu when operated by the user, it usually does not represent the selection result.

Thank you for reading, the above is "Tkinter options menu and menu button how to use" the content, after learning this article, I believe we have a deeper understanding of Tkinter options menu and menu button how to use this problem, the specific use of the situation still needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

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

Internet Technology

Wechat

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

12
Report