python - Kivy 不同小部件和布局类之间的属性和通信
问题描述
我有一个关于以下问题的简单示例。我有三个类 MyLayout(根),Pop 一个弹出类和 MyBox,这是一个通过单击 MyLayout 中的按钮动态创建的 Boxlayout。我在 root 中创建了与弹出字段一起使用的 capitalise() 函数。我的问题是与 MyBox 实例的交互。例如,弹出窗口如何知道哪个 MyBox 调用了它并将名字 + 姓氏返回到相应的 TextInput 框?
此外,如果我想在所有 MyBox 实例中整理 TextInput 框中的所有数据,我将如何做到这一点。我假设使用属性。
提前致谢
# filename popper.py
from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.stacklayout import StackLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
Builder.load_string('''
#:import Factory kivy.factory.Factory
<MyBox>:
orientation:'vertical'
TextInput:
text: 'N/A'
Button:
text: 'Choose a name'
on_press: Factory.Pop().open()
<Pop>:
auto_dismiss: False
title: 'Names'
size_hint: [0.4, 0.5]
pos_hint:{'right': 0.4, 'top': 1}
id: msg_box
GridLayout:
id: _pop
rows: 3
GridLayout:
id: pop_grid
cols:2
padding: [0,5]
Spinner:
text: 'First Name'
id: fn
sync_height: True
values: ['andrew', 'brian', 'colin', 'david', 'edmond']
width: self.width
on_text: self.text = app.root.capitalise(self.text)
Spinner:
text: 'Last Name'
id: ln
sync_height: True
values: ['Adams', 'Bass', 'Carney', 'Davies', 'Edmonds']
width: self.width
Button:
padding: [0,5]
text: 'OK'
on_release: root.dismiss()
width: self.width
<MyLayout>:
orientation: 'tb-lr'
size_hint: .2, 0.5
width: self.width
Button:
text: 'Create name box.'
on_press: app.root.make_name_box()
width: 300
''')
class MyLayout(StackLayout):
pass
def make_name_box(self):
self.add_widget(MyBox())
def capitalise(self, text):
return text.capitalize()
class Pop(Popup):
def __init__(self, **kwargs):
super(Pop, self).__init__(**kwargs)
class MyBox(BoxLayout):
def __init__(self, **kwargs):
super(MyBox, self).__init__(**kwargs)
size_hint = None, None
width = 300
class PopperApp(App):
def build(self):
return MyLayout()
if __name__ =='__main__':
PopperApp().run()
解决方案
您想要的主要是将“创建者”框的引用传递给“已创建”弹出窗口。您可以通过在创建时传递小部件来做到这一点on_press: Factory.Pop(root).open()
完整的工作代码在这里:
Builder.load_string('''
#:import Factory kivy.factory.Factory
<MyBox>:
orientation:'vertical'
TextInput:
text: root.box_text # bind the text to the box_text
Button:
text: 'Choose a name'
on_press: Factory.Pop(root).open() # pass the creator of the pop here
<Pop>:
auto_dismiss: False
title: 'Names'
size_hint: [0.4, 0.5]
pos_hint:{'right': 0.4, 'top': 1}
id: msg_box
GridLayout:
id: _pop
rows: 3
GridLayout:
id: pop_grid
cols:2
padding: [0,5]
Spinner:
text: 'First Name'
id: fn
sync_height: True
values: ['andrew', 'brian', 'colin', 'david', 'edmond']
width: self.width
on_text: self.text = app.root.capitalise(self.text)
Spinner:
text: 'Last Name'
id: ln
sync_height: True
values: ['Adams', 'Bass', 'Carney', 'Davies', 'Edmonds']
width: self.width
Button:
padding: [0,5]
text: 'OK'
on_release:
root.creator.box_text = "{} {}".format(fn.text, ln.text) # use the creator reference
root.dismiss()
width: self.width
<MyLayout>:
orientation: 'tb-lr'
size_hint: .2, 0.5
width: self.width
Button:
text: 'Create name box.'
on_press: app.root.make_name_box()
width: 300
''')
class MyLayout(StackLayout):
pass
def make_name_box(self):
self.add_widget(MyBox())
def capitalise(self, text):
return text.capitalize()
class Pop(Popup):
def __init__(self, creator, **kwargs):
super(Pop, self).__init__(**kwargs)
self.creator = creator # keep a reference to the creator here
class MyBox(BoxLayout):
box_text = StringProperty("N/A")
def __init__(self, **kwargs):
super(MyBox, self).__init__(**kwargs)
size_hint = None, None
width = 300
class PopperApp(App):
def build(self):
self.my_layout = MyLayout()
return self.my_layout
def on_stop(self): # print the names when leaving the app
boxes = self.my_layout.children[:-1]
print([i.box_text for i in boxes])
if __name__ == '__main__':
PopperApp().run()
编辑:至于第二个问题,我在您关闭应用程序时添加了名称的打印。
推荐阅读
- elasticsearch - 在 elasticsearch 中使用 mserch 查询排除索引名称
- python - 为什么我们需要 __init__ 来初始化一个 python 类
- python - 使用 Python 对 excel 文件中的内容进行加密和解密
- excel - 在 Excel 中将行添加到数据透视表时防止嵌套分组
- regression - 预测评估指标 - 相对误差
- mysql - 如何显示名称为 Id
- javascript - Array.filter() 和过滤对象的引用
- reactjs - TypeError: (0 , this.props.children) 不是函数 react + apollo
- ios - 是否需要请求 Siri 授权?
- c# - 用于定位按钮元素的定位器在放置在代码中时不起作用,但它在 Chrome 内部搜索中起作用