2017-06-17 2 views
0

UIにkivyを使用しています。 Time_consuming関数があり、それが実行されるとき、kivy UIは黒で表示されるので、私はThreadingを使用しました。 Time_consuming関数が終了するまでボタンを無効にしてから、もう一度ボタンを有効にします。 Time_consuming()が終了した場合であっても、真である)関数がkivyで終了するまで、un_activeボタン

from threading import Thread 
from kivy.clock import Clock 
from functools import partial 

    def run(): 
     self.run_button.disabled=True 
     self.back_button.disabled=True 

     t=Thread(target=Time_consuming(), args=()) 
     t.start() 

     Clock.schedule_interval(partial(disable, t.isAlive()), 8) 

    def disable(t, what): 
     print(t) 
     if not t: 
      self.run_button.disabled=False 
      self.back_button.disabled=False 

が、この用量は、無効でt.isAliveを()(動作しない:私は以下のようなものを使用されています。問題はどこだ ?

question2:別の問題は、Clock.schedule_intervalは永久に実行され続けることです。機能が終了したらどうやって止めることができますか?

+0

あなたが(スレッドを取得する場所を明確にしてください可能性が)あなたのコード – PalimPalim

+0

が行わ –

+0

質問2コーディングするインポートを追加複製することが容易であるので、から:Clock.schedule_once(my_callbackを) – PalimPalim

答えて

0

私が発見した。このTの代わりに、t.isAliveを(パス):

Clock.schedule_interval(partial(disable, t.isAlive()), 8) 

変更へ:

Clock.schedule_interval(partial(disable, t), 8) 

question2:無効にした場合(。

question1を)がFalseを返すと、スケジュールはキャンセルされ、繰り返されません。

def run_model(): 
    self.run_button.disabled=True 
    self.back_button.disabled=True 

    t=Thread(target=run, args=()) 
    t.start() 

    Clock.schedule_interval(partial(disable, t), 8) 

def disable(t, what): 
    if not t.isAlive(): 
     self.run_button.disabled=False 
     self.back_button.disabled=False 
     return False 
1

あなたの質問にはすでに回答があります。私はまたあなたが答えている間、あなたを助けるためのサンプルアプリケーションを構築していました。私はまだあなたに価値があると思うので、私はそれを掲示しています。 1つのボタンはスレッド化を示し、もう1つはスケジューリングを1回表示します。ハッピーコーディング:)。

from kivy.app import App 
from kivy.base import Builder 
from kivy.properties import ObjectProperty 
from kivy.uix.boxlayout import BoxLayout 
from kivy.clock import Clock 

import time 
import threading 


Builder.load_string(""" 
<rootwi>: 
    label_to_be_changed1: label_to_be_changed1 
    label_to_be_changed2: label_to_be_changed2 
    button1: button1 
    button2: button2 
    orientation: 'vertical' 
    Button: 
     id: button1 
     text:'Button1 - Threading' 
     on_press: root.change_Label_text1() 
    Button: 
     id: button2 
     text: 'Button2 - schedule' 
     on_press: root.change_Label_text2() 
    Label: 
     id: label_to_be_changed1 
    Label: 
     id: label_to_be_changed2 
""") 
class rootwi(BoxLayout): 
    label_to_be_changed1 = ObjectProperty() 
    label_to_be_changed2 = ObjectProperty() 
    button1 = ObjectProperty() 
    button2 = ObjectProperty() 


    def change_Label_text1(self): 
     self.button1.disabled = True 
     threading.Thread(target=self.timeconsuming).start() 

    def timeconsuming(self): 
     #do your stuff 
     time.sleep(5) 
     self.label_to_be_changed1.text = 'thread has ended' 
     self.button1.disabled = False 

    def change_Label_text2(self): 
     self.button2.disabled = True 
     Clock.schedule_once(self.change_Label_text2_callback, 4) 

    def change_Label_text2_callback(self, *largs): 
     self.label_to_be_changed2.text = 'schedule has ended' 
     self.button2.disabled = False 

class MyApp(App): 
    def build(self): 
     return rootwi() 

if __name__ == '__main__': 
    MyApp().run() 
関連する問題