简体   繁体   English

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

[英]Kivy: AttributeError: 'NoneType' object has no attribute 'add_widget'

I am struggling as I learn Kivy, but I am making progress.我在学习 Kivy 的过程中很挣扎,但我正在取得进步。 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.我觉得我已经指定了我想要创建滚动视图的区域,但它不允许它并抱怨“NoneType”没有“addWidget”属性。

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").在那个区域,我有两个正在构建的“scrollview”小部件(一个用于容纳“起草”人员,另一个用于容纳“可用玩家”)。 I am building out these scrollview buttons in the python script, but struggling to get them to take appropriately.我正在 python 脚本中构建这些滚动视图按钮,但努力让它们适当地使用。

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:这是 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

And my python code:还有我的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()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kivy - AttributeError: 'NoneType' object 没有属性 'add_widget' - Kivy - AttributeError: 'NoneType' object has no attribute 'add_widget' 在猕猴桃中制作下拉列表。 &#39;NoneType&#39;对象没有属性&#39;add_widget&#39; - Making dropdown list in kivy. 'NoneType' object has no attribute 'add_widget' Kivy AttributeError: 'NoneType' object 没有属性 'ids' - Kivy AttributeError: 'NoneType' object has no attribute 'ids' AttributeError:&#39;NoneType&#39;对象没有属性&#39;text&#39;kivy - AttributeError: 'NoneType' object has no attribute 'text' kivy Kivy AttributeError: 'NoneType' object 没有属性 'current' - Kivy AttributeError: 'NoneType' object has no attribute 'current' AttributeError:“ NoneType”对象没有属性“ add” - AttributeError: 'NoneType' object has no attribute 'add' Python-Kivy AttributeError: 'NoneType' object 没有属性 'text' - Python-Kivy AttributeError: 'NoneType' object has no attribute 'text' Kivy Builder: AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;text&#39; - Kivy Buildozer: AttributeError: 'NoneType' object has no attribute 'text' Python Kivy AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;bind&#39; - Python Kivy AttributeError: 'NoneType' object has no attribute 'bind' Kivy AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;get_screen&#39; - Kivy AttributeError: 'NoneType' object has no attribute 'get_screen'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM