如何为 Kivy 标签中的变量编码?

How to code for a variable in the Kivy label?

我正在尝试创建一个简单的 Kivy 函数,它将标签中的显示计数并更新为变量或已转换为字符串的变量。使用 Python 3.7 和 Kivy 1.10.1

我一直在阅读之前与标签相关的问题,但它们似乎无法解决我的问题。谢谢。

from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
import time

class SomeData():
    num = 0
    while num < 1000:
        time.sleep(1)
        num+=1

class FirstScreen (Screen):

    runTouchApp(Builder.load_string('''
ScreenManager:
    FirstScreen:

<FirstScreen> 
    BoxLayout:
        orientation: 'vertical'

        GridLayout:
            cols: 3
            spacing: '10dp'
            Button:
            Button:
            Button:

        Label:
            size_hint_y: None
            text: "Below is a scroll of numbers."

        ScrollView:
            Label:
                text_size: self.width, None
                size_hint_y: None
                height: self.texture_size[1]
                halign: 'left'
                valign: 'top'
                text: (num)
    '''))

该文件从未创建 Kivy 屏幕,num 变量被认为是文本标签中的错误。

这是您的代码的一个版本,至少会显示:

from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.properties import NumericProperty
from kivy.uix.screenmanager import Screen
#import time

# class SomeData:
#     num = 0
#     while num < 1000:
#         time.sleep(1)
#         num+=1

class FirstScreen (Screen):
    num = NumericProperty(7)

runTouchApp(Builder.load_string('''
ScreenManager:
    FirstScreen:

<FirstScreen> 
    BoxLayout:
        orientation: 'vertical'

        GridLayout:
            cols: 3
            spacing: '10dp'
            Button:
            Button:
            Button:

        Label:
            size_hint_y: None
            text: "Below is a scroll of numbers."

        ScrollView:
            Label:
                text_size: self.width, None
                size_hint_y: None
                height: self.texture_size[1]
                halign: 'left'
                valign: 'top'
                text: str(root.num)
    '''))

SomeData class 被注释掉了,因为它除了延迟显示什么都不做。另外请注意,在循环中更改 num 的值不会创建数字列表,而只会更改 Label 中显示的数字。也不需要导入时间。所以,评论一下。