Only one root object is allowed by .kv

68 views
Skip to first unread message

Nightslayer Neptune

unread,
Jun 25, 2019, 4:57:45 PM6/25/19
to Kivy users support
I wanna to hide a Label, when the button is pressed.
Then release it when another button is pressed.
(NOW, i AM JUST DOING THE FIRST PART: HIDING)

This is my .kv

 10 Button:
 11     size_hint_y: .1
 12     size_hint_x: .1
 13     on_press: root.label.visible = False
 14 Label:
 15     id: label
 16     visible: True
 17     size_hint_x: 1 if self.visible else 0
 18     opacity: 1 if self.visible else 0
 19     disabled: not self.visible

and this is my python file

  1 from kivy.uix.button import Button
  2 from kivy.app import App
  3 from kivy.uix.screenmanager import ScreenManager, Screen
  4 class HelloApp(App):
  5     pass
  6
  7 if __name__ == '__main__':
  8     HelloApp().run()
when i run it it said 
Only one root object is allowed by .kv

Elliot Garbus

unread,
Jun 25, 2019, 8:11:22 PM6/25/19
to kivy-...@googlegroups.com

 

In your KV file there can be only one root widget.  The class instances, Button and Label are both on the far left, so each is root widget.

A solution is to organize them under a layout.  Now there is one root, the BoxLayout.

 

BoxLayout:

    Button:
         size_hint_y: .1
         size_hint_x: .1
         on_press: root.label.visible = False
    Label:
       id: label
        visible: True


        size_hint_x: 1 if self.visible else 0

        opacity: 1 if self.visible else 0

        disabled: not self.visible

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To post to this group, send email to kivy-...@googlegroups.com.
Visit this group at https://groups.google.com/group/kivy-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/0405b4de-5aac-4ff0-bcef-996febf55478%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

 

Prabhat Mohanty

unread,
Jun 26, 2019, 1:06:44 AM6/26/19
to kivy-...@googlegroups.com
Should not it be
 self.visible = True
?

--

Nightslayer Neptune

unread,
Jun 26, 2019, 10:49:42 AM6/26/19
to Kivy users support
After I changed to this.
When I tried to click the button, it said AttributeError: ‘BoxLayout’ object has no attribute ‘ label’

Elliot Garbus於 2019年6月25日星期二 UTC-4下午8時11分22秒寫道:

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Nightslayer Neptune

unread,
Jun 26, 2019, 10:50:07 AM6/26/19
to Kivy users support
Which sentence you mean??

Prabhat Mohanty於 2019年6月26日星期三 UTC-4上午1時06分44秒寫道:
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages