【问题标题】:How to correctly link button by ids in Kivy?如何在 Kivy 中通过 id 正确链接按钮?
【发布时间】:2019-05-20 08:29:16
【问题描述】:

我想在 boxlayout 中添加按钮,但我的代码中存在一些问题。当我单击添加按钮或删除按钮时,我收到 AttributeError 消息。 谢谢你帮助我。

我的python代码:

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


class MainScreen(Screen):
    pass

class SecondScreen(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass


class TestApp(App):
    def build(self):
        self.title = 'Hello'

    def add_more(self):
        print('test')
        addbutton = self.root.ids.abc
        addbutton.add_widget(Button(text='hello'))

    def remove(self):
        print('test')
        if len(self.root.ids.abc.children) > 0:
            self.root.ids.abc.remove_widget(self.root.ids.abc.children[0])



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

我的kv码:

#: import SwapTransition kivy.uix.screenmanager.SwapTransition

ScreenManagement:
    transition: SwapTransition()
    MainScreen:
    SecondScreen:



<MainScreen>:
    BoxLayout:
        id:aaa
        Button:
            text: 'Add'
            on_press: app.add_more()

        Button:
            text:'Remove'
            on_press: app.remove()
        BoxLayout:
            id:abc


<SecondScreen>:

AttributeError: 'super' 对象没有属性 'getattr'

【问题讨论】:

    标签: python python-3.x kivy


    【解决方案1】:

    问题 - AttributeError

         addbutton = self.root.ids.abc
       File "kivy/properties.pyx", line 843, in kivy.properties.ObservableDict.__getattr__
     AttributeError: 'super' object has no attribute '__getattr__'
    

    根本原因

    根中没有id: abc,它是ScreenManager

    解决方案

    解决方案有两种选择。

    选项 1 - 使用 get_screen()

    在此选项中,我们使用get_screen() 函数检索实例化对象MainScreen:,以便我们可以访问其方法或属性,例如id: abc。我们将进行以下改进:

    kv 文件:

    • name 屏幕,name: 'MainScreen'name: 'SecondScreen' 用于实例化对象,MainScreen:SecondScreen: 分别。

    片段 - kv 文件

    ScreenManagement:
        transition: SwapTransition()
        MainScreen:
            name: 'MainScreen'
        SecondScreen:
            name: 'SecondScreen'
    

    Py 文件

    片段 - Py 文件

    def add_more(self):
        print('test')
        addbutton = self.root.get_screen('MainScreen').ids.abc
        addbutton.add_widget(Button(text='hello'))
    
    def remove(self):
        print('test')
        container = self.root.get_screen('MainScreen').ids.abc
        if len(container.children) > 0:
            container.remove_widget(container.children[0])
    

    选项 2 - 使用 id: mainscreen

    在此选项中,我们将id: mainscreen 添加到实例化对象MainScreen: 并使用ids.mainscreen 访问其方法或属性,例如id: abc。我们将进行以下改进:

    kv 文件:

    • id: mainscreen 添加到实例化对象MainScreen:

    片段 - kv 文件

    ScreenManagement:
        transition: SwapTransition()
        MainScreen:
            id: mainscreen
        SecondScreen:
    

    Py 文件

    • self.root.ids.abc 替换为self.root.ids.mainscreen.ids.abc

    片段 - Py 文件

    def add_more(self):
        print('test')
        addbutton = self.root.ids.mainscreen.ids.abc
        addbutton.add_widget(Button(text='hello'))
    
    def remove(self):
        print('test')
        container = self.root.ids.mainscreen.ids.abc
        if len(container.children) > 0:
            container.remove_widget(container.children[0])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-12
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      • 2017-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多