Search code examples
pythonkivylabelpositionkivy-language

Changing position of a Label in Kivy


i'm new in kivy programming and while it seems that there is a lot of documentation about this problem online, i don't seem to understand any of it so i hope you could help.

I have 4 Buttons and a label, by pressing the buttons, i'm hoping to move the label in that direction. I have two variables pX and pY which are the label's position and want it to update its position each time these two are updated. Thanks in advance.

// main.py
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.widget import Widget
from kivy.graphics import Rectangle, Color
from kivy.core.window import Window
from kivy.config import Config
from kivy.uix.floatlayout import FloatLayout

Window.size = (900, 600)
Config.set('graphics', 'resizable', True)


class FloatLayout(FloatLayout):
    pX = 0.6
    pY = 0.1


class FenetreApp(App):
    def build(self):
        return FloatLayout()


FenetreApp().run()
//fenetre.kv
<Button>:
     size_hint: 0.1, 0.1
     background_color: 0.1, 0.5, 0.6, 1


<Label>:
     size_hint: 0.1, 0.1
     background_color: 1, 0, 0, 1
     canvas.before:
          Color:
               rgb: 0.1, 0.6, 0
          Rectangle:
               pos: self.pos
               size: self.size

<FloatLayout>:
     Button:
          text: "Up"
          pos_hint: {"x":0.8, "top":1}
          on_press: root.pY= root.pY +0.1
     Button:
          text: "Down"
          pos_hint: {"x":0.8, "top":0.8}
          on_press: root.pY= root.pY -0.1
     Button:
          text: "Left"
          pos_hint: {"x":0.7, "top":0.9}
          on_press: root.pX= root.pX -0.1
     Button:
          text: "Right"
          pos_hint: {"x":0.9, "top":0.9}
          on_press: root.pX= root.pX +0.1


     Label:
          name: "L1"
          text: "I wanna move"
          pos_hint: {"x":root.pY, "top":root.pY} ```

Solution

  • You need to use NumericProperty for numeric values.Otherwise, kivy doesn't update its own childrens positions, texts and other stuffs.But if you don't want to use'em, check this code. I hope its clean to understand how it works: main.py:

    from kivy.app import App
    from kivy.core.window import Window
    from kivy.lang import Builder
    Window.size = (900, 600)
    kv = Builder.load_string('''
    FloatLayout:
        pY: .5
        pX: .5
        Button:
            size_hint:.1,.1
            background_color: 0.1, 0.5, 0.6, 1
            text: "Up"
            pos_hint: {"x":0.8, "y":.8}
            on_press: self.parent.pY+=.1
        Button:
            size_hint:.1,.1
            background_color: 0.1, 0.5, 0.6, 1
            text: "Down"
            pos_hint: {"x":0.8, "top":0.8}
            on_press: self.parent.pY-=.1
        Button:
            size_hint:.1,.1
            background_color: 0.1, 0.5, 0.6, 1
            text: "Left"
            pos_hint: {"x":0.7, "top":0.9}
            on_press: self.parent.pX-= .1
        Button:    
            size_hint:.1,.1
            background_color: 0.1, 0.5, 0.6, 1
            text: "Right"
            pos_hint: {"x":0.9, "top":0.9}
            on_press: self.parent.pX+=.1
        Label:
            size_hint: .1,.1
            text: "I like to moving moving"
            pos_hint: {"x":self.parent.pX, "top":self.parent.pY}
    ''')
    class sahm(App):
        def build(self):
            return kv
    if __name__ == '__main__':
        sahm().run()