目錄
前言
Button小部件是一個標准的Tkinter的控件,用於實現各種按鈕。按鈕可以包含文本或圖像,您可以調用Python函數或方法用於每個按鈕。Tkinter的按鈕被按下時,會自動調用該函數或方法
(一)基本用法和可選屬性
1.基本用法
基本用法:Button(根對象, [屬性列表])
根對象:在那個窗體顯示,例如主窗體。
屬性列表:是可選的屬性=屬性值組成。
2.可選屬性
屬性 | 說明 |
---|---|
text | 標簽顯示的文本 |
font | 設置文本的字體和大小 |
fg(foreground) | 字體的顏色, |
bg (background) | 標簽的背景色 |
width | 標簽的寬度(一個中文的字體寬為單位) |
height | 標簽的高度(一個中文的字體高為單位) |
cursor | 鼠標的樣式 |
command | 綁定事件 |
padx | 文字到邊框的距離,水平方向 |
pady | 文字到邊框的距離,垂直方向 |
bd(borderwidth) | 邊框的寬度 |
relief | 邊框的樣式 |
justify | 文本對齊方式 |
image | 圖片 |
compound | 圖片與文字的混搭 |
anchor | 方位 |
(二)屬性的具體實現和案例
1.常用屬性
(1)font
font:設置字體與字體的大小
用法:font=("字體名",大小) 例如:font=(“黑體”, 20)
(2)fg 與 bg
fg 前景色,也就是字體的顏色,bg 背景顏色
用法:fg="red", fg="#121234"
(3)width 與 height
width height 標簽的寬度與高度,都是以系統默認的中文的一個字體寬高為單位
用法:width = 5, height=2
案例一
(1)源代碼
import tkinter as tk
win = tk.Tk()
# 普通的按鈕
button1 = tk.Button(win, text="Button1")
button1.pack()
# 背景色與前景色
button2 = tk.Button(win, text="Button2", bg="green", fg="blue")
button2.pack()
# 寬度與高度
button3 = tk.Button(win, text="Button3", width=10, height=2)
button3.pack()
# 邊距
button4 = tk.Button(win, text="Button4", padx=10, pady=10)
button4.pack()
win.mainloop()
(2)輸出效果
2.按鈕里的圖片
(1)只放圖片,沒有文字
需要先導入圖片的路徑:img1 = tk.PhotoImage(file="image/01.png")
再使用:image=img1
注:目前支持 .png 與 .gif 格式, 還不支持 .jpg格式,Button的大小是根據圖片的大小來確定的。
案例二
(1)源代碼:
import tkinter as tk
win = tk.Tk()
img1 = tk.PhotoImage(file="image/01.png")
img2 = tk.PhotoImage(file="image/03.png")
img3 = tk.PhotoImage(file="image/04.png")
# 300像素大小的圖片
button1 = tk.Button(win, text="Button1", image=img1)
button1.pack()
# 150像素大小的圖片
button2 = tk.Button(win, image=img2)
button2.pack()
# 50像素大小的圖片
button3 = tk.Button(win, image=img3)
button3.pack()
win.mainloop()
(2)輸出效果:
(3)圖片與文字混搭
需要使用:compound="對齊方式",
對齊方式有:'left', "right", "center"
案例三
(1)源代碼
import tkinter as tk
win = tk.Tk()
img1 = tk.PhotoImage(file="image/01.png")
img2 = tk.PhotoImage(file="image/03.png")
img3 = tk.PhotoImage(file="image/04.png")
button1 = tk.Button(win, text="Button1", image=img1, compound="left")
button1.pack()
button2 = tk.Button(win, text="Button2", image=img2, compound="center")
button2.pack()
button3 = tk.Button(win, text="Button3", image=img3, compound="right")
button3.pack()
win.mainloop()
(2)輸出效果
3.鼠標的樣式
cursor="鼠標的屬性值"
pencil:筆型
circle:圓形
hand1:手型1
hand2:手型2
案例四
(1)源代碼
import tkinter as tk
win = tk.Tk()
# 筆型
button1 = tk.Button(win, text="Button1", cursor="pencil")
button1.pack()
# 圓形
button2 = tk.Button(win, text="Button2", cursor="circle")
button2.pack()
# 手型1
button3 = tk.Button(win, text="Button3", cursor="hand1")
button3.pack()
# 手型2
button4 = tk.Button(win, text="Button4", cursor="hand2")
button4.pack()
win.mainloop()
(2)輸出效果
當我們把鼠標放在按鈕上時,鼠標的形狀會顯示不同的樣式。
4.邊框樣式
relief= "邊框樣式值"
flat 無邊框
groove 中間凹
ridge 中間凸
raised 往中間凸
solid 往中間凹
sunken 不可以
案例五
(1)源代碼
import tkinter as tk
win = tk.Tk()
# flat 無邊框
button1 = tk.Button(win, text="flat", relief="flat", bd=10)
button1.pack()
# groove 中間凹
button2 = tk.Button(win, text="groove", relief="groove", bd=10)
button2.pack()
# ridge 中間凸
button3 = tk.Button(win, text="raised", relief="ridge", bd=10)
button3.pack()
# raised 往中間凸
button4 = tk.Button(win, text="ridge", relief="raised", bd=10)
button4.pack()
# solid 往中間凹
button5 = tk.Button(win, text="solid", relief="solid", bd=10)
button5.pack()
# sunken 不可以
button6 = tk.Button(win, text="sunken", relief="sunken", bd=10)
button6.pack()
win.mainloop()
(2)輸出效果
(三)按鈕的事件綁定
1.普通的Button綁定事件
(1)說明:
Button 使用 command=功能函數 來綁定
Button(win, text="確定", command=功能函數)
案例六
(1)源代碼:
我們創建一個簡單的窗體,只有一個按鈕控件,
我們綁定的事件是,當我們點擊"確定"按鈕時,會輸出“你點擊了按鈕”
import tkinter as tk
win = tk.Tk()
# 定義功能函數, event是必須添加的參數,不知道來自哪里
def button_command():
print("你點擊了按鈕")
# 綁定事件
btn = tk.Button(win, text="確定", command=button_command)
btn.place(relx=0.2, rely=0.2, relwidth=0.5, relheight=0.1)
win.geometry("300x300+200+200")
win.mainloop()
(2)輸出效果:
2.傳參數Button綁定事件
(1)說明:
我們使用Button傳遞數值時,需要用:
lambda: 功能函數(var1, var2, ……)
案例七
(1)源代碼:
我們同樣創建一個簡單的窗體,只有一個控件按鈕
我們綁定的事件是,當我們點擊按鈕時,會傳入兩個參數,並在功能函數進行計算。
import tkinter as tk
"""
Button command 傳值事件
command= lambda: function(var1, var2, ...)
"""
def sum_fun(a, b):
result = a + b
print("%d + %d = %d" % (a, b, result))
win = tk.Tk()
button = tk.Button(win, text="傳值事件", command=lambda: sum_fun(12, 13))
button.pack()
win.geometry("300x300+200+200")
win.mainloop()