python - 在 Kivy 屏幕管理器中使用全局变量
问题描述
我是一个初学者,我正在尝试制作一个应用程序,我可以首先从预定列表中选择项目,然后在下一个屏幕上查看仅包含这些选定项目的列表。
我用 2 个列制作了一个网格:复选框和标签。我从这些列中制作了列表并将它们压缩在一起以接收相应复选框索引具有“向下”状态的项目的“结果”列表。
但是,我似乎无法将此更新result
的列表从 ShopInput 类中获取到 SecondWindow 类中。
我已经尝试了很多我在这里找到的东西,但我似乎完全被卡住了。谁能指导我如何继续在第二个屏幕上更新结果列表?
我的代码:
购物清单.py:
import kivy
from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.uix.checkbox import CheckBox
from kivy.uix.scrollview import ScrollView
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
result = ["test", "test2", "test3"]
item_dictionary = {
"item1": "Cookies",
"item2": "Milk",
"item3": "Honey"
}
class ShopInput(Screen):
obj_listitem1 = ObjectProperty(None)
obj_listitem2 = ObjectProperty(None)
obj_listitem3 = ObjectProperty(None)
def button_press(self):
products = list(item_dictionary.values())
added_item_list = []
added_item_list.append(self.obj_listitem1.state)
added_item_list.append(self.obj_listitem2.state)
added_item_list.append(self.obj_listitem3.state)
global result
result.clear()
result.append([x for x, y in zip(products, added_item_list) if y == 'down'])
print(result) # Temporarily to see what gets added to the result list
added_item_list.clear()
class SecondWindow(Screen):
def __init__(self, **kwargs):
super(SecondWindow, self).__init__(**kwargs)
global result
self.add_widget(Label(text=(f'{result}')))
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("shoppinglist.kv")
class ShoppingListApp(App):
def build(self):
return kv
if __name__ == "__main__":
ShoppingListApp().run()
购物清单.kv:
WindowManager:
ShopInput:
SecondWindow:
<ShopInput>:
name: "first"
obj_listitem1: listitem1
obj_listitem2: listitem2
obj_listitem3: listitem3
GridLayout:
cols: 1
rows: 3
size: root.width, root.height
padding: 2
Label:
text:"Make Your Shopping List"
size_hint_y:0.2
ScrollView:
do_scroll_x: False
do_scroll_y: True
size_hint_y:0.675
GridLayout:
cols: 2
size_hint_y: 1.5
height: self.minimum_height
Label:
text: "Add to list:"
Label:
text: "Product:"
CheckBox:
id: listitem1
Label:
text: "Cookies"
CheckBox:
id: listitem2
Label:
text: "Milk"
CheckBox:
id: listitem3
Label:
text: "Honey"
GridLayout:
cols: 2
size_hint_y:0.075
Button:
text:"Add"
on_press: root.button_press()
Button:
text:"Next"
on_release: root.manager.current = "second"
<SecondWindow>:
name: "second"
谢谢!
解决方案
您正在尝试在其方法中设置Label
文本,该方法在用户选择任何内容之前很久就被调用。尝试将课程更改为:SecondWindow
__init__()
Builder.load_file()
SecondWindow
class SecondWindow(Screen):
def __init__(self, **kwargs):
super(SecondWindow, self).__init__(**kwargs)
print('init')
def on_enter(self, *args):
global result
self.add_widget(Label(text=(f'{result}')))
这会延迟设置 ,Label
text
直到SecondWindow
使用 的on_enter()
方法显示Screen
。
推荐阅读
- ios - 在没有 info.plist 键的情况下启用 Face-ID?
- php - Laravel 5.7 Passport 不断重定向到登录
- deep-learning - 寻找用于商业用途的新手写数据集
- javascript - 如何替换多个字符而不是使用替换 n 次
- angular - rxjs takeUntil 不执行 finalize
- javascript - 如何自定义winston输出
- python - Matplotlib png 输出在 reportlab 的 pdf 中出现“损坏”
- kubernetes - 如何在标签中更新和附加值以进行 kubernetes 部署
- javascript - 过滤元素 jQuery 以 exp 开头
- python - Scrapy cnn.com 搜索