Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MDSegmentButton with Icon raises AttributeError #1735

Open
dev8-de opened this issue Sep 8, 2024 · 2 comments
Open

MDSegmentButton with Icon raises AttributeError #1735

dev8-de opened this issue Sep 8, 2024 · 2 comments

Comments

@dev8-de
Copy link

dev8-de commented Sep 8, 2024

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

  • OS: Arch Liniux
  • Python: 3.12
  • Kivy: 2.3.0
  • KivyMD: 2.0.1dev (2024-08-07)
@9acca9
Copy link

9acca9 commented Nov 17, 2024

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

@9acca9
Copy link

9acca9 commented Nov 18, 2024

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

alpianon added a commit to alpianon/KivyMD that referenced this issue Dec 13, 2024
…style

In segmentedbutton.kv, check if MDSegmentButtonIcon has a
parent.parent before checking container's parent _label, otherwise
you may get the following error:
AttributeError: 'NoneType' object has no attribute 'parent'

Fix kivymd#1735

Signed-off-by: Alberto Pianon <[email protected]>
alpianon added a commit to alpianon/KivyMD that referenced this issue Dec 13, 2024
In segmentedbutton.kv, we need to check if the MDSegmentButtonIcon
object actually has a parent.parent before checking container's parent
_label, otherwise you may get the following error: AttributeError:
'NoneType' object has no attribute 'parent'

Fix kivymd#1735

Signed-off-by: Alberto Pianon <[email protected]>
alpianon added a commit to alpianon/KivyMD that referenced this issue Dec 13, 2024
In segmentedbutton.kv, we need to check if the MDSegmentButtonIcon
object actually has a parent.parent before checking container's parent
_label, otherwise you may get the following error: AttributeError:
'NoneType' object has no attribute 'parent'

Fix kivymd#1735

Signed-off-by: Alberto Pianon <[email protected]>
alpianon added a commit to alpianon/KivyMD that referenced this issue Dec 13, 2024
In segmentedbutton.kv, we need to check if the MDSegmentButtonIcon
object actually has a parent.parent before checking container's parent
_label, otherwise you may get the following error: AttributeError:
'NoneType' object has no attribute 'parent'

Fix kivymd#1735

Signed-off-by: Alberto Pianon <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants