我有一个要显示给用户的列表,但是我想将每个部分分成一个单独的textinput,因为这样更有意义。然后,我需要能够捕获这些更改并更新我的变量。
我需要获取变量以正确显示tlmanualreg
,backgroundManualReg[0]
并在closeit()
上将更改存储回列表。
编辑:更新了裸露的弹出项目,以测试变量交换。
from kivy.properties import StringProperty
from os.path import join, dirname
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.app import App
import json
Builder.load_string("""
<-MyPopup@Popup>:
tlpos:tlpos
tlmanualreg:tlmanualreg
cols: 1
GridLayout:
cols:3
Label:
text: "Location"
Label:
text: "Dist from Center (mm)"
Label:
text: "Corners of sheet (for manual-align)"
Label:
text: "Top-Left:"
TextInput:
id: tlpos
text: root.backgroundTLPOS
TextInput:
id: tlmanualreg
text: root.backgroundTLManualReg
Button:
size_hint_y:.1
text: 'Update Variables'
on_press: root.closeit()
""")
class ScreensApp(App):
def build(self):
content = MyPopup()
_popup = Popup(content=content, auto_dismiss=False)
_popup.open()
class MyPopup(Popup):
backgroundManualReg = [[551, 218], [3168, 319], [519, 1617], [3190, 1589]]
backgroundTLPOS = StringProperty("[0, 0]")
backgroundTLManualReg = StringProperty("[1,1],[2,2]")
def __init__(self, **kwargs):
super(MyPopup, self).__init__(**kwargs)
self.ids.tlmanualreg.text = str([1, 1])
self.backgroundTLManualReg = str(self.backgroundManualReg[0])
def closeit(self):
self.backgroundTLPOS = self.tlpos.text
print("backgroundTLPOS: ", self.backgroundTLPOS)
print("backgroundTLManualReg: ", self.ids.tlmanualreg.text)
print("backgroundManualReg: ", self.backgroundManualReg)
if __name__ == '__main__':
ScreensApp().run()
答案 0 :(得分:0)
在以下示例中,我们是:
for child in reversed(self.container.children)
和isinstance(child, TextInput)
检索数据from kivy.properties import StringProperty, DictProperty
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
import ast
Builder.load_string("""
<HeaderCell@Label>
text_size: self.size
halign: "left"
valign: "middle"
canvas.before:
Color:
rgba: 1, 0.502, 0, 1
Rectangle:
pos: self.pos
size: self.size
<MyPopup>:
title: 'My Popup'
container: container
GridLayout:
cols: 1
GridLayout:
cols: 3
size_hint_y: 0.1
HeaderCell:
text: "Location"
HeaderCell:
text: "Dist from Center (mm)"
HeaderCell:
text: "Corners of sheet (for manual-align)"
GridLayout:
id: container
cols:3
Button:
size_hint_y:.1
text: 'Update Variables'
on_press: root.closeit()
""")
class ScreensApp(App):
def build(self):
_popup = MyPopup(auto_dismiss=False)
_popup.open()
class MyPopup(Popup):
backgroundTLPOS = StringProperty("[0, 0]")
bgManualRegDict = DictProperty({'Top-Left': [551, 218], 'Top-Right': [3168, 319],
'Bottom-Left': [519, 1617], 'Bottom-Right': [3190, 1589]})
def __init__(self, **kwargs):
super(MyPopup, self).__init__(**kwargs)
self.populate_rows()
def populate_rows(self):
for key, value in self.bgManualRegDict.items():
location = Label(text="{}:".format(key), valign="middle")
location.bind(size=location.setter('text_size'))
bgTLpos = TextInput(text="{}".format(self.backgroundTLPOS))
bgTLmanualReg = TextInput(id="{}".format(key), text="{}".format(value))
self.container.add_widget(location)
self.container.add_widget(bgTLpos)
self.container.add_widget(bgTLmanualReg)
def closeit(self):
print("\ncloseit:")
for child in reversed(self.container.children):
if isinstance(child, TextInput):
if child.id in self.bgManualRegDict.keys():
print("\tid={0}, text={1}".format(child.id, child.text))
self.bgManualRegDict[child.id] = ast.literal_eval(child.text)
print("\n\tself.bgManualRegDict=", self.bgManualRegDict)
if __name__ == '__main__':
ScreensApp().run()