Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách cập nhật widget Button trong Tkinter

Có nhiều cách cập nhật Button trong Tkinter: thay đổi kích thước, màu nền, bỏ viền, v.v. Ví dụ dưới tạo 3 button, mỗi button khi click gọi hàm cập nhật tính năng riêng.

Ví dụ

from tkinter import *
from tkinter import ttk

win = Tk()
win.geometry("700x300")

# Hàm tăng kích thước button
def Button_Size():
    button1.configure(font=('Calibri 20 bold'))

# Hàm đổi màu nền
def Button_Color():
    button2.configure(bg='green')

# Hàm bỏ viền
def Button_Border():
    button3.configure(borderwidth=0)

# Button 1
button1 = Button(win, text="Increase the Button Size", command=Button_Size)
button1.pack(pady=20)

# Button 2
button2 = Button(win, text="Change the Background Color", command=Button_Color)
button2.pack(pady=20)

# Button 3
button3 = Button(win, text="Remove the Border", command=Button_Border)
button3.pack(pady=20)

win.mainloop()

Kết quả

Ban đầu hiển thị 3 button bình thường.

Cách cập nhật widget Button trong Tkinter

Click "Increase the Button Size" → font to, đậm.

Cách cập nhật widget Button trong Tkinter

Click "Change the Background Color" → nền xanh.

Cách cập nhật widget Button trong Tkinter

Click "Remove the Border" → mất viền.

Cách cập nhật widget Button trong Tkinter