簡體   English   中英

奇異果更新標簽紋理

[英]Kivy Update Label Texture

我需要一次更新一組標簽,但是在功能完成之前,我還需要查看更改的效果。 期望的結果是一種加載條。

就目前而言,我的代碼在函數末尾立即全部應用了更改。

(簡化了代碼以便於閱讀)

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.

您應該使用時鍾來安排標簽文本的更改。 考慮以下代碼:

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

暫無
暫無

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

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