Kivy从外面添加小部件

时间:2016-07-16 10:01:34

标签: android python kivy

我想创建一个函数,为类添加一些小部件,以“更新”小部件的数量。 当我在Eventbar类中执行Update函数时,它可以工作,但是当我在Sidebar类中执行它时,它拒绝添加小部件并且只打印“Update works”。

为什么会如此,如何从Sidebar类执行Update?

以下是代码,所有内容都已导入:

from scrollbar import *
from netclient import Netclient
from setjson import settings_json


class SM(ScreenManager):
    pass

class NormalScreen(Screen):
    pass

class Seidebar(ScrollView): # Sidebar -> use to choose the categories
    texte = ListProperty()
    images = ListProperty()

    def __init__(self, add_button='ERROR', **kwargs):
        super(Seidebar, self).__init__(**kwargs)
        self.add_button = 'error'

        self.size_hint_x = 0.3

        self.my_app = App.get_running_app()
        #print my_app

        self.layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
        # Make sure the height is such that there is something to scroll.
        self.layout.bind(minimum_height=self.layout.setter('height'))

        self.btn2 = Button(size_hint_y=None, height=40)

        #functions of the buttons

        self.btn2.bind(on_press=self.change_cat_to_Music)

        #end of funktions

        self.layout.add_widget(self.btn2)

        self.add_widget(self.layout)

#### change cathegorie Funktions ###

    def change_cat_to_Music(self, *args):
        kv_string.ids.norscreen.ids.eventbar.net.current = 'Music'#changes current cathegory
        print 'cat changed'
        netclient = kv_string.ids.norscreen.ids.eventbar.net #gets the netclient again
        kv_string.ids.norscreen.ids.eventbar.Update(netclient.giveWid)



class ThreeBars(Image): # The Button at the right top corner

    add_button = StringProperty()
    all_button = StringProperty()
    music_button = StringProperty()
    impressum_button = StringProperty()
    settings = StringProperty()

    def __init__(self, **kwargs):
        super(ThreeBars, self).__init__(**kwargs)
        self.source = 'images.png'
        self.sidebarexists = False
        self.sidebar = Seidebar(add_button = self.add_button, height=Window.height * 0.8, pos_hint={'x': 0, 'top':0.5})

    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):

            if self.sidebarexists == False:

                kv_string.ids.norscreen.ids.Specialbox.clear_widgets()
                kv_string.ids.norscreen.ids.Specialbox.add_widget(self.sidebar)
                kv_string.ids.norscreen.ids.Specialbox.add_widget(Eventbar(do_scroll_x = False))

                self.sidebarexists = True
            elif self.sidebarexists == True:
                kv_string.ids.norscreen.ids.Specialbox.remove_widget(self.sidebar)
                self.sidebarexists = False

    def Update(self, giveW, *args):
        kv_string.ids.norscreen.ids.tbars.Update(giveW)

class Eventbar(Scrollbar): #  Displays the Images

    def __init__(self, **kwargs):
        super(Eventbar, self).__init__(**kwargs)
        self.net = Netclient() ##the one and only netclient
        #self.Update(self.net.giveWid)

    def Update(self, giveW, *args): #use to change the displayed events

        self.layout.clear_widgets()

        texte, images, description = self.net.giveWid() #getting the Text

        for i in range(len(texte)):
            btn = Button(text=str(i), size_hint_y=None, height=40)
            wid = Sprite(source='unlock.png')

            self.layout.add_widget(Label(text=texte[i], size_hint=(1,None), size_x=Window.size[1]*0.05, font_size='20sp'))
            self.layout.add_widget(AsyncSprite(source=images[i],size=(300,300),size_hint=(1,None),number=i))
        print 'Update works'

kv_string = Builder.load_string('''
SM:
    id: sm
    NormalScreen:
        id: norscreen
        name: 'NorScreen'
        manager: sm

<NormalScreen>:

    BoxLayout:
        orientation: 'vertical'

        FloatLayout:

            size_hint_y: 0.1

            ThreeBars:
                pos_hint: {"x": -0.45, 'top':0.9}
                id: tbars

            Label:
                pos_hint: {"x": 0.05, 'top':0.9}
                text: 'Latest events'
                size_hint_x: .9

        BoxLayout:
            id: Specialbox
            orientation: 'horizontal'
            Eventbar:
                do_scroll_x: False
                id: eventbar
''')

class Test(App):
    def build(self):
        return kv_string

if __name__ == '__main__':
    Test().run()

0 个答案:

没有答案