基本用法如下:

import tkinter as tk

root = tk.Tk()

filemenu = tk.Menu(root) #創建選單欄

root.config(menu=filemenu) #綁定選單

filemenu.add_command(label='新增') #新增選單項目一

filemenu.add_command(label='存檔') #新增選單項目二

root.mainloop()

在選單欄中新增子選單:

import tkinter as tk

root = tk.Tk()

filemenu = tk.Menu(root)  #創建主要選單欄

root.config(menu=filemenu) #綁定主要選單

menu1 = tk.Menu(filemenu) #創建子選單欄綁在父容器下

menu2 = tk.Menu(filemenu)

menu1.add_command(label='打開檔案') #新增子選單1內的項目一

menu1.add_command(label='關閉檔案') #新增子選單1內的項目二

menu2.add_command(label='複製') #新增子選單2內的項目一

menu2.add_command(label='刪除') #新增子選單2內的項目二

filemenu.add_cascade(label='檔案', menu=menu1) #命名父選單第一欄的名稱, 並綁定子選單1所有項目

filemenu.add_cascade(label='編輯', menu=menu2) #命名父選單第二欄的名稱, 並綁定子選單2所有項目

filemenu.add_cascade(label='說明') #命名父選單第三欄的名稱

root.mainloop()

移除子選單的虛線:

menu1 = tk.Menu(filemenu, tearoff=0) #在子選單後面設置tearoff=0即可

 

基本結構:

menu = Menu ( master, option, ... )

tkinter提供Menu共15個參數如下所示:

1. activebackground: The background color that will appear on a choice when it is under the mouse.

2. activeborderwidth: Specifies the width of a border drawn around a choice when it is under the mouse. Default is 1 pixel.

3. activeforeground: The foreground color that will appear on a choice when it is under the mouse.

4. bg: The background color for choices not under the mouse.

5. bd: The width of the border around all the choices. Default is 1.

6. cursor: The cursor that appears when the mouse is over the choices, but only when the menu has been torn off.

7. disabledforeground: The color of the text for items whose state is DISABLED.

8. font: The default font for textual choices.

9. fg: The foreground color used for choices not under the mouse.

10. postcommand: You can set this option to a procedure, and that procedure will be called every time someone brings up this menu.

11. relief: The default 3-D effect for menus is relief=RAISED.

12. image: To display an image on this menubutton.

13. selectcolor: Specifies the color displayed in checkbuttons and radiobuttons when they are selected.

14. tearoff: Normally, a menu can be torn off, the first position (position 0) in the list of choices is occupied by the tear-off element, and the additional choices are added starting at position 1. If you set tearoff=0, the menu will not have a tear-off feature, and choices will be added starting at position 0.

15. title: Normally, the title of a tear-off menu window will be the same as the text of the menubutton or cascade that lead to this menu. If you want to change the title of that window, set the title option to that string.

 

Menu提供12個呼叫函式(方法)如下所示:

1. add_command(options): Adds a menu item to the menu.

2. add_radiobutton(options): Creates a radio button menu item.

3. add_checkbutton(options): Creates a check button menu item.

4. add_cascade(options): Creates a new hierarchical menu by associating a given menu to a parent menu

5. add_separator(): Adds a separator line to the menu.

6. add(type, options): Adds a specific type of menu item to the menu.

7. delete([startindex , endindex]): Deletes the menu items ranging from startindex to endindex.

8. entryconfig(index, options): Allows you to modify a menu item, which is identified by the index, and change its options.

9. index(item): Returns the index number of the given menu item label.

10. insert_separator (index): Insert a new separator at the position specified by index.

11. invoke (index): Calls the command callback associated with the choice at position index. If a checkbutton, its state is toggled between set and cleared; if a radiobutton, that choice is set.

12. type(index): Returns the type of the choice specified by index: either "cascade", "checkbutton", "command", "radiobutton", "separator", or "tearoff".

 

 

 

※查詢其他Tkinter GUI元件用法如下※

http://jennaweng0621.pixnet.net/blog/category/6758171

arrow
arrow
    文章標籤
    python tkinter menu
    全站熱搜

    楓綺 發表在 痞客邦 留言(0) 人氣()