passing variables between two screen classes in ki

2019-09-11 19:48发布

I have two screen classes ScreenOne and ScreenTwo and I would like to pass a value between them. I am using the ScreenManager and I am switching to the ScreenTwo using "self.manager.current() = "screen2"

I would like to do this without the use of a .kv file. I am not new to programming, but I am new to kivy and I did not expect this to be so complicated. Is there anyway I could pass a value through another manager function or something along those lines?

    class ScreenOne(Screen):

        def __init__(self,**kwargs):
            super (ScreenOne,self).__init__(**kwargs)

            my_box1 = GridLayout(cols=3,spacing=50,size_hint_y=None,size_y=100,)
            my_box1.bind(minimum_height=my_box1.setter('height'))

            drink_list = get_drink_list()

            for drink in drink_list:
                button = Button(text=drink,
                    size_hint_y=None, height=250, background_normal=get_drink_image(drink),font_size='15dp',color=[0,0,0,1])
                button.bind(on_press=self.changer)
                my_box1.add_widget(button)

            root = ScrollView(size_hint=(None, None), size=(600, 400),
                pos_hint={'center_x':.5, 'center_y':.5},bar_width='10dp')
            root.add_widget(my_box1)
            self.add_widget(root)

        def changer(self, instance,*args):
            self.manager.current = instance.text #button name

class ScreenTwo(Screen):

    def __init__(self,**kwargs):
        super (ScreenTwo,self).__init__(**kwargs)

        # print drink_ordered
        #creating layouts
        my_box1 = GridLayout(cols=2)
        description_box = BoxLayout(orientation='vertical', spacing=30)

        #creating objects
        drink_image = Image(source="drink_pics/" + self.name + ".jpg")
        drink_name = Label(text=self.name,font_size='24dp',size_hint_y=None, height=100)
        empty_box = Label(text="",font_size='24dp',size_hint_y=None, height=20)

        ingredient_list = get_drink_ingredients(self.name)
        description = ''

        for count in range(len(ingredient_list)):
            if ((count % 3 == 0) and (count != 0)):
                description += "\n"

            description += ingredient_list[count] + ", "

        description = description[:-2]
        description = description.replace('_', ' ')

        drink_description = Label(text="Description: " + description,font_size='15dp'
            ,size_hint_y=None, height=100)
        drink_order = Button(text="Order Drink",
                size_hint_y=None, height=100, size_hint_x=None, width=385, padding=[100,100])
        drink_order.bind(on_press=self.changer)

        #adding to widgets
        my_box1.add_widget(drink_image)
        description_box.add_widget(drink_name)
        description_box.add_widget(drink_description)
        description_box.add_widget(drink_order)
        description_box.add_widget(empty_box)
        my_box1.add_widget(description_box)
        self.add_widget(my_box1)

    def changer(self,*args):
        self.manager.current = 'next screen'

0条回答
登录 后发表回答