【问题标题】:Kivy Update Label TextureKivy 更新标签纹理
【发布时间】:2016-02-07 19:08:55
【问题描述】:

我需要更新一组标签,一次1个,但我还需要在功能完成之前查看更改的效果。期望的结果是一种加载条。

就目前而言,我的代码在函数结束时一次性应用所有更改。

(简化代码以方便阅读)

main.py

def TextAnimation(self):
    #self.ids.??? are labels
    self.ids.x1y1.text = "-"
    self.ids.x2y1.text = "-"
    self.ids.x3y1.text = "-"
    self.ids.x1y1.texture_update()
    self.ids.x2y1.texture_update()
    self.ids.x3y1.texture_update()
    time.sleep(0.2)
    self.ids.x4y1.text = "-"
    self.ids.x5y1.text = "-"
    self.ids.x6y1.text = "-"
    self.ids.x4y1.texture_update()
    self.ids.x5y1.texture_update()
    self.ids.x6y1.texture_update()
    time.sleep(0.2) 

我的印象是labelName.texture_update() 立即调用下一帧,而不是等待函数结束,但似乎不像文档中描述的那样工作;

Warning The texture update is scheduled for the next frame. If you need the texture immediately after changing a property, you have to call the texture_update() method before accessing texture:

    l = Label(text='Hello world')
    # l.texture is good
    l.font_size = '50sp'
    # l.texture is not updated yet
    l.texture_update()
    # l.texture is good now.

【问题讨论】:

    标签: python oop kivy


    【解决方案1】:

    您应该使用Clock 来安排标签文本更改。考虑这段代码:

    test.kv:

    #:kivy 1.9.0
    Root:
        cols: 1
    
        Label:
            id: my_label
    
        Button:
            text: 'animate text'
            on_press: root.animate_text()
    

    main.py:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    from kivy.app import App
    from kivy.uix.gridlayout import GridLayout
    from kivy.clock import Clock
    
    
    class Root(GridLayout):
    
        string = ''
    
        def animate_text(self):
            Clock.schedule_interval(self.update_label, 0.1)
    
        def update_label(self, dt):
            self.string += '>'
            self.ids.my_label.text = self.string
    
            if len(self.string) > 20:
                Clock.unschedule(self.update_label)
                self.string = ''
                self.ids.my_label.text = 'DONE'
    
    
    class Test(App):
        pass
    
    
    Test().run()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 2019-05-15
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多