在kivy中在两个屏幕类之间传递变量

时间:2015-12-04 02:38:26

标签: python class kivy

我有两个屏幕类ScreenOneScreenTwo,我想在它们之间传递一个值。我正在使用ScreenManager,我正在使用"self.manager.current() = "screen2"

切换到ScreenTwo

我想在不使用.kv文件的情况下这样做。我不是编程的新手,但我是kivy的新手,我没想到这会如此复杂。无论如何,我可以通过另一个经理函数或类似的东西传递一个值吗?

    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 个答案:

没有答案