ttk วิดเจ็ตที่มีธีมใน Tkinter ถูกนำมาใช้เพื่อออกแบบคุณสมบัติภายนอกและสไตล์ของวิดเจ็ตในแอปพลิเคชัน ttk ใช้ T cl/Tk ล่ามเพื่อให้ผู้ใช้เข้าถึงอินเทอร์เฟซที่มีคุณลักษณะและคุณลักษณะในตัวมากมายที่เป็นประโยชน์สำหรับวิดเจ็ตหรือแอปพลิเคชันใดๆ ทีนี้ถ้าเราเปรียบเทียบ Ttk ธีมที่มี Tcl ธีมมีหลากหลายรูปแบบ
โดยทั่วไปแล้ว Ttk จะสนับสนุนเพียงไม่กี่ธีมซึ่งมีดังต่อไปนี้ -
- ชนะรางวัล
- หอย
- alt
- ค่าเริ่มต้น
- คลาสสิก
- ทิวทัศน์
- xpnative
ในการเปลี่ยนธีมโดยรวมของแอปพลิเคชัน tkinter เราต้องใช้ฟังก์ชัน style.theme_use(theme_name) .
ตัวอย่าง
# Import the required libraries in tkinter
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
# Create an instance of tkinter frame or window
win = Tk()
# Set the size of tkinter window
win.geometry("700x350")
# Create an instance of ttk Style
style = ttk.Style()
# Configure the theme with style
style.theme_use('clam')
# Define a function to show the message
def display_msg():
messagebox.showinfo("Message", "You are learning Python Tkinter!")
# Add a Customized Label widget
label = ttk.Label(win, text="Hey Folks, I have a Message for You!", font=('Aerial 16'))
label.pack(pady=5)
# Add a Button widget
ttk.Button(win, text="Show Message", command=display_msg).place(x=285, y=98)
win.mainloop() ผลลัพธ์
การเรียกใช้โค้ดด้านบนจะเป็นการเปิดหน้าต่างพร้อมวิดเจ็ตป้ายกำกับและปุ่ม ธีมโดยรวมของแอปพลิเคชันถูกกำหนดโดย ttk วิดเจ็ตที่มีธีม เราสามารถกำหนดค่าธีมจากรายการธีมที่มีอยู่ในไลบรารี tkinter
