簡體   English   中英

Kivy更新帶有變量的標簽文本

[英]Kivy Update Label Text with Variable

問題:在這種情況下,如何更新標簽文本?

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()

驗證碼:

#: 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文本並輸出'initial text'。

一旦“ searchforpart”方法運行(作為對在搜索框中按回車的響應),即使“ print(param)”打印“文本更新”,標簽文本也不會更新。

問題:在這種情況下,如何更新標簽文本?

我是否誤解了如何觸發Kivy事件循環更改的核心概念?

使用Kivy屬性:

from kivy.properties import StringProperty

class (YourClass):
    text_variable_1 = StringProperty()

當您在kv中引用此屬性,並且其值更改時,gui將自動更新。 如果您不使用屬性,則不會發生這種情況,因為kv代碼無法知道更改的值。

另外,您不必遵循pep8,但是屬性名稱應以小寫字母開頭-對於您的示例來說可能並不重要,但是如果您嘗試在此處設置屬性,則在kv中可能很重要。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM