kivymd / KivyMD

KivyMD is a collection of Material Design compliant widgets for use with Kivy, a framework for cross-platform, touch-enabled graphical applications. https://youtube.com/c/KivyMD https://twitter.com/KivyMD https://habr.com/ru/users/kivymd https://stackoverflow.com/tags/kivymd
https://kivymd.readthedocs.io
MIT License
2.26k stars 674 forks source link

MDSegmentButton with Icon raises AttributeError #1735

Open dev8-de opened 2 months ago

dev8-de commented 2 months ago

Description of the Bug

if you use the MDSegmentedButtonIcon, the application will exit with an AttributeError:

AttributeError: 'NoneType' object has no attribute 'parent'

Code and Logs


from kivy.lang import Builder

from kivymd.uix.label import MDLabel
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.segmentedbutton import (
    MDSegmentedButton,
    MDSegmentedButtonItem,
    MDSegmentButtonIcon,
    MDSegmentButtonLabel,
)
from kivymd.app import MDApp

KV = '''
MDScreen:
    md_bg_color: self.theme_cls.backgroundColor

    MDBoxLayout:
        id: box
        orientation: "vertical"
        size_hint_x: .7
        adaptive_height: True
        spacing: "24dp"
        pos_hint: {"center_x": .5, "center_y": .5}
'''

class Example(MDApp):
    def on_start(self):
        for segment_type in ["large", "normal", "medium", "small"]:
            self.root.ids.box.add_widget(
                MDBoxLayout(
                    MDLabel(
                        text=f"Type '{segment_type}'",
                        adaptive_height=True,
                        bold=True,
                        pos_hint={"center_y": 0.5},
                        halign="center",
                    ),
                    MDSegmentedButton(
                        MDSegmentedButtonItem(
                            MDSegmentButtonIcon(icon='cancel'),
                            MDSegmentButtonLabel(
                                text="Songs",
                            ),
                        ),
                        MDSegmentedButtonItem(
                            MDSegmentButtonLabel(
                                text="Albums",
                            ),
                        ),
                        MDSegmentedButtonItem(
                            MDSegmentButtonLabel(
                                text="Podcasts",
                            ),
                        ),
                        type=segment_type,
                    ),
                    orientation="vertical",
                    spacing="12dp",
                    adaptive_height=True,
                )
            )

    def build(self):
        return Builder.load_string(KV)

Example().run()

Errormessage:

 Traceback (most recent call last):
   File "~/work/project/.venv/lib/python3.12/site-packages/kivy/lang/builder.py", line 245, in create_handler
     return eval(value, idmap), bound_list
            ^^^^^^^^^^^^^^^^^^
   File "MDSegmentedButton", line 55, in <module>
 AttributeError: 'NoneType' object has no attribute 'parent'

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "~/work/project/.venv/lib/python3.12/site-packages/kivy/lang/builder.py", line 699, in _apply_rule
     value, bound = create_handler(
                    ^^^^^^^^^^^^^^^
   File "~/work/project/.venv/lib/python3.12/site-packages/kivy/lang/builder.py", line 248, in create_handler
     raise BuilderException(rule.ctx, rule.line,
 kivy.lang.builder.BuilderException: Parser: File "MDSegmentedButton", line 52:
 ...
      50:    pos_hint: {"center_y": .5}
      51:    padding:
 >>   52:        "12dp", \
      53:        0, \
      54:        0 \
 ...
 AttributeError: 'NoneType' object has no attribute 'parent'
   File "~/work/project/.venv/lib/python3.12/site-packages/kivy/lang/builder.py", line 245, in create_handler
     return eval(value, idmap), bound_list
            ^^^^^^^^^^^^^^^^^^
   File "MDSegmentedButton", line 55, in <module>

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "~/work/project/app/segmentbutton.py", line 69, in <module>
     Example().run()
   File "~/work/project/.venv/lib/python3.12/site-packages/kivy/app.py", line 955, in run
     self._run_prepare()
   File "~/work/project/.venv/lib/python3.12/site-packages/kivymd/app.py", line 142, in _run_prepare
     super()._run_prepare()
   File "~/work/project/.venv/lib/python3.12/site-packages/kivy/app.py", line 950, in _run_prepare
     self.dispatch('on_start')
   File "kivy/_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
   File "~/work/project/app/segmentbutton.py", line 42, in on_start
     MDSegmentButtonIcon(icon='cancel'),
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "~/work/project/.venv/lib/python3.12/site-packages/kivymd/uix/label/label.py", line 739, in __init__
     super().__init__(*args, **kwargs)
   File "~/work/project/.venv/lib/python3.12/site-packages/kivymd/uix/behaviors/declarative_behavior.py", line 311, in __init__
     super().__init__(**kwargs)
   File "~/work/project/.venv/lib/python3.12/site-packages/kivymd/theming.py", line 977, in __init__
     super().__init__(**kwargs)
   File "~/work/project/.venv/lib/python3.12/site-packages/kivymd/uix/behaviors/backgroundcolor_behavior.py", line 164, in __init__
     super().__init__(**kwarg)
   File "~/work/project/.venv/lib/python3.12/site-packages/kivy/uix/label.py", line 319, in __init__
     super(Label, self).__init__(**kwargs)
   File "~/work/project/.venv/lib/python3.12/site-packages/kivy/uix/widget.py", line 366, in __init__
     self.apply_class_lang_rules(
   File "~/work/project/.venv/lib/python3.12/site-packages/kivy/uix/widget.py", line 470, in apply_class_lang_rules
     Builder.apply(
   File "~/work/project/.venv/lib/python3.12/site-packages/kivy/lang/builder.py", line 545, in apply
     self._apply_rule(
   File "~/work/project/.venv/lib/python3.12/site-packages/kivy/lang/builder.py", line 714, in _apply_rule
     raise BuilderException(rule.ctx, rule.line,
 kivy.lang.builder.BuilderException: Parser: File "MDSegmentedButton", line 52:
 ...
      50:    pos_hint: {"center_y": .5}
      51:    padding:
 >>   52:        "12dp", \
      53:        0, \
      54:        0 \
 ...
 BuilderException: Parser: File "MDSegmentedButton", line 52:
 ...
      50:    pos_hint: {"center_y": .5}
      51:    padding:
 >>   52:        "12dp", \
      53:        0, \
      54:        0 \
 ...
 AttributeError: 'NoneType' object has no attribute 'parent'
   File "~/work/project/.venv/lib/python3.12/site-packages/kivy/lang/builder.py", line 245, in create_handler
     return eval(value, idmap), bound_list
            ^^^^^^^^^^^^^^^^^^
   File "MDSegmentedButton", line 55, in <module>

   File "~/work/project/.venv/lib/python3.12/site-packages/kivy/lang/builder.py", line 699, in _apply_rule
     value, bound = create_handler(
                    ^^^^^^^^^^^^^^^
   File "~/work/project/.venv/lib/python3.12/site-packages/kivy/lang/builder.py", line 248, in create_handler
     raise BuilderException(rule.ctx, rule.line,

Versions

9acca9 commented 1 week ago

Im having this same issue. Do you find an answer? thanks

9acca9 commented 1 week ago

By the way, i just learn to create the MDialog... "components" in the .kv file. there you can use the MDSegmentButtonIcon without trouble!!!