Tkinter는 GUI 기반 응용 프로그램을 만드는 데 사용되는 Python 라이브러리입니다. 루프에 특정 기능이 정의된 기능적 응용 프로그램을 만들어야 한다고 가정해 보겠습니다. 재귀 함수는 레이블 위젯에 무한한 시간 동안 일부 텍스트를 표시합니다.
이 재귀 함수를 중지하기 위해 버튼을 클릭할 때마다 조건을 변경하는 함수를 정의할 수 있습니다. True 또는 False가 될 수 있는 전역 변수를 선언하여 조건을 변경할 수 있습니다.
예시
# Import the required library
from tkinter import *
# Create an instance of tkinter frame
win= Tk()
# Set the size of the Tkinter window
win.geometry("700x350")
# Define a function to print something inside infinite loop
run= True
def print_hello():
if run:
Label(win, text="Hello World", font= ('Helvetica 10 bold')).pack()
# After 1 sec call the print_hello() again
win.after(1000, print_hello)
def start():
global run
run= True
def stop():
global run
run= False
# Create buttons to trigger the starting and ending of the loop
start= Button(win, text= "Start", command= start)
start.pack(padx= 10)
stop= Button(win, text= "Stop", command= stop)
stop.pack(padx= 15)
# Call the print_hello() function after 1 sec.
win.after(1000, print_hello)
win.mainloop() 출력

이제 "중지" 버튼을 클릭할 때마다 함수 호출이 중지됩니다.