標籤:高度 map val out idg ror python函數 message bsp
Button小組件是一個標準的Tkinter的組件,用於實現各種按鈕。按鈕可以包含文本或映像,您可以調用Python函數或方法用於每個按鈕。 Tkinter的按鈕被按下時,會自動調用該函數或方法。
該按鈕可以只顯示在一個單一的字型的文本,但文本可能跨越一個以上的行。此外,一個字元可以有底線,例如標記的鍵盤快速鍵。預設情況下,使用Tab鍵可以移動到一個按鈕組件。
通常使用工具列按鈕,在應用程式視窗,並接受或解僱在對話方塊中輸入的資料。
Button按鈕屬性
函數 |
描述 |
text |
顯示常值內容 |
command |
指定Button的事件處理函數 |
compound |
指定文本與映像的位置關係 |
bitmap |
指定位元影像 |
focus_set |
設定當前組件得到的焦點 |
master |
代表了父視窗 |
bg |
設定背景顏色 |
fg |
設定前景顏色 |
font |
設定字型大小 |
height |
設定顯示高度、如果未設定此項,其大小以適應內容標籤 |
relief |
指定外觀裝飾邊界附近的標籤,預設是平的,可以設定的參數; flat、groove、raised、ridge、solid、sunken |
width |
設定顯示寬度,如果未設定此項,其大小以適應內容標籤 |
wraplength |
將此選項設定為所需的數量限制每行的字元,數預設為0 |
state |
設定組件狀態;正常(normal),啟用(active),禁用(disabled) |
anchor |
設定Button文本在控制項上的顯示位置 可用值:n(north),s(south),w(west),e(east),和ne,nw,se,sw |
bd |
設定Button的邊框大小;bd(bordwidth)預設為1或2個像素 |
textvariable |
設定Button與textvariable屬性 |
Button按鈕方法
以下是Button常用的小工具
方法 |
描述 |
flash() |
Flash the button. This method redraws the button several times, alternating between active and normal appearance. |
invoke() |
Invoke the command associated with the button. |
Python Tkinter Button範例程式碼
建立了4個Button按鈕、設定了不同的屬性
width,height,relief,bg,bd,fg,state,bitmap,command,anchor
from tkinter import *from tkinter import messageboxroot = Tk()root.title("Button Test")def callback(): messagebox.showinfo("Python command","人生苦短、我用Python")"""建立4個Button按鈕、並設定width,height,relief,bg,bd,fg,state,bitmap,command,anchor"""Button(root, text="外觀裝飾邊界附近的標籤", width=21,height=3,relief=RAISED).pack()Button(root, text="設定按鈕狀態",width=21,state=DISABLED).pack()Button(root, text="設定bitmap放到按鈕左邊位置", compound="left",bitmap="error").pack()Button(root, text="設定command事件調用命令", fg="blue",bd=7,width=28,command=callback).pack()Button(root, text ="設定高度寬度以及文字顯示位置",anchor = ‘sw‘,width = 30,height = 2).pack()root.mainloop()
View Code
python Tkinter之Button