Tkinter 버튼을 클릭하고 명령을 실행하면 명령이 반환 될 때까지 GUI가 정지 된 것처럼 보입니다.
예를 들어 카운터는 2 초 후에 업데이트됩니다.
import tkinter as tk
import time
class Window():
def __init__(self):
self.clicks = 0
self.root = tk.Tk()
self.button_text = tk.StringVar(value="Click " + str(self.clicks))
self.button = tk.Button(self.root, textvariable=self.button_text,
command=self.click)
self.button.pack()
def click(self):
self.clicks += 1
self.button_text.set("Click " + str(self.clicks))
time.sleep(2)
if __name__ == '__main__':
Window().root.mainloop()
콜백 중에 창을 업데이트 할 수있는 방법이 있습니까?
버튼의 update_idletasks
메서드를 호출 할 수 있습니다 .
def click(self):
self.clicks += 1
self.button_text.set("Click " + str(self.clicks))
##################################
self.button.update_idletasks()
##################################
time.sleep(2)
해당 줄을에 추가 click
하면 버튼의 텍스트가 즉시 업데이트됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다