Home > other >  Kivy Label's text overlapping when it's updates
Kivy Label's text overlapping when it's updates

Time:09-19

I have problem: when I try to update Label's text, which was initialized from .kv, it ovelaps original and doesn't metter how many times I update it or will I try to clear it, the only working method is initialize Label's text from App build method (this is example from 32nd exercise youtube lesson: original message right after run

Error with overlapping

ExampleApp.kv example:

# Spinner example exercise 32
<CustomLayout>:
    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height
    
        Label:
            id: click_label
            text: "Issuer"
            font_size: 22
        
        Spinner:
            id: spinner_id
            text: "Issuer"
            values: ["ip1", "ip2", "ip3", "ip4"]
            on_text: root.spinner_clicked(spinner_id.text)  # here Label's text updates
        
        BoxLayout:

Python 3's script example (example.py):

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder

# Spinner example exercise 32
Builder.load_file("ExampleApp.kv")


class CustomLayout(Widget):
    # here Label's text updates
    def spinner_clicked(self, value):
        self.ids.click_label.text = f"Issuer: {value}"


class ExampleApp(App):

    def build(self):
        # These 3 strings works correctly
        # main_widget = CustomLayout()
        # main_widget.ids.click_label.text = "Issuer: ???"
        # return main_widget
        # This one - not
        return CustomLayout()


if __name__ == '__main__':
    ExampleApp().run()

my kivy packages:

Kivy==2.1.0
kivy-deps.angle==0.3.2
kivy-deps.glew==0.3.1
kivy-deps.sdl2==0.4.5
Kivy-examples==2.1.0
Kivy-Garden==0.1.5

CodePudding user response:

As you've noticed, your .kv file has been loaded twice first with method Builder.load_file and thereafter with naming convention (here ExampleApp.kv will be converted into example.kv during loading). This is why it has to update the same gui twice every time.

The solution is quite straightforward. Keep any one and discard other. I.e. either follow naming convention for the main gui (and don't use Builder.load_file or Builder.load_string) or use those method(s) but using different file name (that follows naming convention).

Lastly to examine the double updation (or overlapping text), you can modify your .kv as,

...
        Label:
            id: click_label
            text: "Issuer: "
            font_size: 22
        
        Spinner:
            id: spinner_id
            text: "Issuer"
            values: ["", "ip1", "ip2", "ip3", "ip4"] # Now select the first value.
...
  • Related