坏:在 kivy 中使用循环时冻结应用程序

发布于 2025-01-18 18:37:26 字数 1207 浏览 4 评论 0原文

当我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文