简体   繁体   中英

Why kivy is not updating the position of widgets when pos_hint is changed Programmatically?

I want to build a screen where on following button press changes happens on label's size and position:

Button : Changes
x++   -> x co-ordinate of label increments by 0.1 in pos_hint property
x--   -> x co-ordinate of label decrements by 0.1 in pos_hint property

So far I have tried this,

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.scatter import Scatter
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty



class Screen(Widget):
    lbl = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(Screen, self).__init__(**kwargs)
        self.count_x = 0.1

    def print_pos_change(self,instance,value):
        print(instance,"Moved to", value)

    def callback(self,arg):
        if arg == "x++":
            self.count_x+=0.1
            self.lbl.pos_hint["x"] = self.count_x
        elif arg == "x--":
            self.count_x-=0.1
            self.lbl.pos_hint["x"] = self.count_x

class WidgetEx(App):
    kv_directory = "kv-files"

    def build(self):
        return Screen()


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

Here is the kv file,

<Screen>
    lbl:lbl
    FloatLayout:
        size:root.width,root.height
        Label:
            id:lbl
            text:"Hello"
            size_hint:0.5,0.5
            on_pos:root.print_pos_change(self,self.pos)
            canvas:
                Color:
                    rgba: 0, 0, 1, 1
                Rectangle:
                    pos:self.pos
                    size:self.size
        GridLayout:
            cols:2
            size_hint:1,0.1
            Button:
                text:"x++"
                on_press:root.callback("x++")
            Button:
                text:"x--"
                on_press:root.callback("x--")

Now problem here is neither position is not changing nor print_pos_change is called during change.

I know i can directly use self.lbl.x and self.lbl.y but i want to change it using self.lbl.pos_hint . How do i do that???

Here is ss of the UI,用户界面图片

I have used do_layout() for floatlayout at end of method callback but buttons are also now moving with labels??How do i fix this?

Why size_hint is working properly and pos_hint don't??Is there any logic behind it?

I want to increment pos_hint["x"] property to increment by 0.1 at every x++ button press.

There are many ways to do this.
Try use numeric property on the pos hint right and top.

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

KV = """

MyWidget:
    FloatLayout:
        size:root.width,root.height
        Label:
            text:"Hello"
            size_hint:0.5,0.5
            pos_hint: {"top":root.pos_hint_top, "right":root.pos_hint_right}
            canvas.before:
                Color:
                    rgba: 0, 0, 1, 1
                Rectangle:
                    pos:self.pos
                    size:self.size
        GridLayout:
            cols:2
            size_hint:1,0.1
            Button:
                text:"x++"
                on_press:root.callback("x++")
            Button:
                text:"x--"
                on_press:root.callback("x--")

"""

class MyWidget(Widget):
    pos_hint_right = NumericProperty(0.5)
    pos_hint_top = NumericProperty(0.5)

    def callback(self,arg):
        if arg == "x++":
            self.pos_hint_right += 0.1
        elif arg == "x--":
            self.pos_hint_right -= 0.1

class WidgetEx(App):
    def build(self):
        return Builder.load_string(KV)


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM