一、參數說明
| 語法 | 作用 |
|---|---|
| Button(root,text='xxxx') | 按鈕圖標顯示內容 |
| Button(root,text='xxxx',height=2) | 組件的高度(所占行數) |
| Button(root,text='xxxx',width=20) | 組件的寬度(所占字符個數) |
| Button(text='xxxx',fg='blue') | 按鈕字體顏色 |
| Button(root,text='xxxx',activebackground='grey') | 按鈕按下時的前景字體顏色 |
| Button(root,text=‘xxxxx’,justify=tk.LEFT) | 多行文本的對齊方式 |
| Button(root,text='xxxx',pady=10) | 文本上下兩側的空格數 |
| Button(root,text='xxxx',padx=10) | 文本左右兩側的空格數(默認為1) |
| Button(root,text='xxxx',command=函數) | 按鈕觸發執行的命令(函數) |
| Button(root,text='xxxx',state=tk.DISABLED) | 按鈕禁止點擊 |
| Button(root,text='xxxx',underline=1) | 文字下划線(默認為-1) |
| Button(root,text='xxxx',author=tk.CENTER) | 文字位置 |
| Button(root,text='xxxx',textvariable=var) | 動態設置文本 |
二、代碼示例
import tkinter as tk
window = tk.Tk()
def main():
global window
# 設置主窗體大小
winWidth = 600
winHeight = 400
# 獲取屏幕分辨率
screenWidth = window.winfo_screenwidth()
screenHeight = window.winfo_screenheight()
# 計算主窗口在屏幕上的坐標
x = int((screenWidth - winWidth)/ 2)
y = int((screenHeight - winHeight) / 2)
# 設置主窗口標題
window.title("Button參數說明")
# 設置主窗口大小
window.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y))
# 設置窗口寬高固定
window.resizable(0,0)
# 設置窗口圖標
window.iconbitmap("./image/icon.ico")
""" button參數列表
STANDARD OPTIONS
activebackground, activeforeground, anchor,
background, bitmap, borderwidth, cursor,
disabledforeground, font, foreground
highlightbackground, highlightcolor,
highlightthickness, image, justify,
padx, pady, relief, repeatdelay,
repeatinterval, takefocus, text,
textvariable, underline, wraplength
WIDGET-SPECIFIC OPTIONS
command, compound, default, height,
overrelief, state, width
"""
var = tk.StringVar()
var.set("提交")
tk.Button(window, text="提交", width=30, pady=5,fg="#f00", bg="#bbb",
borderwidth=0, activeforeground="yellow", activebackground="#666"
,underline=-1, command=click, textvariable=var).pack()
window.mainloop()
def click():
print("click")
if __name__ == '__main__':
main()
三、效果圖

