python自动化办公(十二)MenuButton控件:语法及其参数、demos

时间:2024-10-23 08:35:06

目录

一、MenuButton简介

二、MenuButton语法及其参数

Menu Buttons Options

三、案例1:MenuButton控件添加三个checkButton

 四、案例2:MenuButton控件添加三个radioButton,并输出其绑定的值

五、案例3:较新版的ttk库的MenuButton控件


一、MenuButton简介

  • MenuButton控件在被按下时,弹出下拉菜单。 下拉菜单里面可以放置radioButton或者checkButton。

二、MenuButton语法及其参数

Menu Button Syntax:

MenuBttn = Menubutton(master, option..... )

Menu Buttons Options

A complete list of options for the Tkinter Menu Button. Note however, that some will only work with the newer “ttk” version. A short segment on this is included at the end of the tutorial.

No. Option Description
1 activebackground Color of the background when widget is under focus.
2 activeforeground Color of the foreground when widget in under focus.
3 bg Background color for the area around the widget.
4 bd Size of the border around the widget. Default value is 2 pixels.
5 cursor When the mouse is hovering over this widget, it can be changed to a special cursor type like an arrow or dot.
6 direction Sets the direction of the text relative to the Menu button. Options are, LEFT, RIGHT and ‘above’.
7 fg The color for the text.
8 height The height of the widget in terms of text lines.
9 highlightcolor The text color when the widget is under focus.
10 image Used to display an image
11 justify Specifies how the text is aligned within the widget. One of “left”, “center”, or “right”.
12 menu Used to assign a menu object to the menu button. Allows for a list of options to be displayed.
13 padx Amount of padding in terms of pixels in the left-right directions.
14 pady Amount of padding in terms of pixels for the area above and below the widget.
15 relief It specifies the type of the border. Default is Flat, other options include RAISED and SUNKEN.
16 State Default is NORMAL. DISABLED causes the Widget to gray out and go inactive.
17 text The text displayed next on the Menu Button.
18 textvariable Allows us to assign a variable to the Menu Button. Text displayed on the Menu Button can be changed by manipulating this variable.
19 width Width of the widget in terms of number of characters.

三、案例1:MenuButton控件添加三个checkButton

  1. from tkinter import *
  2. root = Tk()
  3. ("200x150")
  4. frame = Frame(root)
  5. ()
  6. # 定义一个Menubutton,其实它就是控件,只不过这个控件点击会弹出菜单。
  7. # relief=RAISED定义Menubutton控件的外观样式
  8. MenuBttn = Menubutton(frame, text="Favourite food", relief=RAISED)
  9. ()
  10. Var1 = IntVar()
  11. Var2 = IntVar()
  12. Var3 = IntVar()
  13. # Menubutton控件定义一个菜单Menu1,点击Menubutton就可以弹出Menu1菜单
  14. Menu1 = Menu(MenuBttn, tearoff=0)
  15. MenuBttn["menu"] = Menu1
  16. # Menu1菜单里面定义三个checkButton控件
  17. Menu1.add_checkbutton(label="Pizza", variable=Var1)
  18. Menu1.add_checkbutton(label="Cheese Burger", variable=Var2)
  19. Menu1.add_checkbutton(label="Salad", variable=Var3)
  20. ()

GUI效果:

 四、案例2:MenuButton控件添加三个radioButton,并输出其绑定的值

  1. from tkinter import *
  2. def retrieve():
  3. print(())
  4. root = Tk()
  5. ("200x150")
  6. frame = Frame(root)
  7. ()
  8. MenuBttn = Menubutton(frame, text="Favourite food", relief=RAISED)
  9. Menu1 = Menu(MenuBttn, tearoff=0)
  10. MenuBttn["menu"] = Menu1
  11. Var1 = IntVar()
  12. Menu1.add_radiobutton(label="Pizza", variable=Var1, value=1, command=retrieve)
  13. Menu1.add_radiobutton(label="Cheese Burger", variable=Var1, value=2, command=retrieve)
  14. Menu1.add_radiobutton(label="Salad", variable=Var1, value=3, command=retrieve)
  15. ()
  16. ()

GUI效果:

五、案例3:较新版的ttk库的MenuButton控件

        较新的 ttk 库带来了一些更新的控件,它们的性能通常比原始控件更好。 它们提供了更大的灵活性并有更多的选择。 比如说ttk版本的MenuButton可以绑定变量textvariable。

         在下面的部分中,我们演示了如何执行此操作。

  1. from tkinter import *
  2. import tkinter
  3. from ttk import Menubutton
  4. def f():
  5. var.set("Food")
  6. root = Tk()
  7. var = StringVar()
  8. mb = Menubutton(root, textvariable=var)
  9. ()
  10. = Menu(mb, tearoff=0)
  11. mb["menu"] =
  12. b = Button(root, text="Click", command=f)
  13. ()
  14. mayoVar = IntVar()
  15. ketchVar = IntVar()
  16. .add_checkbutton(label="mayo", variable=mayoVar)
  17. .add_checkbutton(label="ketchup",variable=ketchVar)
  18. ()
  19. ()

 GUI效果: