簡體   English   中英

如何在Kivy的.kv文件中添加ID?

[英]How to add an id in Kivy's .kv file?

我正在努力提出一個使用ID的簡單示例。 稍后,我想使用ID來更改參數,例如,更改標簽或按鈕中的文本。 那我該如何開始呢? 我找不到使用ID的簡單示例。

import kivy
from kivy.uix.gridlayout import GridLayout
from kivy.app import App

class TestApp(App):
    pass

if __name__ == '__main__':
    TestApp().run()

.kv文件:

#:kivy 1.0

Button:
    text: 'this button \n is the root'
    color: .8, .9, 0, 1
    font_size: 32

    Label: 
        text: 'This is a label'
        color: .9, 0, .5, .5

在這種情況下,我想為標簽使用ID並能夠更改文本。

您可以在此處找到有關kv語言的一些信息。 您需要為此使用kivy屬性

這是一個有關如何在按下按鈕時更改標簽文本的示例:python文件:

import kivy
from kivy.uix.gridlayout import GridLayout
from kivy.app import App
from kivy.properties import ObjectProperty

class GridScreen(GridLayout):
    label = ObjectProperty() #accessing the label in python
    def btnpress(self):
        self.label.text = 'btn pressed' #changing the label text when button is pressed

class TestApp(App):
    def build(self):
        return GridScreen()

if __name__ == '__main__':
    TestApp().run()

kv文件:

<GridScreen>:
    label: label #referencing the label
    rows: 2
    Button:
        text: 'this button \n is the root'
        color: .8, .9, 0, 1
        font_size: 32
        on_press: root.btnpress() #calling the method 

    Label:
        id: label
        text: 'This is a label'
        color: .9, 0, .5, .5

暫無
暫無

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

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