'How can I trace why a variable is not populated at init time in Kivy GUI?

How is it possible that the Kivy GUI app is not load the whole code at the time of initialization?

Is it possible that when we called some function so the execution of the code which is inside that function starts at runtime.

The code is:

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()

When you run this code so you will see that at the time of initialization of the app the print function is getting print the the dictionary "selected_items" which I never want to be print it out at the time of initialization of the code.

Here is a picture will help you to better understand:

The arrow show the Dictionary which is print out at the time of initialization of the app

Due to this reason I am unable to access the data of the dictionary "selected_items" in the class "Description_item" because it points to empty list which is the value of the dictionary keys.

So how I can access the data of the dictionary "selected_items"?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source