Tkinter รองรับการจัดสไตล์ปุ่มได้หลายแบบ ผ่าน ttk.Style() บทความนี้แสดง 2 แบบ: จัดสไตล์เฉพาะปุ่มบางตัว และจัดสไตล์ทุกปุ่มในหน้าต่าง
1. จัดสไตล์เฉพาะปุ่มที่ต้องการ
สร้างสไตล์ชื่อ 'W.TButton' แล้วระบุในพารามิเตอร์ style ของปุ่มที่ต้องการ
ตัวอย่างโค้ด
from tkinter import *
from tkinter.ttk import *
canv = Tk()
canv.geometry('200x150')
# สร้าง Style object
sto = Style()
# กำหนดสไตล์ 'W.TButton'
sto.configure('W.TButton', font=('Arial', 10, 'underline'), foreground='Green')
# ปุ่มที่ 1: ใช้สไตล์ W.TButton
btns = Button(canv, text='Welcome!', style='W.TButton', command=canv.destroy)
btns.grid(row=0, column=1, padx=50)
# ปุ่มที่ 2: ไม่ใช้สไตล์ (ปกติ)
btnns = Button(canv, text='Click to Start!', command=None)
btnns.grid(row=1, column=1, pady=10, padx=50)
canv.mainloop()
ผลลัพธ์

2. จัดสไตล์ทุกปุ่มในหน้าต่าง
ใช้ชื่อสไตล์ 'TButton' (ชื่อมาตรฐานของ ttk) จะมีผลกับทุกปุ่มอัตโนมัติ
ตัวอย่างโค้ด
from tkinter import *
from tkinter.ttk import *
canv = Tk()
canv.geometry('200x150')
sto = Style()
# กำหนดสไตล์ TButton (global)
sto.configure('TButton', font=('calibri', 10, 'bold', 'underline'), foreground='Green')
btns = Button(canv, text='Welcome!', style='TButton', command=canv.destroy)
btns.grid(row=0, column=1, padx=50)
btnns = Button(canv, text='Click to start!', command=None)
btnns.grid(row=1, column=1, pady=10, padx=50)
canv.mainloop()
ผลลัพธ์

สรุป: ใช้ 'W.TButton' (custom name) สำหรับปุ่มเฉพาะตัว ใช้ 'TButton' สำหรับปุ่มทั้งหมด สามารถสร้างสไตล์หลายชุดได้ตามต้องการ