Tkinter 選單



此部件的目標是允許我們建立各種可供應用程式使用的選單。核心功能提供建立三種菜單型別的方法:彈出式、頂級和下拉式。

也可以使用其他擴充套件部件來實現新型選單,例如OptionMenu部件,它實現了一種特殊的型別,在選擇中生成專案的彈出列表。

語法

以下是建立此部件的簡單語法:

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

引數

  • master −:這表示父視窗。

  • options − 以下是此部件最常用的選項列表。這些選項可以用逗號分隔的鍵值對使用。

序號 選項 & 描述
1

activebackground

當滑鼠懸停在選項上時將顯示的背景顏色。

2

activeborderwidth

指定當滑鼠懸停在選項上時圍繞選項繪製的邊框寬度。預設為 1 畫素。

3

activeforeground

當滑鼠懸停在選項上時將顯示的前景色。

4

Bg

滑鼠未懸停在選項上的背景顏色。

5

Bd

所有選項周圍邊框的寬度。預設為 1。

6

Cursor

當滑鼠懸停在選項上時出現的滑鼠游標,但僅限於選單已分離時。

7

disabledforeground

狀態為 DISABLED 的專案的文字顏色。

8

Font

文字選項的預設字型。

9

Fg

滑鼠未懸停在選項上時使用的前景色。

10

Postcommand

您可以將此選項設定為一個過程,並且每次有人開啟此選單時都會呼叫該過程。

11

Relief

選單的預設 3D 效果是 relief=RAISED。

12

Image

在此選單按鈕上顯示影像。

13

Selectcolor

指定選中複選框和單選按鈕時顯示的顏色。

14

Tearoff

通常,選單可以分離,選項列表中的第一個位置(位置 0)由分離元素佔用,其他選項從位置 1 開始新增。如果將 tearoff 設定為 0,則選單將沒有分離功能,並且選項將從位置 0 開始新增。

15

Title

通常情況下,彈出式選單視窗的標題與指向該選單的選單按鈕或級聯選單的文字相同。如果要更改視窗標題,請將title選項設定為該字串。

方法

這些方法適用於Menu物件:

序號 選項 & 說明
1

add_command (options)

向選單新增選單項。

2

add_radiobutton( options )

建立一個單選按鈕選單項。

3

add_checkbutton( options )

建立一個複選按鈕選單項。

4

add_cascade(options)

透過將給定選單關聯到父選單來建立一個新的分層選單。

5

add_separator()

向選單新增分隔線。

6

add( type, options )

向選單新增特定型別的選單項。

7

delete( startindex [, endindex ])

刪除從startindex到endindex範圍內的選單項。

8

entryconfig( index, options )

允許您修改由索引標識的選單項並更改其選項。

9

index(item)

返回給定選單項標籤的索引號。

10

insert_separator ( index )

在索引指定的位置插入新的分隔符。

11

invoke ( index )

呼叫與索引位置處的選項關聯的命令回撥。如果是複選按鈕,則其狀態在選中和未選中之間切換;如果是單選按鈕,則選中該選項。

12

type ( index )

返回索引指定的選項型別:“cascade”(級聯)、“checkbutton”(複選按鈕)、“command”(命令)、“radiobutton”(單選按鈕)、“separator”(分隔符)或“tearoff”(彈出式)。

示例

自己嘗試以下示例:

from tkinter import *
def donothing():
   filewin = Toplevel(root)
   button = Button(filewin, text="Do nothing button")
   button.pack()

root = Tk()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="New", command=donothing)
filemenu.add_command(label="Open", command=donothing)
filemenu.add_command(label="Save", command=donothing)
filemenu.add_command(label="Save as...", command=donothing)
filemenu.add_command(label="Close", command=donothing)

filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)
menubar.add_cascade(label="File", menu=filemenu)
editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Undo", command=donothing)
editmenu.add_separator()
editmenu.add_command(label="Cut", command=donothing)
editmenu.add_command(label="Copy", command=donothing)
editmenu.add_command(label="Paste", command=donothing)
editmenu.add_command(label="Delete", command=donothing)
editmenu.add_command(label="Select All", command=donothing)

menubar.add_cascade(label="Edit", menu=editmenu)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help Index", command=donothing)
helpmenu.add_command(label="About...", command=donothing)
menubar.add_cascade(label="Help", menu=helpmenu)

root.config(menu=menubar)
root.mainloop()

執行上述程式碼後,將產生以下結果:

tkinter menu
python_gui_programming.htm
廣告