Kivy标签式面板。 __init __()添加小部件时不接受任何参数

时间:2019-08-15 14:17:16

标签: kivy

与标题相同。当我点击Add New Tab按钮时,出现此错误:

TypeError: object.__init__() takes no parameters

当我取消注释第二个CloseableHeader时,我可以看到此选项卡,但是我尝试将其添加为新tab的方式无效。

代码

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelHeader


theRoot = """
#:import Factory kivy.factory.Factory

<UserProfile>    
    canvas.before:
        Color:
            rgba: (1, 1, 1, 1)
        Rectangle:
            pos: self.pos
            size: self.size

<NewTabButton>
    orientation: 'vertical'
    Button:
        text: 'Add New Tab'
        on_press: root.change_tab()

<CloseableHeader>
    color: 0,0,0,0
    size_hint_x: None
    width: self.texture_size[0] + 40
    BoxLayout:
        pos: root.pos
        size_hint: None, None
        size: root.size
        padding: 3
        Label:
            id: lbl
            text: root.text
        BoxLayout:
            size_hint: None, 1
            orientation: 'vertical'
            width: 22
            Image:
                source: 'tools/theming/defaulttheme/close.png'
                on_touch_down:
                    if self.collide_point(*args[1].pos) :\
                        root.panel.remove_widget(root)

<MainTabbedPanel>:
    size_hint: (1, 1)
    do_default_tab: False
    #default_tab: edit_button_tab
    tab_width: 130

    FloatLayout:
        NewTabButton:
            id: new_tab_button

        UserProfile:
            id: menu_profile

    CloseableHeader:
        id: new_tab_button_tab
        text: 'Edit'
        content: new_tab_button.__self__
        #EDIT
        panel: root
    #CloseableHeader:
        #id: menu_profile_tab
        #text: 'Profile'
        #content: menu_profile.__self__

MainTabbedPanel:    
    id: main_tabbed_panel
"""

class NewTabButton(BoxLayout):

    def change_tab(self):
        App.get_running_app().root.add_header()

class MainTabbedPanel(TabbedPanel):

    def add_header(self):
        self.add_widget(CloseableHeader(panel=self, content=self.ids.menu_profile, text='Profile'))

class UserProfile(BoxLayout):
    pass

class CloseableHeader(TabbedPanelHeader):
    pass


sm = Builder.load_string(theRoot)

class TabbedPanelApp(App):
    def build(self):
        return sm

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

编辑

我发现需要将panel: root添加到kivy class

0 个答案:

没有答案