問題描述
我正在創建一個 kivy 用戶界面來顯示由我作為標準 python 對象編寫的數據模型生成的值.本質上,我希望用戶能夠按下一個按鈕,這將改變底層數據模型,并且這種改變的結果將自動更新和顯示.據我了解,這可以使用 kivy 屬性(在本例中為 ObjectProperty)來實現.
I am working on creating a kivy user interface to display the values generated by a data model I've written as a standard python object. In essence, I would like the user to be able to press a button, which would change the underlying data model and the results of this change would be automatically updated and displayed. It is my understanding that this can be implemented using kivy properties (in this case, ObjectProperty).
下面是一些示例代碼:
import kivy
kivy.require('1.7.0')
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty
from kivy.lang import Builder
Builder.load_string("""
<RootWidget>:
cols: 2
Label:
text: "Attribute a:"
Label:
text: root.data_model.a
Label:
text: "Attribute b:"
Label:
text: root.data_model.b
Label:
text: "Attribute c:"
Label:
text: root.data_model.c
Button:
text: "Make data_model.a longer"
on_press: root.button_press()
Button:
text: "Make data_model.b shorter"
on_press: root.button_press2()
""")
class DataModel(object):
def __init__(self):
self.a = 'This is a'
self.b ='This is b'
@property
def c(self):
return self.a + ' and ' + self.b
class RootWidget(GridLayout):
data_model = ObjectProperty(DataModel())
def button_press(self, *args):
self.data_model.a = 'This is a and it is really long now'
print self.data_model.c
def button_press2(self, *args):
self.data_model.b = 'B'
print self.data_model.c
class TestApp(App):
def build(self):
return RootWidget()
app = TestApp()
app.run()
所需的結果是當用戶按下任一按鈕時,標簽會自動更新以顯示新屬性.從打印語句可以看出,data_model 正在正確更新.但是,沒有一個標簽正在更新.有人可以澄清如何做到這一點嗎?
The desired result is for when user presses either button, the labels would automatically update to show the new properties. As can be seen by the print statements, the data_model is getting updated correctly. However, none of the labels are updating. Can someone clarify how to do this?
推薦答案
但是,所有標簽都沒有更新.有人可以澄清如何做到這一點嗎?
However, none of the labels are updating. Can someone clarify how to do this?
你引用的屬性需要是Kivy屬性,但是你引用的a
、b
和c
都是python屬性所以Kivy無法綁定到他們的更改.
The attributes you reference need to be Kivy properties, but the a
, b
and c
you reference are all just python attributes so Kivy has no way to bind to their changes.
要使用屬性,您需要您的對象從 EventDispatcher
繼承(Kivy 小部件會自動執行此操作,這就是它們的屬性起作用的原因).
To work with properties you need your object to inherit from EventDispatcher
(Kivy widgets do this automatically, which is why their properties work).
from kivy.event import EventDispatcher
class DataModel(EventDispatcher):
a = StringProperty('')
b = StringProperty('')
c = StringProperty('')
def __init__(self, *args, **kwargs):
super(DataModel, self).__init__(*args, **kwargs)
self.a = 'This is a'
self.b ='This is b'
self.bind(a=self.set_c)
self.bind(b=self.set_c)
def set_c(self, instance, value):
self.c = self.a + ' and ' + self.b
請注意,這不是獲得你想要的 c 行為的唯一方法(甚至不一定是最好的方法).您可以使用 kv 語言創建綁定(我通常會這樣做),或者您可以查看 Kivy 的 AliasProperty 以獲得更類似于您的原始定義的內容.
Note that this is not the only way (or even necessarily the best way) to get the behaviour you wanted for c. You could create the binding in kv language (I'd usually do it that way) or you can look at Kivy's AliasProperty for something more like your original definition.
當然你也可以在聲明屬性時設置 a 和 b 的值.
Of course you could also set the values of a and b when the properties are declared.
這篇關于Kivy ObjectProperty 更新標簽文本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!