使用 python 在 kivy gui 中运行时访问字典数据的问题

Problem in accessing the data of a dictionary at runtime in kivy gui with python

当数据在 运行 时间在字典中传输时,如何访问 class“Item_Menu”的字典“selected_items”的数据给用户在 Kivy gui 中选择复选框,我想在另一个 class“Description_item”中再次检索该数据,以便在按下按钮时在 kivy 的另一个屏幕上显示数据。

整个源代码:-

from kivymd.app import MDApp
from kivymd.uix.card import MDCard
from kivymd.uix.screen import MDScreen
from kivymd.uix.selectioncontrol import MDCheckbox
from kivymd.uix.textfield import MDTextField
from kivymd.uix.label import MDLabel
from kivymd.uix.boxlayout import MDBoxLayout
from kivy.uix.scrollview import ScrollView
from kivy.metrics import dp
from kivymd.uix.fitimage import FitImage
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.button import MDRaisedButton
from kivymd.uix.behaviors import RoundedRectangularElevationBehavior
from kivy.uix.screenmanager import Screen,ScreenManager
class my_mdcard(MDCard,RoundedRectangularElevationBehavior):
    pass

class Item_Menu(Screen,MDBoxLayout):
    list_of_information={"checkbox":[],"labels":[],"quantity":[]}
    selected_items= {"checkbox": [], "labels": [], "quantity": []}
    def __init__(self,**kwargs):
        super(Item_Menu,self).__init__(**kwargs)
        self.size_hint=(1,0.9)
        self.pos_hint={"top":1}
        self.orientation="vertical"
        scrollbarwin=ScrollView()
        content_box=BoxLayout(orientation='vertical',padding=dp(8),spacing=dp(8),size_hint=(1,None))
        content_box.bind(minimum_height=content_box.setter('height'))
        for i in range(0,50):
            Template_card= my_mdcard(
                size_hint_y=None,
                size_hint_x=.960,
                height = dp(100),
                padding = dp(4),
                pos_hint={'center_y': .5, 'center_x': .490},
                radius = [20,],
                elevation = 4,

            )
            checkbox=MDCheckbox(
                size_hint=(None, None),
                size= (dp(48),dp(48)),
                pos_hint={'center_y': .5}
            )
            checkbox.bind(active=lambda instance,value:self.Selected_checkbox(instance,value))
            self.list_of_information["checkbox"].append(checkbox)
            image_box=MDBoxLayout(adaptive_size=True)
            image=FitImage(
                source="D:/Study/Python/Kivy/images/2.jpg",
                size_hint= (None, None),
                height=dp(80),
                width=dp(130),
                radius=[12,],
                pos_hint={'center_y':0.5}

            )
            image_box.add_widget(image)
            text_box=MDBoxLayout(orientation="vertical",adaptive_height=True,pos_hint={'center_y':0.5},padding=[12,0,0,0])
            item_name=MDLabel(text=f"item{i+1}",font_style="H5",size_hint=(1,None),bold=True,theme_text_color="Primary")
            item_name.bind(texture_size=item_name.setter('size'))
            self.list_of_information["labels"].append(item_name)
            price=MDLabel(text=u"Price: \u20B910/per",font_style="Subtitle1",size_hint=(1,None),bold=True,theme_text_color="Hint")
            price.bind(texture_size=price.setter('size'))
            quantitybox=MDBoxLayout(orientation='vertical',adaptive_height=True,size_hint_x=0.2,pos_hint = {'center_y': .5,'center_x':0.5})
            quantityfield=MDTextField(
                hint_text= "Quantity",
                mode= "rectangle",
                size_hint=(None,None),
                width=dp(80),
                height= dp(40),
                padding=[0,0,15,0]
            )
            self.list_of_information["quantity"].append(quantityfield)
            quantitybox.add_widget(quantityfield)
            Template_card.add_widget(checkbox)
            Template_card.add_widget(image_box)
            Template_card.add_widget(text_box)
            text_box.add_widget(item_name)
            text_box.add_widget(price)
            Template_card.add_widget(quantitybox)
            content_box.add_widget(Template_card)
        scrollbarwin.add_widget(content_box)
        buttonbox=MDBoxLayout(orientation="vertical",pos_hint={"top":0.1},adaptive_height=True)
        button=MDRaisedButton(text="Selected!!!",size_hint=(1,0.2))
        
        button.bind(on_release=lambda x:self.Change_window(x))
        buttonbox.add_widget(button)
        self.add_widget(scrollbarwin)
        self.add_widget(buttonbox)
    def Selected_checkbox(self,instance,value):
        for i in range(0,len(self.list_of_information["checkbox"])):
            if instance==self.list_of_information["checkbox"][i] and value== True:
                self.selected_items["labels"].append(self.list_of_information["labels"][i])
                self.selected_items["quantity"].append(self.list_of_information["quantity"][i])
            elif instance==self.list_of_information["checkbox"][i] and value== False :
                self.selected_items["labels"].remove(self.list_of_information["labels"][i])
                self.selected_items["quantity"].remove(self.list_of_information["quantity"][i])
    def Change_window(self,instance):
        MyApp.sm.current="Item Description"
class Description_item(Screen):
    def __init__(self,**kwargs):
        super(Description_item,self).__init__(**kwargs)

        print(Item_Menu.selected_items)
        wholeContentBoxContainer=MDBoxLayout(orientation="vertical")

        for item in range(0,len(Item_Menu.selected_items["labels"])):
                label=MDLabel(text=Item_Menu.selected_items["labels"][item].text)
                print(Item_Menu.selected_items["labels"][item].text)
                quantity=MDLabel(text=Item_Menu.selected_items["quantity"][item].text)
                wholeContentBoxContainer.add_widget(label)
                wholeContentBoxContainer.add_widget(quantity)
        self.add_widget(wholeContentBoxContainer)
class MyApp(MDApp):
    sm=ScreenManager()
    def build(self):
        self.sm.add_widget(Item_Menu(name="Item Menu"))
        self.sm.add_widget(Description_item(name="Item Description"))
        self.theme_cls.theme_style="Dark"
        return self.sm


MyApp().run()

那么我如何访问在 运行 时间获取数据的字典的数据。 如果您有任何解决方案。所以,请让我知道这对我有很大帮助。如果你这样做了,我会感谢你的努力。谢谢!!

代码很难调试,但你的问题是你的第二个屏幕 Description_item 是在开始时创建的,所以它需要字典 selected_items 空 所以你应该在调用 Change_window 时创建它:

   def Change_window(self,instance):
        MyApp.sm.add_widget(Description_item(name="Item Description"))
        MyApp.sm.current="Item Description"

MyApp class 将如下所示:

class MyApp(MDApp):
    sm=ScreenManager()
    def build(self):
        self.sm.add_widget(Item_Menu(name="Item Menu"))
        self.theme_cls.theme_style="Dark"
        return self.sm