python/kivy:在.kv文件中访问属性值
当我点击Account(root.display_account())然后调用display_account()。之后,RVACCOUNT()函数调用。之后,当我点击+添加帐户然后高清add_account(个体经营):调用
python/kivy:在.kv文件中访问属性值
我有一个类AccountPopup其定义属性state_text和赋值文字:在.kv文件“测试”
如何获得state_text“测试”的价值,并通过在on_text: root.filter(self.text,state_text)并在def filter中打印。
test.py
class AccountPopup(Popup): state_text = ObjectProperty(None)
popupAccountCity = ObjectProperty(None)
def display_cities_treeview_account(self, instance):
if len(instance.text) > 0:
#if self.popupAccountCity is None:
self.popupAccountCity = TreeviewCityAccount(self.state_text.text)
self.popupAccountCity.filter(instance.text,self.state_text.text)
self.popupAccountCity.open()
class TreeviewCityAccount(Popup):
state_text = ObjectProperty(None)
def __init__(self,state_text, **kwargs):
print(state_text)
def filter(self, f,state):
print(state)
class RVACCOUNT(BoxLayout):
def add_account(self):
self.mode = "Add"
popup = AccountPopup(self)
popup.open()
class MainMenu(BoxLayout):
def display_account(self):
self.dropdown.dismiss()
self.remove_widgets()
self.rvaccount = RVACCOUNT()
self.content_area.add_widget(self.rvaccount)
class FactApp(App):
title = "Test"
def build(self):
self.root = Builder.load_file('test.kv')
return MainMenu()
if __name__ == '__main__':
FactApp().run()
test.kv
<AccountPopup>: state_text:state_text
TextInput:
id:state_text
text:'Testing'
<TreeviewCityAccount>:
BoxLayout
orientation: "vertical"
TextInput:
id: treeview
size_hint_y: .1
on_text: root.filter(self.text,state_text)
<RVACCOUNT>:
BoxLayout:
orientation: "vertical"
Button:
size_hint: .07, .03
text: "+Add Account"
on_press: root.add_account()
<MainMenu>:
content_area: content_area
dropdown: dropdown
BoxLayout:
orientation: 'vertical'
#spacing : 10
BoxLayout:
canvas.before:
Rectangle:
pos: self.pos
size: self.size
MenuButton:
id: btn
text: 'Master'
size : (60,30)
on_release: dropdown.open(self)
CustDrop:
DropdownButton:
text: 'Account'
size_hint_y: None
height: '32dp'
on_release: root.display_account()
有人能帮助我吗?
回答:
你应该引用它作为self.state_text
无处不在,也使其在PY文件,可以一比StringProperty
访问它作为
on_text: root.filter(self.text,root.state_text)
根KV是指在你的情况下,最左边的小部件又名<TreeviewCityAccount>:
。
见https://kivy.org/docs/api-kivy.lang.html
或者你可以在KV文件ID工作。
以上是 python/kivy:在.kv文件中访问属性值 的全部内容, 来源链接: utcz.com/qa/257423.html