【问题标题】:Kivy: 'NoneType' object has no attribute 'bind'Kivy:“NoneType”对象没有“绑定”属性
【发布时间】:2015-05-31 07:23:03
【问题描述】:

我正在测试 Kivy 的 ActionBar 小部件,这是我的程序 -

 from kivy.app import App
 from kivy.lang import Builder
 from kivy.uix.boxlayout import BoxLayout

 Builder.load_string('''
 <RootWidget>:
      ActionBar:
          pos_hint: {'top':1}
      ActionView:
          ActionButton:
              text: "Button"
''')


class RootWidget(BoxLayout):
     pass

class MainApp(App):
     def build(self):
         return RootWidget()

if __name__ == "__main__":
     MainApp().run()

这里没什么,我只是在 BoxLayout 中添加了一个 ActionBar。
这是我正在执行程序的traceback

【问题讨论】:

  • 可能 RootWidget 需要一个 __init__ 来调用它的超级构造函数。
  • @PaulRooney 我刚刚尝试过,但它仍然给我同样的错误。
  • 如果你从你的构建方法中返回一个 BoxLayout 的实例怎么办。同样的错误?
  • @PaulRooney 你的意思是这个 - paste.ubuntu.com/11468542,如果我运行那个程序,我会得到同样的错误。

标签: python widget kivy attributeerror


【解决方案1】:

试试这样:

 <RootWidget>:
      ActionBar:
          pos_hint: {'top':1}
          ActionPrevious:
          ActionView:
              ActionButton:
                  text: "Button"

在您的情况下, ActionView 被视为 RootWidget 的子级,还要注意 ActionPrevious 。

【讨论】:

  • 我确实确认,如果您确实删除了“ActionPrevious”行,则会收到错误“widget.fbind('pos_hint', self._trigger_layout) AttributeError: 'NoneType' object has no attribute ' fbind''
【解决方案2】:

这是一个使用 ActionBar 的工作示例:

actionbardemo.kv

ActionBarDemo:

    # Reference actionbardemo.py
    #: import main actionbardemo

        <ActionBarDemo>:
            orientation: "vertical"
            padding: 10
            spacing: 10
            canvas.before:
                Color:
                    rgb: [0.22,0.22,0.22]
                Rectangle:
                    pos: self.pos
                    size: self.size

            BoxLayout:
                size_hint_y: None
                ActionBar:
                    pos_hint: {'top':1}
                    ActionView:
                        use_separator: True
                        ActionPrevious:
                        ActionOverflow:
                            ActionButton:
                                text: 'Menu 1'
                                on_press: root.menuOne()
                            ActionButton:
                                text: 'Menu 2'
                                on_press: root.menuTwo()
            BoxLayout:
            TextInput:

actionbardemo.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.label import Label

class ActionBarDemo(BoxLayout):
    def menuOne(menu):
        popup = Popup(title='Popup', content=Label(text='Menu one'), size_hint=(None, None), size=(400, 400))
        popup.open()

    def menuTwo(menu):
        popup = Popup(title='Popup', content=Label(text='Menu two'), size_hint=(None, None), size=(400, 400))
        popup.open()

class ActionBarDemoApp(App):
    def build(self):
        return ActionBarDemo()

if __name__== '__main__':
    dbApp = ActionBarDemoApp()

    dbApp.run()

结果:

【讨论】:

    猜你喜欢
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多