2014-01-09 94 views
2

這是我的代碼:如何在kivy中更新屬性時啓動動畫?

from kivy.app import App 
from kivy.uix.boxlayout import BoxLayout 
from kivy.lang import Builder 
from kivy.properties import NumericProperty 


Builder.load_string(''' 
<Simple>: 
    Label: 
     text: str(root.sometext) 
    Button: 
     text: '+++' 
     on_release: root.inc() 
''') 


class Simple(BoxLayout): 
    sometext = NumericProperty(0) 

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

     self.sometext = 5 

    def inc(self): 
     self.sometext += 5 


class TApp(App): 
    def build(self): 
     return Simple() 


TApp().run() 

一切工作,每次當按鈕按下是標籤由5

更新什麼我不會添加一些動畫。 喜歡:更新號碼前往左邊,當更新號碼來自右側。 如果這是複雜的動畫,歡迎其他概念。

怎麼辦? 我查看了文檔,但每個示例都有postion,而不是文本更新(或者至少是我發現的)。

回答

4

@inclement是正確的。我不認爲可以直接爲文字製作動畫,但是可以爲標籤製作動畫。也可以連接動畫(operator '+')並使用動畫的事件on_complete將事物放在中間,就像您要查找的增量一樣。

這很簡單,但你需要在代碼中的一些變化:

  1. 你必須能夠從Python的訪問Label

    1.1。向標籤添加id

    Label: 
        id: _the_label 
    

    1.2。添加ObjectPropertySimple類:

    class Simple(BoxLayout): 
        the_label = ObjectProperty(None) 
    

    1.3。 「連接」的idObjectProperty

    <Simple>: 
        the_label: _the_label 
    
  2. 確保Label有空間移動到左側或右側。的一種方式做到這一點:

    2.1嵌入Label到另一個Widget,在這種情況下RelativeLayout

    RelativeLayout: 
        Label: 
    

    2.2定義尺寸爲Label和居中:

    size_hint: 0.3, 0.1 
    center_x: self.parent.width/2 
    center_y: self.parent.height/2 
    
  3. 現在您可以繼續創建動畫效果的方法Label

    def animate(self): 
        left = Animation(x=0, color= [0,0,0,0]) 
        left.bind(on_complete=self.inc) 
        right = Animation(center_x=self.the_label.parent.width/2, color= [1,1,1,1]) 
        anim = left + right 
        anim.start(self.the_label) 
    
    def inc(self, instance, value): 
        self.sometext += 5 
    

    提示:bindon_complete的左邊動畫添加到方法inc中。另請注意anim = left + right連接兩個動畫。還有運營商*運行並行動畫。

儘管無法直接爲文本設置動畫效果,但標籤的某些屬性會間接影響它。例如,font_size,color等。這是一個complete list。如果您真的需要動畫文字,我肯定應該有辦法破解通過動畫padding property動畫的文字。

最後的代碼是在這裏:

from kivy.app import App 
from kivy.uix.boxlayout import BoxLayout 
from kivy.lang import Builder 
from kivy.properties import NumericProperty, ObjectProperty 
from kivy.animation import Animation 

Builder.load_string(''' 
<Simple>: 
    the_label: _the_label 
    RelativeLayout: 
     Label: 
      id: _the_label 
      size_hint: 0.3, 0.1 
      center_x: self.parent.width/2 
      center_y: self.parent.height/2 
      text: str(root.sometext) 
    Button: 
     text: '+++' 
     on_release: root.animate() 
''') 


class Simple(BoxLayout): 
    the_label = ObjectProperty(None) 
    sometext = NumericProperty(5) 

    def animate(self): 
     left = Animation(x=0) 
     left.bind(on_complete=self.inc) 
     right = Animation(center_x=self.the_label.parent.width/2) 
     anim = left + right 
     anim.start(self.the_label) 

    def inc(self, instance, value): 
     self.sometext += 5 

class TApp(App): 
    def build(self): 
     return Simple() 

TApp().run() 
+0

這是否正常工作。我會看到我會在實踐中使用它,它對我來說太簡單了太多代碼。也有可能有fadeIn,FadeOut效果和如何? – WebOrCode

+0

當然,這真的很酷。只需在第一個動畫('left = Animation(x = 0,color = [0,0,0,0])')中將'color'屬性設置爲與背景相同的顏色(黑色),然後回到白色在第二個動畫上('right = Animation(center_x = self.the_label.parent.width/2,color = [1,1,1,1])'')。我將編輯文件。 –

1

該怎麼辦?我查看了文檔,但每個示例都有postion,而不是文本更新(或者至少是我發現的)。

Kivy的動畫修改了特定的屬性,我不認爲動畫文本在這種情況下確實有意義。

這聽起來像是想獲得您想要的行爲,您真的需要添加一個或多個位置動畫以獲得移動效果的標籤 - 也就是說,Animation類不會讓您爲一個屬性(文本)通過改變他人(pos),它只涉及以一種特定方式改變一個特定的財產。

如果這是動畫複雜化,歡迎其他概念。

如果我想這樣做,我實際上會考慮使用帶有兩個標籤的Carousel控件。 inc方法會更改當前隱藏標籤的文本,然後調用切換當前可見小部件的Carousel方法。旋轉木馬已經有了一個動畫,第一個Widget滑落,而下一個滑動,所以它會照顧你的細節。

雖然這很具體,但使用Carousel恰好適合您的特殊問題。我認爲您的問題的真正答案是,您需要考慮自行添加更復雜的行爲,因爲它不是僅爲一個屬性設置動畫的情況。

相關問題