'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