Tkinter는 응용 프로그램에 필요한 다양한 종류의 위젯을 추가하는 기능을 제공합니다. 이러한 위젯 중 일부는 버튼 위젯, 항목 위젯, 텍스트 상자, 슬라이더 등입니다. 이 기사에서는 버튼을 켜거나 끌 수 있는 응용 프로그램을 만드는 방법을 살펴보겠습니다.
이 예에서는 데모용으로 이 두 버튼을 사용합니다.
-
켜기
-
끄기
예시
# 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 모드로 동작하는 Button이 생성됩니다.

버튼을 클릭하면 다음과 같이 변경됩니다 -
