Tkinter Menubutton(メニューボタン)

4037 ワード

Python-Tkinter Menubutton:メニューボタンはドロップダウンメニューで、画面に滞在する時間の一部です.メニューのウィジェットは、ユーザが各menubuttonをクリックすると、そのメニューボタンの選択を表示することができる.
 
1つのメニューボタンは、画面に滞在する時間の一部であるドロップダウンメニューです.メニューのウィジェットは、ユーザが各menubuttonをクリックすると、そのメニューボタンの選択を表示することができる.
構文:
このwidgetを作成する簡単な構文です.
  w = Menubutton ( master, option, ... )

パラメータ:
  • master:これは親ウィンドウを表します.
  • options:このユーティリティで最も一般的なオプションのリストです.これらのオプションは、キー値のペアとしてカンマで区切ることができる.

  • Option
    Description
    activebackground
    The background color when the mouse is over the menubutton.
    activeforeground
    The foreground color when the mouse is over the menubutton.
    anchor
    This options controls where the text is positioned if the widget has more space than the text needs. The default is anchor=CENTER, which centers the text.
    bg
    The normal background color displayed behind the label and indicator.
    bitmap
    To display a bitmap on the menubutton, set this option to a bitmap name.
    bd
    The size of the border around the indicator. Default is 2 pixels.
    cursor
    The cursor that appears when the mouse is over this menubutton.
    direction
    Set direction=LEFT to display the menu to the left of the button; use direction=RIGHT to display the menu to the right of the button; or use direction='above' to place the menu above the button.
    disabledforeground
    前景色がこのメニューボタンに表示すると無効になります.
    fg
    The foreground color when the mouse is not over the menubutton.
    height
    The height of the menubutton in lines of text (not pixels!). The default is to fit the menubutton's size to its contents.
    highlightcolor
    Color shown in the focus highlight when the widget has the focus.
    image
    To display an image on this menubutton,
    justify
    This option controls where the text is located when the text doesn't fill the menubutton: use justify=LEFT to left-justify the text (this is the default); use justify=CENTER to center it, or justify=RIGHT to right-justify.
    menu
    To associate the menubutton with a set of choices, set this option to the Menu object containing those choices. That menu object must have been created by passing the associated menubutton to the constructor as its first argument.
    padx
    左と右のメニューボタンのテキストがどれだけ残っているか.デフォルトは1です.
    pady
    How much space to leave above and below the text of the menubutton. Default is 1.
    relief
    Selects three-dimensional border shading effects. The default is RAISED.
    state
    Normally, menubuttons respond to the mouse. Set state=DISABLED to gray out the menubutton and make it unresponsive.
    text
    To display text on the menubutton, set this option to the string containing the desired text. Newlines ("") within the string will cause line breaks.
    textvariable
    You can associate a control variable of class StringVar with this menubutton. Setting that control variable will change the displayed text.
    underline
    Normally, no underline appears under the text on the menubutton. To underline one of the characters, set this option to the index of that character.
    width
    The width of the widget in characters. The default is 20.
    wraplength
    Normally, lines are not wrapped. You can set this option to a number of characters and all lines will be broken into pieces no longer than that number.
    例:
    以下の例を試してみます.
    from Tkinter import *
    import tkMessageBox
    import Tkinter
    
    top = Tk()
    
    mb=  Menubutton ( top, text="condiments", relief=RAISED )
    mb.grid()
    mb.menu  =  Menu ( mb, tearoff = 0 )
    mb["menu"]  =  mb.menu
        
    mayoVar  = IntVar()
    ketchVar = IntVar()
    
    mb.menu.add_checkbutton ( label="mayo",
                              variable=mayoVar )
    mb.menu.add_checkbutton ( label="ketchup",
                              variable=ketchVar )
    
    mb.pack()
    top.mainloop()
    

    これにより、次の結果が得られます.
    転載先:https://www.cnblogs.com/tkinter/p/5628858.html