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

时间:2019-06-05 01:45:15

标签: kivy python-3.7

我正在尝试创建一个简单的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变量被认为是文本标签中的错误。

1 个答案:

答案 0 :(得分:0)

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

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类已被注释掉,因为它除了延迟显示外什么都不做。另外,请注意,在循环中更改num的值不会创建数字列表,而只会更改Label中显示的数字。