Kivy更新带变量的标签文本

时间:2018-02-17 23:04:20

标签: python text label scrollview kivy

问题:如何在此方案中更新标签文本?

Python代码:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty, ObjectProperty, NumericProperty

class SearchForPart(Widget):
    def searchforpart(self):
        Barty = 'text update';
        demogar = DemoGarage()
        demogar.ids.scrollref.UpdateParam(Barty)

class ScrollableLabel1(ScrollView):  
    text_variable_1 = StringProperty()
    def __init__(self, **kwargs):
        super(ScrollableLabel1, self).__init__(**kwargs)
        self.text_variable_1 = 'initial' 

    def UpdateParam(self, param):
        self.param = param
        self.text_variable_1 = param
        print(param)

class AnotherScreen(Screen):
    pass

class MainScreen(Screen):
    pass

class DemoGarage(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

presentation = Builder.load_file("garagemainexample.kv")

class MainApp(App):
    def build(self):
        return presentation

if __name__ == "__main__":
    MainApp().run()

Kv代码:

#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
    transition: FadeTransition()
    MainScreen:
    DemoGarage:

<BigButton@Button>:
    font_size: 40
    size_hint: 0.5, 0.15
    color: 0,1,0,1 

<MainScreen>:
    name: "main"
    FloatLayout:
        BigButton:
            on_release: app.root.current = "demogarage"
            text: "Demo Garage"
            pos_hint: {"x":0.25, "top": 0.4} 

<DemoGarage>:
    name: "demogarage"
    ScrollableLabel1:
        id:scrollref
    SearchForPart:
        id:searchpart

<ScrollableLabel1>:
    Label:
        id: mylabel
        text: root.text_variable_1
        font_size: 20
        text_size: self.width, None
        size_hint_y: None
        height: self.texture_size[1]
        padding_y: 10
        padding_x: 140

<SearchForPart>:
    size_hint: 1,1
    FloatLayout:
        Label:
            text: "Search for Part:"
            font_size: 30
            pos_hint: {"x": 0.95, "y": 0.85}
            color: 0,1,0,1
            size_hint: 0.3, 0.2
        TextInput:
            id: partname
            multiline: False
            font_size: 30
            pos_hint: {"x": 0.85, "y": 0.15}
            color: 0,1,0,1
            on_text_validate: root.searchforpart()
            size_hint: 1, 0.5

初始化程序后,Label文本已成功初始化并输出初始文本&#39;。

&#39; searchforpart&#39;方法运行(作为对在搜索框中输入的响应),即使&#39; print(param)&#39;打印&#39;文本更新&#39;,标签文字未更新。

问题:如何在此方案中更新标签文本?

我是否误解了如何触发对Kivy事件循环的更改的核心概念?

1 个答案:

答案 0 :(得分:1)

使用Kivy属性:

from kivy.properties import StringProperty

class (YourClass):
    text_variable_1 = StringProperty()

当您在kv中引用此属性并且其值更改时,gui将自动更新。如果您不使用某个属性,则不会发生这种情况,因为kv代码无法知道更改的值。

此外,您不必关注pep8,但属性名称应以小写字母开头 - 它可能对您的示例并不重要,但如果您尝试设置,则可能无关紧要那里的财产。