theStud54
theStud54

Reputation: 665

Kivy: AttributeError: 'NoneType' object has no attribute 'add_widget'

I am struggling as I learn Kivy, but I am making progress. I have an issue right now where I am getting the below error trace.

I feel like i have specified the area which I want to create the scrollview, but it won't allow it and complains that "NoneType" has no 'addWidget' attribute.

What I think I am doing is creating two layout sections within the "Menu" class rule. In that area, I have two "scrollview" widgets that I am building (one to hold the people "drafting" and the other to hold the "available players"). I am building out these scrollview buttons in the python script, but struggling to get them to take appropriately.

Thoughts?

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'

Here is the kv file:

#: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

And my python code:

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()

Upvotes: 0

Views: 3308

Answers (1)

ikolim
ikolim

Reputation: 16001

In kv file, move all your ObjectProperty hook-ups to after class rule, <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

Upvotes: 1

Related Questions