簡述
VariableListProperty類專門將資料做長度上的調整並改為串列,例如:
VariableListProperty([1]) represents [1, 1, 1, 1]
VariableListProperty([1, 2]) represents [1, 2, 1, 2]
VariableListProperty([‘1px’, (2, ‘px’), 3, 4.0]) represents [1, 2, 3, 4.0]
VariableListProperty(5) represents [5, 5, 5, 5]
VariableListProperty(3, length=2) represents [3, 3]
基本範例
在main.py中寫上此段程式碼:
from kivy.app import App from kivy.uix.gridlayout import GridLayout from kivy.properties import VariableListProperty class MyLayout(GridLayout): a1 = VariableListProperty([0.2],length=2) def btn1(self): self.a1 = 0.7 def on_a1(self, instance, x): print('a1 change to {}'.format(self.a1)) class Myapp(App): def build(self): return MyLayout() if __name__ == '__main__': Myapp().run()
在以上程式碼中,我在MyLayout中宣告a1為VariableListProperty類,其值為[0.2]、length為2代表將[0.2]調整長度至2,因此a1的最終值為[0.2,0.2],並宣告btn1方法,當btn1方法被呼叫時,指定a1的值為0.7,搭配length=2代表最終值為[0.7,0.7],最後,當a1發生改變時,自動呼叫on_a1方法與on_a2方法,列印'a1 change to {}'.format(self.a1)
在my.kv中寫上此段程式碼:
<MyLayout>: rows:2 Button: text:'hello' Button: text: 'A1' size_hint: root.a1 on_press: root.btn1() Button: text: 'B2' size_hint: root.a1
執行結果如下:
VariableListProperty使用技巧
沒有
沒有留言:
張貼留言