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