Tkinter มีฟีเจอร์สำหรับเพิ่ม Widget ต่างๆ ที่จำเป็นสำหรับแอปพลิเคชัน เช่น Button widget, Entry Widget, Text Box, Slider เป็นต้น ในบทความนี้ เราจะดูวิธีสร้างแอปพลิเคชันที่มีปุ่มสลับระหว่าง On และ Off
ในตัวอย่างนี้ เราจะใช้ปุ่ม 2 แบบสำหรับสาธิต:
Switch On
Switch Off
ตัวอย่าง
# Import tkinter in the notebook
from tkinter import *
# Create an instance of window of frame
win = Tk()
# set Title
win.title('On/Off Demonstration')
# Set the Geometry
win.geometry("600x400")
win.resizable(0, 0)
# Create a variable to turn on the button initially
is_on = True
# Create Label to display the message
label = Label(win, text="Night Mode is On", bg="white", fg="black", font=("Poppins bold", 22))
label.pack(pady=20)
# Define our switch function
def button_mode():
global is_on
# Determine it is on or off
if is_on:
on_.config(image=off)
label.config(text="Day Mode is On", bg="white", fg="black")
is_on = False
else:
on_.config(image=on)
label.config(text="Night Mode is On", fg="black")
is_on = True
# Define Our Images
on = PhotoImage(file="on.png")
off = PhotoImage(file="off.png")
# Create A Button
on_ = Button(win, image=on, bd=0, command=button_mode)
on_.pack(pady=50)
# Keep Running the window
win.mainloop()
ผลลัพธ์
การรันโค้ดข้างต้นจะสร้างปุ่มสำหรับสลับโหมด On/Off

หากคุณคลิกปุ่ม จะเปลี่ยนเป็นดังนี้:
