如何使用Kivy

时间:2016-12-27 00:37:28

标签: python python-2.7 python-3.x kivy kivy-language

我有一些麻烦来转移元素列表,我从文件(在第一个屏幕中)到第二个和第三个屏幕。

我有3个屏幕由ScreenManager管理,每个屏幕都在不同的file.py中并且有一个file.kv. 这就是我的ScreenManager的样子(filename:login.py):

from interface1 import Interface1App
from interface2 import Interface2App

class LoginApp(App):
    def build(self):
        manager = ScreenManager()

        #add view login
        manager.add_widget(Login(name='login'))

        # add view 'interface1'
        app = Interface1App()
        app.load_kv()
        interfacen1 = app.build()
        manager.add_widget(interfacen1)

        # add view 'interface2'
        app2 = Interface2App()
        app2.load_kv()
        interfacen2 = app2.build()
        manager.add_widget(interfacen2)

        # add view 'interface3'
        app3 = Interface3App()
        app3.load_kv()
        interfacen3 = app3.build()
        manager.add_widget(interfacen3)

        manager.transition = SlideTransition(direction="left")
        return manager

在屏幕管理器登录中,我导入所有屏幕(例如:来自interface1 import Interface1App)

在第二个文件中,我在self.list中输入了一些数据,并将其用于该接口(filename:interface1.py):

from interface2 import Interface2App
class AllImage(Screen):
    CONTAINER_PNG = os.path.join(AllImage_ROOT, 'images')
    IMAGES_NAMES = [c[:-4] for c in os.listdir(CONTAINER_PNG)]

    def __init__(self, **kwargs):
        Screen.__init__(self, **kwargs)
        for im in IMAGES_NAMES:
            if IMAGES_NAMES != None :


                toggleimage = ToggleWithImage(src=im+'.png')
                toggleimage.bind(on_press=  lambda a, im=im:self.onpress_addpage(self.listim, im)) 
                self.layout.add_widget(toggleimage)

    def change_screen(self, x, list): 

        self.manager.transition = SlideTransition(direction="left")
        self.manager.current = 'interface2'
        self.manager.get_screen('interface2')
        self.manager.list1 = [1,2,3]


    def backtologin(self):
        self.manager.transition = SlideTransition(direction="right")
        self.manager.current = 'login'
        self.manager.get_screen('login')


class Interface1App(App):
    '''The kivy App that runs the main root. All we do is build a AllImage
    widget into the root.'''

    def build(self):

        screen = AllImage()
        screen.name = 'interface1'
        return screen

之后我想再次使用第二个接口(self.list)中收集的数据并在第三个接口中使用它(文件名:interace2.py):

from interface1 import AllImage #this isn't working
class Combinaison(Screen):


    screen = ObjectProperty(None)    

    def __init__(self, **kwargs):
        # self._previously_parsed_text = ''
        super(Combinaison, self).__init__(**kwargs)
        # self.list = list(self.manager.get_screen('interface1').list)
        print(self.manager.list1) # The problem is here <===========


    def backtointerface1(self):
        self.manager.transition = SlideTransition(direction="right")
        self.manager.current = 'interface1'
        self.manager.get_screen('interface1')

class Interface2App(App):
    '''The kivy App that runs the main root. All we do is build a Combinaison
    widget into the root.'''

    def build(self):
        screen = Combinaison()
        screen.name = 'interface2'
        return screen

使用此代码我有这个错误:print(self.manager.list1) AttributeError: 'NoneType' object has no attribute 'list1'

我想我不明白我应该如何使用self.manager.list1 = [1,2,3]

我希望我很清楚,对不起这篇长篇文章感到抱歉。 谢谢您的时间:))

1 个答案:

答案 0 :(得分:0)

您可以使用Screen.manager.get_screen(name).list = list(self.list)

一个完整的例子:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button


class Screen1(Screen):

    list = [1,2,3]

    def __init__(self,**kwargs):

        super(Screen1,self).__init__(**kwargs)
        self.switch_button = Button(text="goto 2")
        self.switch_button.bind(on_release=self.switch)
        self.add_widget(self.switch_button)

    def switch(self,*args):
        self.list = [4,5,6]
        self.manager.get_screen("screen2").list = list(self.list)
        self.manager.current = "screen2"


class Screen2(Screen):

    def __init__(self,**kwargs):

        super(Screen2,self).__init__(**kwargs)
        self.switch_button = Button(text="goto 1")
        self.switch_button.bind(on_release=self.switch)
        self.add_widget(self.switch_button)

    def switch(self,*args):
        self.manager.current = "screen1"

    def on_enter(self):
        print self.list


class MainApp(App):

    def build(self):

        sc1 = Screen1(name="screen1")
        sc2 = Screen2(name="screen2")

        self.sm = ScreenManager()

        self.sm.add_widget(sc1)
        self.sm.add_widget(sc2)

        return self.sm


MainApp().run()

您也可以使用ScreenManager存储列表。

self.manager.list1 = [1,2,3] # dont call this in __init__
self.manager.list2 = [4,5,6] # call it in on_enter instead

这些列表在添加到管理器后将在所有屏幕上显示。屏幕上的on_enter方法适用于您的示例。