我试图了解在应用程序中存储类实例的最佳方式是什么,以便我正确访问属性并正确调用每个类方法。我希望该解决方案能够与 ORM 即 SqlAlchemy 一起使用,并在最后添加 GUI。
假设我有笔记本课。还有可以属于一个笔记本的 Note 类。还有一个 Picture 类 - 它的实例可以出现在属于不同 Notebook 实例的多个 Notes 中。
现在我想到了下面的方法(我已经使它更简单/没有 ORM 只是为了得到这个想法):
class Notebook(object):
def __init__(self):
self.notesid=[]
def view_notes(self,notes,pictures):
for key,item in notes.items():
if key in self.notesid:
item.show(pictures)
class Note(object):
def __init__(self,id,content,picture):
self.id = id
self.content = content
self.picturesid = [picture.id]
def show(self,pictures):
print(self.id, self.content)
for item in pictures:
if item.id in self.picturesid:
print(item)
class Picture(object):
def __init__(self,id,path):
self.id = id
self.path = path
def __str__(self):
'''shows picture'''
return "and here's picture …Run Code Online (Sandbox Code Playgroud) 我无法通过绑定到按钮的方法中的root.ids.created_in_kv.created_in_py访问动态创建的子项。当我检查root.ids.created_in_kv.ids字典时它是空的,但是root.ids.created_in_kv.children 中有孩子
我想要实现的是创建一个充当多选器的弹出窗口。它将接受可能的选择并动态创建标签-复选框对并将其添加到弹出内容中,并且在“应用”按钮上它将仅返回选择的列表(str())。
我不能在 kv 中构建带有多个小部件的弹出窗口,但以下工作(建议使其“更好”而不是受欢迎):
电压代码:
<SelectorPopup>:
title: 'empty'
BoxLayout:
id: inside
orientation: 'vertical'
BoxLayout:
id: options
BoxLayout:
id: buttons
orientation: 'vertical'
Button:
text: 'Apply'
on_release: root.return_selected()
Button:
text: 'Cancel'
on_release: root.dismiss()
<LabeledCheckbox@BoxLayout>:
id: entity
CheckBox:
id: choice
Label:
text: root.id
Run Code Online (Sandbox Code Playgroud)
我正在创建标签-复选框对(打包在 GridLayout 中)并将其放入选项 BoxLayout的 python 代码:
class SelectorPopup(Popup):
def return_selected(self):
selected=[]
a = self.ids.inside.options.choices.ids # dict is empty
for item in a.keys():
selected.append(item) if a[item].ids.choice.value #add if checkbox …Run Code Online (Sandbox Code Playgroud)