완료하는 데 다소 시간이 걸리는 프로세스가 있습니다. 필자가 pygtk GUI에서 이것을 호출하면 약 10 초 후에 창이 잠기고 (어둡게되어 사용자 행동을 막는다).pygtk GUI가 장시간 실행되는 프로세스에서 잠기는 것을 막으십시오.
나는 이런 일이 발생하지 않도록하고 싶지만, 어떻게해야할지 모르겠다. 나는 멀티 스레딩이 답이라고 생각했지만, 작동하지 않는 것 같습니다. 나는 온라인에서 찾은 두 가지 방법을 시도했다. 먼저, 장기 실행 기능을 사용하기 위해 this FAQ을 수정했습니다. 둘째로 나는 this answer 에서처럼 threading.Thread를 직접 사용해 보았습니다.하지만 역시 잠겨 있습니다.
두 개의 샘플이 아래에 있습니다. 나는 멀티 쓰레딩에 익숙하지 않기 때문에 내가 원하는 해결책이 아닐 수도있다. 나는 기본적으로 GUI가 잠금 상태가되지 않게하여 진행률 막대로 업데이트하고 사용자가 취소 버튼을 사용할 수있게하려고합니다.
#Sample 1
import threading
import time
import gobject
import gtk
gobject.threads_init()
class MyThread(threading.Thread):
def __init__(self, label, button):
super(MyThread, self).__init__()
self.label = label
self.button = button
self.counter = 0
button.connect("clicked", self.on_button_click)
self.quit = False
def update_label(self, counter):
self.label.set_text("Counter: %i" % counter)
time.sleep(20)
return False
def on_button_click(self, widget):
self.counter += 1
gobject.idle_add(self.update_label, self.counter)
window = gtk.Window()
label = gtk.Label()
box = gtk.VBox()
button = gtk.Button("Test")
box.pack_start(label)
box.pack_start(button)
window.add(box)
window.show_all()
window.connect("destroy", lambda _: gtk.main_quit())
thread = MyThread(label, button)
thread.start()
gtk.main()
thread.quit = True
#####################################
#Sample 2
from threading import Thread
import time
import gobject
import gtk
class Test():
def __init__(self):
self.counter = 0
self.label = gtk.Label()
button = gtk.Button("Test")
window = gtk.Window()
box = gtk.VBox()
box.pack_start(self.label)
box.pack_start(button)
window.add(box)
window.connect("destroy", lambda _: gtk.main_quit())
button.connect("clicked", self.on_button_click)
window.show_all()
def update_label(self, counter):
self.label.set_text("Counter: %i" % counter)
time.sleep(20)
return False
def on_button_click(self, widget):
self.counter += 1
thread = Thread(target=self.update_label, args=(self.counter,))
thread.start()
while thread.is_alive():
pass
thread.stop()
test = Test()
gtk.main()
좋습니다. 왜 스레드의 상태를 지속적으로 폴링 할 수 없는지 이해합니다. 하지만 스레드 종료 시점을 알아야합니다. 스레드의 진행 상태에 대한 상태 표시 줄을 추가하면 상태 표시 줄을 중지 할 때를 어떻게 알 수 있습니까? –
예제에 진행률 표시 줄을 추가했습니다. 'glib.timeout_add'를 사용하면 응용 프로그램이 응답하지 않는 상태에서 스레드 상태를 폴링 할 수 있습니다. – jcollado
좋습니다. 이것은 의미가 있습니다. 나는'glib.timeout_add'에 대해 몰랐다. 당신의 도움을 주셔서 감사합니다. –