python - 如果我必须给它们一个唯一的名称,我如何从字典键(数量未知)创建按钮,以便我可以在它们上使用 call_back() 函数
问题描述
我正在制作一个 GUI。有一个主数据库(字典),从中导入其密钥。为了轻松导入数据,我添加了一个 TextInput 并添加了一个搜索过滤器视图。每当在 Textinput 中键入一个字母时,它都会显示与文本字符串匹配的所有键(在标签中)。
在一个键(标签文本)旁边有一个按钮,可将标签的文本添加到新滚动视图中的另一个标签,在该滚动视图中形成不同的计算。
所以我之前认为进入下一个标签我应该尝试通过将它打印到控制台来获取按钮的值,并且每当我按下标签旁边的按钮时,我只会在搜索视图中获得最后一个标签的文本而不是 Label1s它连接到。(但我不知道我是否连接正确,它以某种方式与标签的命名有关,但我不知道如何)
这是一张可以更好地理解我的问题的图片(它显示了我想要做的事情):
py:
class CalculationView(BoxLayout):
SearchGridShown = BooleanProperty()
SLB = ObjectProperty()
def find_searched_text(self, text):
Dict = db.databaseDict
self.ids.SearchGrid.clear_widgets()
FD = {k: v for (k, v) in Dict.items() if text in k and len(text) > 0}
for kk, vv in FD.items():
self.SLB = Label(
color=(.2, .1, 0, 1),
text=str(kk))
self.BTN = Button(
text='OK',
background_color=(.05, .3, .05, 1),
font_size='13sp',
size_hint=(.1, 1))
self.BTN.bind(on_press=lambda x: self.add_product_for_calculation(self.SLB.text))
self.BL = BoxLayout()
self.BL.add_widget(self.SLB)
self.BL.add_widget(self.BTN)
self.ids.SearchGrid.add_widget(self.BL)
def add_product_for_calculation(self, product):
print('0' + product + '0')
Dict = db.databaseDict
V = Dict[product]
print(V)
千伏:
<CalculationView>:
orientation: 'vertical'
size_hint_x: .96
size_hint_y: .9
pos_hint: {'x': .02,'y': .0875}
BoxLayout:
spacing: 2, 0
padding: 1
size_hint: 1, .06
TextInput:
size_hint: 1.5, 1
text: 'Kereses'
on_focus: self.text = ""
font_size: '13sp'
on_text: root.find_searched_text(self.text)
Button:
size_hint: .25, 1
text: 'Keres'
on_release: root.SearchGridShown = not root.SearchGridShown
BoxLayout:
...
ScrollView:
do_scroll_x: False
do_scroll_y: True
size_hint_x: 1
size_hint_y: .9
pos_hint: {'x': 0,'y': .1}
GridLayout:
id: CalculationContainer
size_hint_y: None
height: self.minimum_height
row_force_default: True
row_default_height: '30dp'
cols: 4
spacing: 2, 0
padding: 1
BoxLayout:
...
BoxLayout:
size_hint: 1, .001
FloatLayout:
size: self.size
pos: self.pos
GridLayout:
id: SearchGrid
spacing: 2, 2
cols:1
size_hint: .975, None
height: self.minimum_height
row_force_default: True
row_default_height: '35dp'
pos_hint: {'x': 0 if root.SearchGridShown else 0,'top': 990}
opacity: .75
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
size: self.size
pos: self.pos
解决方案
推荐阅读
- javascript - 如何防止在 React 中使用纯子级重新渲染纯组件?
- python - 我需要用泡菜来保存字典
- java - Spring Data 中的 Pageable + @Query + JOIN (fetch?) 不起作用
- python - 定制词汇的一种热编码
- python - Python:如何在不耗尽内存的情况下提取 Google Cloud Storage 中的 Zip 文件?
- reactjs - React Suspense 没有按预期工作
- node.js - 将远程文件传递到 Node.js 应用程序中的 libreoffice-convert 库时收到“错误:没有这样的文件或目录,打开”
- autodesk-forge - 无法获取 /api/forge/oauth/callback
- python - 使用 pd.read_html 复制表
- python - POST 方法不适用于 Flask 应用程序 - 错误 404