Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python Kivy removing a label on the fly

ev66

New Coder
Kivy and Python GUI

I can create a label programmatically, but am unable to remove the same label

If the label is already part of the GUI, I can remove it, but not a label I have created after app is running.

The error is:
File "C:\Users\evan\Desktop\Raspberry\v10.py", line 21, in removeMyLabel
self.remove_widget(self.ids.new_lb)
^^^^^^^^^^^^^^^
Can you help please ?
Thanks
pythoncode:

Python:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.uix.label import Label

# manually setting in script
Window.size = (800, 480)


class YourWidget(Widget):
    def __init__(self, **kwargs):
        super(YourWidget, self).__init__(**kwargs)
        pass

    def addMyLabel(self):
        new_lb = Label(text="X", size_hint=(None, None), size=(50, 50), pos=(50, 50))
        self.add_widget(new_lb)

    def removeMyLabel(self):
        self.remove_widget(self.ids.new_lb)


class V10App(App):
    def build(self):
        Window.clearcolor = (0.2, 0.2, 0.2, 1)
        return YourWidget()


if __name__ == "__main__":
    V10App().run()

kv code:

Code:
<YourWidget>:
    Button:
        id: button1
        text: 'Add The Label'
        pos: (100, 320)
        size: (450, 50)
        size_hint: (None, None)
        on_release: root.addMyLabel()

    Button:
        id: button2
        text: 'Remove The Label'
        pos: (100, 220)
        size: (450, 50)
        size_hint: (None, None)
        on_release: root.removeMyLabel()
 
Solution
Ok. That makes sense. The id is part of the kv file.
I think I have found a way, by using the widget,children reference.

However, your solution is much better, and works fine.

Thanks ratwolf.
Kivy and Python GUI

I can create a label programmatically, but am unable to remove the same label

If the label is already part of the GUI, I can remove it, but not a label I have created after app is running.

The error is:

Can you help please ?
Thanks
pythoncode:

Python:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.uix.label import Label

# manually setting in script
Window.size = (800, 480)


class YourWidget(Widget):
    def __init__(self, **kwargs):
        super(YourWidget, self).__init__(**kwargs)
        pass

    def addMyLabel(self):
        new_lb = Label(text="X", size_hint=(None, None), size=(50, 50), pos=(50, 50))
        self.add_widget(new_lb)

    def removeMyLabel(self):
        self.remove_widget(self.ids.new_lb)


class V10App(App):
    def build(self):
        Window.clearcolor = (0.2, 0.2, 0.2, 1)
        return YourWidget()


if __name__ == "__main__":
    V10App().run()

kv code:

Code:
<YourWidget>:
    Button:
        id: button1
        text: 'Add The Label'
        pos: (100, 320)
        size: (450, 50)
        size_hint: (None, None)
        on_release: root.addMyLabel()

    Button:
        id: button2
        text: 'Remove The Label'
        pos: (100, 220)
        size: (450, 50)
        size_hint: (None, None)
        on_release: root.removeMyLabel()
Python:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.uix.label import Label

# manually setting in script
Window.size = (800, 480)


class YourWidget(Widget):
    def __init__(self, **kwargs):
        super(YourWidget, self).__init__(**kwargs)
        self.my_label = None  # Keep reference to label

    def addMyLabel(self):
        if not self.my_label:
            self.my_label = Label(text="X", size_hint=(None, None), size=(50, 50), pos=(50, 50))
            self.add_widget(self.my_label)

    def removeMyLabel(self):
        if self.my_label:
            self.remove_widget(self.my_label)
            self.my_label = None


class V10App(App):
    def build(self):
        Window.clearcolor = (0.2, 0.2, 0.2, 1)
        return YourWidget()


if __name__ == "__main__":
    V10App().run()

I guess you're getting the error because self.ids.new_lb only works for widgets defined in the KV file with an id. However, the label you're trying to remove (new_lb) is created dynamically in Python, and it has no id in the ids dictionary.

Solution​


You need to keep a reference to the dynamically created label so you can remove it later.
 
Last edited:
Ok. That makes sense. The id is part of the kv file.
I think I have found a way, by using the widget,children reference.

However, your solution is much better, and works fine.

Thanks ratwolf.
 
Last edited:
Solution
Back
Top Bottom