2014-04-13 1 views
0

파이썬과 PyGObject로 간단한 프로그램을 작성합니다. 나는 그녀에게 ProgressBar를 통한 진행 상황을 사용자에게 알려주기를 바랍니다. 인터페이스가 보유하지 않을 때, 나는 기회 사용자를주고 싶습니다조치의 폐지를 구현하는 방법은 무엇입니까?

import time 
import threading 
from gi.repository import Gtk, GObject 

GObject.threads_init() 

def tread_function(): 
    progress.set_fraction(0) 
    time.sleep(5) 
    progress.set_fraction(0.25) 
    time.sleep(5) 
    progress.set_fraction(0.5) 
    time.sleep(5) 
    progress.set_fraction(0.75) 
    time.sleep(5) 
    progress.set_fraction(1) 


def clickOk(*args): 
    t = threading.Thread(target=tread_function) 
    t.start() 

def clickCancel(*args): 
    pass 

buttonOk = Gtk.Button("Start Count") 
buttonOk.connect("clicked", clickOk) 

buttonCancel = Gtk.Button("Cancel Count") 
buttonCancel.connect("clicked", clickCancel) 

progress = Gtk.ProgressBar() 
progress.set_show_text(True) 

vBox = Gtk.VBox() 
vBox.pack_start(buttonOk, True, True, 10) 
vBox.pack_start(buttonCancel, True, True, 10) 
vBox.pack_start(progress, True, True, 10) 

window = Gtk.Window() 
window.connect('destroy', Gtk.main_quit) 
window.add(vBox) 
window.show_all() 

Gtk.main() 

을 지금 :이 프로세스는 달리 인터페이스가 테스트를 위해이 같은 몇 가지를 지금 유지하고, 별도의 스레드가 될 것을 봤 그가 설정에서 실수를했다면 작업을 완전히 끝내기 위해서입니다. 그러나 나는 끈기있게 찾지 못한다. 또는 완전한 실행 전에 쓰레드를 죽이는 방법을 문서에서 찾아 낼 수는 없다.

+0

"행동의 폐지"란 무엇을 의미합니까? 스레드를 중단 하시겠습니까? 나는 "폐지"라는 단어가 당신이 그것을 사용하려는 방식으로 작동한다고 생각하지 않습니다. – user2357112

+0

예, 중단되었습니다. 미안 영어가 제 모국어가 아닙니다. – Atterratio

+0

'sleep (5)'는 장시간 실행되는 함수/동작을 의미합니다. 맞습니까? – drahnr

답변

0

파이썬에서 스레드를 죽일 수있는 간단한 방법은 없습니다. 이 문제를 해결하려면 초기 스레드 종료를 트리거하는 자체 후크를 빌드해야합니다. 이 작업을 수행하는 좋은 방법은 설정할 수있는 스레드 안전 트리거 인 Event을 사용하는 것입니다.

이렇게하기 전에 코드를 일부 클래스로 마무리 할 수 ​​있습니다. 클래스없이 GUI를 작성하면 장기간에 통증을 유발할뿐입니다.

from threading import Event,Thread 


class FakeClass(object): 
    def __init__(self): 
     self.progress = Gtk.ProgressBar() 
     self.progress.set_show_text(True) 
     self.buttonOk = Gtk.Button("Start Count") 
     self.buttonOk.connect("clicked", self.clickOk) 
     self.buttonCancel = Gtk.Button("Cancel Count") 
     self.buttonCancel.connect("clicked", self.clickCancel) 

     #create an event to trigger in your thread 
     self.trigger = Event() 
     self.t = None 

     #Other GTK stuff here... 

    def tread_function(self): 
     progress_fraction = 0.0 

     #keep looping while the trigger is not set and the 
     #progress is not > 1.0 
     while not self.trigger.is_set() and progress <= 1.0: 
      progress.set_fraction(progress_fraction) 
      time.sleep(1) 
      progress_fraction += 0.1 

    def clickOk(self, *args): 
     # reset the trigger 
     self.trigger.clear() 
     #launch the thread 
     self.t = threading.Thread(target=self.tread_function) 
     self.t.start() 

    def clickCancel(self, *args): 
     # set the trigger (interrupting the thread) 
     self.trigger.set() 
     # join the thread so it is not left hanging 
     if not self.t is None: 
      self.t.join() 


    # More other GTK stuff here... 
+0

작업자 스레드의'g_timeout_add'가 분수를 갱신하는 데 충분합니다 (백분율은'user_data' 인수로 전달 될 수 있습니다.) 나는 파이썬에서 이들의 정확한 이름을 알 수 없으므로 Event를 도입 할 필요가 없습니다. 'g_timeout_add'는 다음과 같습니다. threadsafe뿐만 아니라. – drahnr

관련 문제