Kivy:AttributeError:'NoneType'对象没有属性'add_widget'

时间:2018-08-21 22:44:13

标签: python kivy kivy-language

在学习Kivy的过程中,我很挣扎,但是我正在进步。我现在遇到以下错误跟踪,这是一个问题。

我感觉我已经指定了要创建滚动视图的区域,但它不允许这样做,并抱怨“ NoneType”没有“ addWidget”属性。

我想我正在做的是在“菜单”类规则中创建两个布局部分。在该区域中,我正在构建两个“ scrollview”小部件(一个用于容纳人员“草稿”,另一个用于容纳“可用玩家”)。我在python脚本中构建了这些scrollview按钮,但努力使它们正确使用。

有想法吗?

Returning 331 undrafted players.
[INFO   ] [Base        ] Leaving application in progress...
 Traceback (most recent call last):
   File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1668, in <module>
     main()
   File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1662, in main
     globals = debugger.run(setup['file'], None, None, is_module)
   File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1072, in run
     pydev_imports.execfile(file, globals, locals)  # execute the script
   File "/Users/Miller/GitHub/nfldb-data/gui_kivy/app.py", line 95, in <module>
     MyAppli().run()
   File "/Users/Miller/GitHub/nfldb-data/nfl_env/lib/python2.7/site-packages/kivy/app.py", line 826, in run
     runTouchApp()
   File "/Users/Miller/GitHub/nfldb-data/nfl_env/lib/python2.7/site-packages/kivy/base.py", line 502, in runTouchApp
     EventLoop.window.mainloop()
   File "/Users/Miller/GitHub/nfldb-data/nfl_env/lib/python2.7/site-packages/kivy/core/window/window_sdl2.py", line 727, in mainloop
     self._mainloop()
   File "/Users/Miller/GitHub/nfldb-data/nfl_env/lib/python2.7/site-packages/kivy/core/window/window_sdl2.py", line 460, in _mainloop
     EventLoop.idle()
   File "/Users/Miller/GitHub/nfldb-data/nfl_env/lib/python2.7/site-packages/kivy/base.py", line 337, in idle
     Clock.tick()
   File "/Users/Miller/GitHub/nfldb-data/nfl_env/lib/python2.7/site-packages/kivy/clock.py", line 581, in tick
     self._process_events()
   File "kivy/_clock.pyx", line 384, in kivy._clock.CyClockBase._process_events
   File "kivy/_clock.pyx", line 414, in kivy._clock.CyClockBase._process_events
   File "kivy/_clock.pyx", line 412, in kivy._clock.CyClockBase._process_events
   File "kivy/_clock.pyx", line 167, in kivy._clock.ClockEvent.tick
   File "/Users/Miller/GitHub/nfldb-data/gui_kivy/app.py", line 60, in create_scrollview
     self.players_view.add_widget(scrollview)
 AttributeError: 'NoneType' object has no attribute 'add_widget'

这是kv文件:

#:kivy 1.10.0

<AppScreenManager>:
    Menu:

<Menu>:

    BoxLayout:
        orientation: 'horizontal'

        BoxLayout:
            drafters_view: drafters_view
            orientation: 'vertical'
            size_hint: .6, 1

            Label:
                text: "CMBoys - 2018 LA Draft"

            BoxLayout:
                orientation: 'horizontal'

                Label:
                    id: current_pick
                    text: 'Round: ' + root.round_str

                Label:
                    text: 'Pick: ' + root.pick_str

                Label:
                    text: 'Remaining Picks: ' + root.picks_remaining_str

                Label:
                    text: 'Up: ' + root.current_person

            Label:
                id: label_player_selected
                text: 'Selected: ' + root.player_selected_name

            ScrollView:
                id: drafters_view
                size_hint: 1, .9


        BoxLayout:
            players_view: players_view
            orientation: 'vertical'
            size_hint: .4, 1

            Label:
                text: "Available Players"
                size_hint: 1, .1

            ScrollView:
                id: players_view
                size_hint: 1, .9

我的python代码:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window
from kivy.properties import ObjectProperty, StringProperty, NumericProperty
from kivy.clock import Clock


