有没有办法在 kv 文件中更改 kivy api 变量(例如 scroll_distance)?

Is there a way to change kivy api variables (e.g. scroll_distance) inside kv file?

最近开始学习kivy,对.kv文件和python文件的交互方式不是很了解。例如,

class TimelineScr(ScrollView):
    scroll_wheel_distance = 60

如果我想改变里面的这个变量

<TimelineScr>:
    Label:
        canvas.before:
            Color:
                rgb: define.LIGHT_BLUE
            Rectangle:
                pos: self.pos
                size: root.width, self.texture_size[1]
            Color:
                rgb: (0.78, 0.92, 0.91)
            Rectangle:
                size: root.width-40, self.texture_size[1]-40
                pos: 20, 20

        size_hint_y: None
        height: self.texture_size[1]
        padding_y: 40
        #shorter: True
        line_height: 2

        color: define.TEXT_GREEN
        font_size: 20
        font_name: 'src/fonts/static/Inter-Regular.ttf'

        text: str(main.timeline)
        text_size: (root.width - 80, None)

我可以做吗?如何做?

对不起我的英语

我不是真正的 kivy 专家,但这是我经常开始的: 我有一个 main.py 这样的:

import # what u need

Builder.load_file('the.kv')
    
class fscreen(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        


class secscreen(Widget):

    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        pass

class thscreen(Widget):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        pass
            
class theapp(App):
    def build(self):
        self.screenm = ScreenManager()

        self.fscreen = fscreen()
        screen = Screen(name = "first screen")
        screen.add_widget(self.fscreen)
        self.screenm.add_widget(screen)

        self.secscreen = secscreen()
        screen  = Screen(name = "secondscreen")
        screen.add_widget(self.secscreen)
        self.screenm.add_widget(screen)

        self.thscreen = thscreen()
        screen  = Screen(name = "thirdscreen")
        screen.add_widget(self.thscreen)
        self.screenm.add_widget(screen)
        return self.screenm

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

和这样的 the.kv

<fscreen>
        
    canvas.before:
        Color:
            rgba: 1, 1, 1, 1
    
        Rectangle:
            size: root.width, root.height
            pos: 0, 0
    
    
    
    ScrollView:
        id: scroll_view
        size: root.width*0.4, root.height*0.4
        pos: root.height*0.3, root.height*0.3
        Label:
            id: output
            color: 0, 0, 0, 1
            text: ''
            size_hint_x: 1.0
            size_hint_y: None
            text_size: self.width, None
            height: self.texture_size[1]

现在如果我想更改 the.kv 文件中的标签,有两种更改方法:

  1. 按编号: 出于演示目的,我们必须在我们的 <fscreen> 中的 kv 文件中添加一个 Button,并在我们的 main.py 中添加一个函数,如下所示:

the.kv 文件中:

Button:
    text: ' change label by id'
    size: root.width*0.4, root.height*0.05
    pos: root.width*0.3, root.height*0.1
    on_press: root.change_label()

main.py中:

def change_label(self):
    self.ids.output.text = 'label has been changed by id'

同样,如果我们想通过 id 更改 ScrollView

ScrollView id 有很多与标签相同的属性,例如假设我们有另一个 Button 和另一个函数,如图所示,然后我们可以像这样通过 id 更改任何内容:

def change_by_id(self):
    self.ids.scroll_view.size[0] = self.width*0.5   
    self.ids.scroll_view.size[1] = self.height*0.5 

    self.ids.scroll_view.pos[0] = self.width*0.25   
    self.ids.scroll_view.pos[1] = self.height*0.25
    self.ids.scroll_view.#valid attribute = ...

    self.ids.output.text = 'something'
    self.ids.output.font_size = #....
    self.ids.output.color = #....    # and so on
  1. 按属性:

为此,让我们更改 the.kv 文件中的内容:

    ScrollView:
        id: scroll_view
        size: root.width*0.4, root.height*0.4
        pos: root.height*0.3, root.height*0.3
        Label:
            id: output
            color: 0, 0, 0, 1
            text: root.string   ###### this is what we changed
            size_hint_x: 1.0
            size_hint_y: None
            text_size: self.width, None
            height: self.texture_size[1]

        Button:
            text: ' change label by prop'
            size: root.width*0.4, root.height*0.05
            pos: root.width*0.3, root.height*0.1
            on_press: root.change_by_prop()

现在 main.py 必须有一个像这样的字符串 属性:

class fscreen(Widget):
    string = StringProperty()
    def __init__(self, **kwargs):
        super().__init__(**kwargs
        self.string = ''
   
    def change_by_prop(self):
        self.string = 'text has been changed by property'

ofc 有很多属性 NumericProperty()StringProperty() 等等...

这就是我在 python main.

中更改 kv 文件内容的方式