Button簡介
功能按鈕
也可稱作按鈕
,在窗口組件中可以設計,在單擊功能按鈕時執行某一個特定的動作,這個動作也稱爲callback
方法,也就是說我們可以將功能按鈕當做用戶與程序間溝通的橋樑。
按鈕上面也可以有文字,或是和標籤一樣可以有圖像,如果是文字樣式的功能按鈕,可以設定此文字的字形
它的語法格式:
Botton(父對象, options,...)
參數:
- 第一個參數:
父對象
,表示這個功能按鈕將建立在哪一個窗口內 - 第二個參數:
options
,參數如下
參數 | 含義 |
---|---|
borderwidth | 邊界寬度 默認是兩個像素 |
bd | 邊界寬度 默認是兩個像素 |
background | 背景色彩 |
bg | 背景色彩 |
command | 單機功能按鈕時,執行此方法 |
cursor | 當鼠標光標移至按鈕上時的形狀 |
foreground | 前景色彩 |
fg | 前景色彩 |
font | 字形 |
height | 高,單位是字符高 |
highlightbackground | 當功能按鈕獲取焦點時的背景顏色 |
highlightcolor | 當工人按鈕取得焦點時的顏色 |
image | 功能按鈕上的圖像 |
justify | 當有多行文字時,最後一行文字的對齊方式 |
padx | 可設置功能按鈕與文字的間隔 默認是1 |
pady | 可設置功能按鈕的上下間距 默認是1 |
relief | 可由此控制文字外框 默認是relief=FLAT |
state | 若設置爲DISABLED,則以灰階顯示功能按鈕,表示暫時無法使用 默認是state=NORMAL |
text | 功能按鈕名稱 |
underline | 可以設置第幾個文字有下劃線 從0開始計算,默認是-1,表示無下劃線 |
width | 寬,單位是字符寬 |
wraplength | 限制每行的文字數 默認是0,表示只有 "\n" 纔會換行 |
Button的基本應用
例子
:單擊按鈕顯示字符串
import tkinter
"""這裏我們相當於後續添加了label的屬性"""
def show():
label["text"] = "students"
label["bg"] = "lightblue"
label["fg"] = "red"
root = tkinter.Tk()
"""這裏我們先創建一個空的laber標籤"""
label = tkinter.Label(root)
"""command調用函數時只需要寫函數的名字,不需要加括號"""
button = tkinter.Button(root, text="打印", command=show)
label.pack()
button.pack()
root.mainloop()
運行結果:
當然,我們也可以使用config
的方式來後續添加屬性
例子
:改用config
的方式來添加
import tkinter
def show():
label.config(text="python!!!", bg="lightblue", fg="red")
root = tkinter.Tk()
label = tkinter.Label(root)
button = tkinter.Button(root, text="打印", command=show)
label.pack()
button.pack()
root.mainloop()
運行結果:
這裏我們可以進一步優化,我們可以使用root.destroy來退出程序
例子
:
import tkinter
def show():
label.config(text="python!!!", bg="lightblue", fg="red")
root = tkinter.Tk()
label = tkinter.Label(root)
button = tkinter.Button(root, text="打印", width=15, command=show)
"""同理,這裏的destroy也不需要加括號"""
buttonBye = tkinter.Button(root, text="退出", width=15, command=root.destroy)
label.pack()
"""這裏的side=tkinter.LEFT相當於css裏面的浮動,意思就是從左邊開始排序"""
button.pack(side=tkinter.LEFT)
buttonBye.pack(side=tkinter.LEFT)
root.mainloop()
運行結果:
這裏解釋一下,root.destroy
可以關閉root窗口對象,同時程序結束
另一個常用的方法是quit
,可以讓Python Shell
執行的程序結束,但是root
窗口則繼續執行
例子:使用定時器功能,添加結束按鈕
import tkinter
count = 1
def show(aa):
def counting():
global count
count += 1
aa.config(text=str(count))
aa.after(1000, counting)
counting()
root = tkinter.Tk()
label = tkinter.Label(root, bg="yellow", fg="red", height=3, width=15)
buttonBye = tkinter.Button(root, text="退出", width=15, command=root.destroy)
label.pack()
show(label)
buttonBye.pack(side=tkinter.LEFT)
root.mainloop()
運行結果:
例子
:按按鈕變顏色
import tkinter
def yellow():
root.config(bg="lightyellow")
def blue():
root.config(bg="lightblue")
root = tkinter.Tk()
root.geometry("300x200")
exitbutton = tkinter.Button(root, text="Exit", command=root.destroy)
bluebutton = tkinter.Button(root, text="Blue", command=blue)
yellowbutton = tkinter.Button(root, text="Yellow", command=yellow)
exitbutton.pack(anchor=tkinter.S, side=tkinter.RIGHT, padx=5, pady=5)
bluebutton.pack(anchor=tkinter.S, side=tkinter.RIGHT, padx=5, pady=5)
yellowbutton.pack(anchor=tkinter.S, side=tkinter.RIGHT, padx=5, pady=5)
root.mainloop()
運行結果:
使用Lambda 表達式
我們可以用Lambda
表達式來添加參數
例子
:改良計時器,觸發開啓計時功能
import tkinter
count = 1
def show(aa):
def counting():
global count
count += 1
aa.config(text=str(count))
aa.after(1000, counting)
counting()
root = tkinter.Tk()
label = tkinter.Label(root, bg="yellow", fg="red", height=3, width=15)
button = tkinter.Button(root, text="開始", width=15, command=lambda: show(label))
buttonBye = tkinter.Button(root, text="退出", width=15, command=root.destroy)
label.pack()
button.pack(side=tkinter.LEFT)
buttonBye.pack(side=tkinter.LEFT)
root.mainloop()
運行結果:
例子
:改良變顏色按鈕,簡化代碼
import tkinter
def color(bgcolor):
root.config(bg=bgcolor)
root = tkinter.Tk()
root.geometry("300x200")
exitbutton = tkinter.Button(root, text="Exit", command=root.destroy)
bluebutton = tkinter.Button(root, text="Blue", command=lambda: color("lightblue"))
yellowbutton = tkinter.Button(root, text="Yellow", command=lambda: color("lightyellow"))
exitbutton.pack(anchor=tkinter.S, side=tkinter.RIGHT, padx=5, pady=5)
bluebutton.pack(anchor=tkinter.S, side=tkinter.RIGHT, padx=5, pady=5)
yellowbutton.pack(anchor=tkinter.S, side=tkinter.RIGHT, padx=5, pady=5)
root.mainloop()
運行結果:
建立含圖像的功能按鈕
一般功能按鈕是用文字當按鈕名稱,其實也可以用圖像
當做按鈕名稱,若是使用圖像當按鈕名稱,在Button()
內可以省略text
參數設置按鈕名稱,但是要在Button()
內要增加image
參數設置圖像對象
例子
:使用圖像按鈕
import tkinter
def show():
label.config(text="Python!!!", bg="lightyellow", fg="red")
root = tkinter.Tk()
img = tkinter.PhotoImage(file="1.gif")
label = tkinter.Label(root)
button = tkinter.Button(root, image=img, command=show)
label.pack()
button.pack()
root.mainloop()
運行結果:
圖像與文字並存的功能按鈕
在設計功能按鈕時,若是想要讓圖像和文字並存於功能按鈕內,需要在Button()
內增加參數"compound=參數"
,參數如下
參數 | 含義 |
---|---|
LEFT | 左 |
TOP | 上 |
RIGHT | 右 |
BOTTOM | 下 |
CENTER | 中央 |
例子
:將圖像放在文字上方
import tkinter
def show():
label.config(text="Python!!!", bg="lightyellow", fg="red")
root = tkinter.Tk()
img = tkinter.PhotoImage(file="1.gif")
label = tkinter.Label(root)
button = tkinter.Button(root, image=img, text="點我", compound=tkinter.TOP, command=show)
label.pack()
button.pack()
root.mainloop()
運行結果:
例子
:將圖像與文字重疊
button = tkinter.Button(root, image=img, text="點我", fg="red", compound=tkinter.CENTER, command=show)
運行結果:
簡易計算器按鈕佈局的應用
import tkinter
root = tkinter.Tk()
label = tkinter.Label(root, text="", bg="lightyellow", width=28)
btn7 = tkinter.Button(root, text="7", width=3)
btn8 = tkinter.Button(root, text="8", width=3)
btn9 = tkinter.Button(root, text="9", width=3)
btnRemaining = tkinter.Button(root, text="%", width=3)
btnSquare = tkinter.Button(root, text="√", width=3)
btn4 = tkinter.Button(root, text="4", width=3)
btn5 = tkinter.Button(root, text="5", width=3)
btn6 = tkinter.Button(root, text="6", width=3)
btnx = tkinter.Button(root, text="*", width=3)
btnDivision = tkinter.Button(root, text="÷", width=3)
btn1 = tkinter.Button(root, text="1", width=3)
btn2 = tkinter.Button(root, text="2", width=3)
btn3 = tkinter.Button(root, text="3", width=3)
btnAdd = tkinter.Button(root, text="+", width=3)
btnReduction = tkinter.Button(root, text="-", width=3)
btnZero = tkinter.Button(root, text="0", width=3)
btnDoubleZero = tkinter.Button(root, text="-", width=3)
btnD = tkinter.Button(root, text=".", width=3)
btnEqual = tkinter.Button(root, text="=", width=9)
label.grid(row=0, column=0, columnspan=5)
btn7.grid(row=1, column=0, padx=5)
btn8.grid(row=1, column=1, padx=5)
btn9.grid(row=1, column=2, padx=5)
btnRemaining.grid(row=1, column=3, padx=5)
btnSquare.grid(row=1, column=4, padx=5)
btn4.grid(row=2, column=0, padx=5)
btn5.grid(row=2, column=1, padx=5)
btn6.grid(row=2, column=2, padx=5)
btnx.grid(row=2, column=3, padx=5)
btnDivision.grid(row=2, column=4, padx=5)
btn1.grid(row=3, column=0, padx=5)
btn2.grid(row=3, column=1, padx=5)
btn3.grid(row=3, column=2, padx=5)
btnAdd.grid(row=3, column=3, padx=5)
btnReduction.grid(row=3, column=4, padx=5)
btnZero.grid(row=4, column=0, padx=5)
btnDoubleZero.grid(row=4, column=1, padx=5)
btnD.grid(row=4, column=2, padx=5)
btnEqual.grid(row=4, column=3, padx=5, columnspan=2)
root.mainloop()
運行結果:
設計鼠標光標在功能按鈕上的形狀
import tkinter
root = tkinter.Tk()
label = tkinter.Label(root, text="Python!!!")
button = tkinter.Button(root, text="Button", cursor="star")
label.pack()
button.pack()
root.mainloop()
運行結果:
謝謝觀看,筆者會持續更新,如有錯誤或者建議,請私信我