坏:在 kivy 中使用循环时冻结应用程序
当我在kivy应用中使用循环以显示循环的更新值时,我有问题,因此,当我运行应用程序并按Toggle按钮开始显示Loop I Program Crash的最后值。
这是代码:
*.py
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from random import randint
class ShowLoopValueInterface(BoxLayout):
ToggleButton_label = StringProperty("normal")
Label_text_value = StringProperty("normal")
def togglebutton_on_state(self, widget):
if widget.state == "down":
self.ToggleButton_label = "down"
# self.to_text_value()
Clock.schedule_interval(self.to_text_value, 0.5)
else:
self.ToggleButton_label = "normal"
def to_text_value(self, _):
# def to_text_value(self):
while True:
self.Label_text_value = str(randint(0, 100))
class ShowLoopValueApp(App):
pass
ShowLoopValueApp().run()
*.kv
ShowLoopValueInterface:
<ShowLoopValueInterface>:
orientation: "vertical"
ToggleButton:
text: root.ToggleButton_label
on_state: root.togglebutton_on_state(self)
Label:
text: root.Label_text_value
i have problem when i use loop in kivy app to show update value for loop, so when i run app and press toggle button to start showing last value of loop i program crash.
this is code:
*.py
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from random import randint
class ShowLoopValueInterface(BoxLayout):
ToggleButton_label = StringProperty("normal")
Label_text_value = StringProperty("normal")
def togglebutton_on_state(self, widget):
if widget.state == "down":
self.ToggleButton_label = "down"
# self.to_text_value()
Clock.schedule_interval(self.to_text_value, 0.5)
else:
self.ToggleButton_label = "normal"
def to_text_value(self, _):
# def to_text_value(self):
while True:
self.Label_text_value = str(randint(0, 100))
class ShowLoopValueApp(App):
pass
ShowLoopValueApp().run()
*.kv
ShowLoopValueInterface:
<ShowLoopValueInterface>:
orientation: "vertical"
ToggleButton:
text: root.ToggleButton_label
on_state: root.togglebutton_on_state(self)
Label:
text: root.Label_text_value
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论