Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีอัปเดต Button Widget ใน Tkinter

เราสามารถอัปเดต Button widget ใน Tkinter หลายวิธี เช่น เปลี่ยนขนาด เปลี่ยนสีพื้นหลัง หรือลบขอบ ตัวอย่างนี้สร้างปุ่ม 3 ปุ่ม แต่ละปุ่มเมื่อคลิกจะเรียกฟังก์ชันต่างกันเพื่ออัปเดตคุณสมบัติของตัวเอง

ตัวอย่าง

from tkinter import *
from tkinter import ttk

# สร้าง instance ของ tkinter frame
win = Tk()
win.geometry("700x300")

# ฟังก์ชันเพิ่มขนาดปุ่ม
def Button_Size():
    button1.configure(font=('Calibri 20 bold'))

# ฟังก์ชันเปลี่ยนสีพื้นหลัง
def Button_Color():
    button2.configure(bg='green')

# ฟังก์ชันลบขอบ
def Button_Border():
    button3.configure(borderwidth=0)

# ปุ่มแรก
button1 = Button(win, text="Increase the Button Size", command=Button_Size)
button1.pack(pady=20)

# ปุ่มที่สอง
button2 = Button(win, text="Change the Background Color", command=Button_Color)
button2.pack(pady=20)

# ปุ่มที่สาม
button3 = Button(win, text="Remove the Border", command=Button_Border)
button3.pack(pady=20)

win.mainloop()

ผลลัพธ์

หน้าต่างเริ่มต้น:

วิธีอัปเดต Button Widget ใน Tkinter

คลิก "Increase the Button Size":

วิธีอัปเดต Button Widget ใน Tkinter

คลิก "Change the Background Color":

วิธีอัปเดต Button Widget ใน Tkinter

คลิก "Remove the Border":

วิธีอัปเดต Button Widget ใน Tkinter