📅  最后修改于: 2020-10-27 01:53:08             🧑  作者: Mango
菜单按钮窗口小部件可以定义为始终显示给用户的下拉菜单。它用于为用户提供一个选项,以选择应用程序内存在的适当选项。
Menubutton用于在Python应用程序中实现各种类型的菜单。菜单与菜单按钮关联,当用户单击菜单时可以显示菜单按钮的选择。
下面给出了使用Python tkinter菜单按钮的语法。
w = Menubutton(Top, options)
下面列出了各种选项。
SN | Option | Description |
---|---|---|
1 | activebackground | The background color of the widget when the widget is under focus. |
2 | activeforeground | The font color of the widget text when the widget is under focus. |
3 | anchor | It specifies the exact position of the widget content when the widget is assigned more space than needed. |
4 | bg | It specifies the background color of the widget. |
5 | bitmap | It is set to the graphical content which is to be displayed to the widget. |
6 | bd | It represents the size of the border. The default value is 2 pixels. |
7 | cursor | The mouse pointer will be changed to the cursor type specified when the widget is under the focus. The possible value of the cursor type is arrow, or dot etc. |
8 | direction | It direction can be specified so that menu can be displayed to the specified direction of the button. Use LEFT, RIGHT, or ABOVE to place the widget accordingly. |
9 | disabledforeground | The text color of the widget when the widget is disabled. |
10 | fg | The normal foreground color of the widget. |
11 | height | The vertical dimension of the Menubutton. It is specified as the number of lines. |
12 | highlightcolor | The highlight color shown to the widget under focus. |
13 | image | The image displayed on the widget. |
14 | justify | This specified the exact position of the text under the widget when the text is unable to fill the width of the widget. We can use the LEFT for the left justification, RIGHT for the right justification, CENTER for the centre justification. |
15 | menu | It represents the menu specified with the Menubutton. |
16 | padx | The horizontal padding of the widget. |
17 | pady | The vertical padding of the widget. |
18 | relief | This option specifies the type of the border. The default value is RAISED. |
19 | state | The normal state of the Mousebutton is enabled. We can set it to DISABLED to make it unresponsive. |
20 | text | The text shown with the widget. |
21 | textvariable | We can set the control variable of string type to the text variable so that we can control the text of the widget at runtime. |
22 | underline | The text of the widget is not underlined by default but we can set this option to make the text of the widget underlined. |
23 | width | It represents the width of the widget in characters. The default value is 20. |
24 | wraplength | We can break the text of the widget in the number of lines so that the text contains the number of lines not greater than the specified value. |
# !/usr/bin/python3
from tkinter import *
top = Tk()
top.geometry("200x250")
menubutton = Menubutton(top, text = "Language", relief = FLAT)
menubutton.grid()
menubutton.menu = Menu(menubutton)
menubutton["menu"]=menubutton.menu
menubutton.menu.add_checkbutton(label = "Hindi", variable=IntVar())
menubutton.menu.add_checkbutton(label = "English", variable = IntVar())
menubutton.pack()
top.mainloop()
输出: