'main_loop'函數while循環不會每5秒增加self.i值,'run'函數不會每隔1秒遞減self.mycounter值。kivy python時鐘表更新
爲什麼?
我在做什麼錯?
我不想使用time.sleep。
class MyThread(BoxLayout):
stop = []
timer = []
times = []
i = NumericProperty(0)
mycounter = NumericProperty(0)
def incrementi(self, *args):
self.i += 1
def decrementcounter(self,*args):
self.mycounter -= 1
def run(self):
self.mycounter = 30
while not self.stop:
self.timer.append(self.mycounter)
Clock.schedule_interval(self.decrementcounter, 1.0)
self.times.append(self.mycounter)
return self.mycounter
def main_loop(self):
self.i = 0
while True:
Clock.schedule_interval(self.incrementi, 5.0)
if self.i == 2:
self.mycounter = 30
threading.Thread(target = self.run).start()
if self.i == 5:
self.stop.append('dummystring')
if self.i == 6:
self.stop.pop(0)
self.timer = []
self.ids.lbl.text = "{}".format(self.i)
if self.i == 7:
self.i = 0
def read_it(self):
threading.Thread(target = self.main_loop).start()
class MyApp(App):
def build(self):
self.load_kv('thread.kv')
return MyThread()
if __name__ == "__main__":
app = MyApp()
app.run()
kivy.pv
<MyThread>:
Button:
text: "start program"
on_release: root.read_it()
Label:
id: lbl
text: "current step"
font_size: 50
直到您的while循環返回時,Kivy中才會發生任何事情,因爲它會阻塞該線程。使用時鐘表來定期運行你的代碼。 – inclement
但我在while循環中有線程...例如我開始線程,如果self.i等於2 ...所以我不能打電話while循環'X'秒與時鐘...你有什麼建議請? – BlueDog
我不明白爲什麼這會阻止你通過時鐘定期調用函數,但如果你真的想這樣做,那麼把整個函數放在一個線程中。 – inclement