Ändern der Position eines Labels in Kivy

Aug 16 2020

Ich bin neu in der Kivy-Programmierung und obwohl es scheint, dass es online eine Menge Dokumentation zu diesem Problem gibt, scheine ich nichts davon zu verstehen, also hoffe ich, dass Sie helfen können.

Ich habe 4 Knöpfe und ein Etikett, durch Drücken der Knöpfe hoffe ich, das Etikett in diese Richtung zu bewegen. Ich habe zwei Variablen pX und pY, die die Position des Labels darstellen, und möchte, dass es seine Position jedes Mal aktualisiert, wenn diese beiden aktualisiert werden. Danke im Voraus.

// 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} ```

Antworten

1 320V Aug 16 2020 at 08:48

Sie müssen NumericProperty für numerische Werte verwenden. Andernfalls aktualisiert kivy die Positionen, Texte und andere Dinge seiner eigenen Kinder nicht. Aber wenn Sie sie nicht verwenden möchten, überprüfen Sie diesen Code. Ich hoffe, es ist sauber zu verstehen, wie es funktioniert: 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()