class AppScreenManager(ScreenManager):

    def __init__(self, **kwargs):
        super(AppScreenManager, self).__init__(**kwargs)


class Menu(Screen):
    players_view = ObjectProperty(None)
    drafters_view = ObjectProperty(None)
    player_selected_name = StringProperty()
    player_selected_id = StringProperty()
    pick = NumericProperty()
    pick_str = StringProperty()
    round = NumericProperty()
    round_str = StringProperty()
    picks_remaining = NumericProperty()
    picks_remaining_str = StringProperty()
    current_person = StringProperty()

    def __init__(self, **kwargs):
        super(Menu, self).__init__(**kwargs)
        Clock.schedule_once(self.create_scrollview)
        Clock.schedule_once(self.create_drafting_scrollview)
        self.player_selected_name = 'No Player Selected.'
        self.player_selected_id = ''
        self.pick = 1
        self.pick_str = str(self.pick)
        self.round = 1
        self.round_str = str(self.round)
        self.picks_remaining = str(round((12*16),0))
        self.picks_remaining_str = str(self.picks_remaining)
        self.draft_order = ['Millz', 'Basso', 'Dronas', 'Doug', 'Mass', 'Russ', 'Greene', 'Greer', 'Rich', 'Lype', 'Ditty', 'Nowe']
        self.current_person = self.draft_order[0]

    def create_scrollview(self, dt):
        layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
        layout.bind(minimum_height=layout.setter("height"))

        for person in ['player 1', 'player 2', 'player 3','player 4']:
            btn = Button(id=person, text=person, size=(40, 40), size_hint=(1, None),
                         background_color=(0.5, 0.5, 0.5, 1), color=(1, 1, 1, 1), on_press=self.player_select)
            layout.add_widget(btn)

        scrollview = ScrollView(size_hint=(1, None), size=(Window.width, Window.height))
        scrollview.add_widget(layout)
        self.players_view.add_widget(scrollview)

    def create_drafting_scrollview(self,dt):
        layout = GridLayout(cols=1, spacing=10, size_hint_y=None)
        layout.bind(minimum_height=layout.setter("height"))

        for person in ['person 1','person 2','person 3']:
            btn = Button(text=person, size=(40, 40), size_hint=(1, None),
                         background_color=(0.5, 0.5, 0.5, 1), color=(1, 1, 1, 1))
            layout.add_widget(btn)

        scrollview = ScrollView(size_hint=(1, None), size=(Window.width, Window.height))
        scrollview.add_widget(layout)
        self.drafters_view.add_widget(scrollview)

    def player_select(self, instance):
        # Update the Label:
        self.player_selected_name = instance.text
        self.player_selected_id = instance.id
        # Do Something:
        print 'Button pressed!!'
        print instance.text
        print instance.id


Builder.load_file("debug.kv")


class MyAppli(App):

    def build(self):
        return AppScreenManager()


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

1 个答案:

答案 0 :(得分:1)

在kv文件中,将所有ObjectProperty关联移动到类规则<Menu>:之后

debug.kv

#:kivy 1.10.0

<AppScreenManager>:
    Menu:

<Menu>:
    drafters_view: drafters_view
    players_view: players_view

    BoxLayout:
        orientation: 'horizontal'

        BoxLayout:
            orientation: 'vertical'
            size_hint: .6, 1

            Label:
                text: "CMBoys - 2018 LA Draft"

            BoxLayout:
                orientation: 'horizontal'

                Label:
                    id: current_pick
                    text: 'Round: ' + root.round_str

                Label:
                    text: 'Pick: ' + root.pick_str

                Label:
                    text: 'Remaining Picks: ' + root.picks_remaining_str

                Label:
                    text: 'Up: ' + root.current_person

            Label:
                id: label_player_selected
                text: 'Selected: ' + root.player_selected_name

            ScrollView:
                id: drafters_view
                size_hint: 1, .9


        BoxLayout:
            orientation: 'vertical'
            size_hint: .4, 1

            Label:
                text: "Available Players"
                size_hint: 1, .1

            ScrollView:
                id: players_view
                size_hint: 1, .9