2

I am creating buttons which on_press does the movement of another button. This is view of it...

Screenshot of UI

I can't post image but i have given link

I am changing pos_hint property on button events but the widget is not moving according to it. Actually it is not moving even a pixel :) Ya i know i can change widget.x and widget.y properties to move widget, but i want to know if its possible to change it using pos_hint

EDIT: I am using python 3.x and on debain OS

I have tried assigning button.pos_hint to a dictionary and then changing it and again assigning it to button.pos_hint Here's what i am telling

new_dict = some_button.pos_hint
#changing new_dict here...
some_button.pos_hint = new_dict

main.py-->

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.properties import ObjectProperty
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import DictProperty

class Login2(FloatLayout):

    btn_demo = ObjectProperty(None)

    def just(self, arg):
        if arg == "x++":
            print(self.btn_demo.pos_hint)
            if "x" in self.btn_demo.pos_hint.keys():
                self.btn_demo.pos_hint["x"] += 0.1
            else:
                self.btn_demo.pos_hint["x"] = 0
        elif arg == "top--":
            print(self.btn_demo.pos_hint)
            if "top" in self.btn_demo.pos_hint.keys():
                self.btn_demo.pos_hint["top"] -= 0.1
            else:
                self.btn_demo.pos_hint["top"] = 0

class MainApp(App):

    def build(self):
        return Login2()


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

kv file-->

<Login2>
    btn_demo:btn_demo
    GridLayout:
        cols:1
        size: root.width,root.height

        FloatLayout:
            Button:
                pos_hint:{"top":1,"x":0.3}
                size_hint:0.2,0.2
                id:btn_demo
                text:"Demo"


        GridLayout:
            size_hint: 1,0.2
            cols:6

            Button:
                text:"x++"
                on_press:root.just("x++")
            Button:
                text:"x--"
                on_press:root.just("x--")
            Button:
                text:"y++"
                on_press:root.just("y++")
            Button:
                text:"y--"
                on_press:root.just("y--")
            Button:
                text:"top++"
                on_press:root.just("top++")
            Button:
                text:"top--"
                on_press:root.just("top--")
            Button:
                text:"bot++"
                on_press:root.just("bot++")
            Button:
                text:"bot--"
                on_press:root.just("bot--")
            Button:
                text:"rgt++"
                on_press:root.just("rgt++")
            Button:
                text:"rgt--"
                on_press:root.just("rgt--")
            Button:
                text:"lef++"
                on_press:root.just("lef++")
            Button:
                text:"lef--"
                on_press:root.just("lef--")

How do i use pos_hint to change position of button widget???

SOLUTION ::::> call layout_instance.do_layout() for change to appear on screen... Just like this:

#change co-ordinates you want on widget_instance
#then call widget_instance's parent layout's do_layout() method
Yash Patel
  • 125
  • 8

1 Answers1

0

You should use "right" instead of "x" in pos_hint

This example works:

from kivy.app import App
from kivy.lang import Builder

KV = """

FloatLayout:
    Button:
        text: "hello"
        id: hello
        size_hint: 0.1, 0.1
        pos_hint: {"top":0.3, "right": 0.5}
        on_release:
            world.pos_hint = {"top":0.1, "right": 0.1}
    Button:
        id: world
        text: "world"
        size_hint: 0.1, 0.1
        pos_hint: {"top":0.5, "right": 0.9}
        on_release:
            world.pos_hint = {"top":0.9, "right": 0.9}

"""

class MyApp(App):

    def build(self):
        return Builder.load_string(KV)


MyApp().run()
el3ien
  • 5,362
  • 1
  • 17
  • 33
  • No man its not moving when we press a button....I want to change the position of button dynamically using ```pos_hint``` I have modified method ```just``` to increment ```right``` by 0.1 on each press but still doesn't work – Yash Patel Oct 29 '19 at 13:57
  • @YashPatel runs this example. If it doesnt move, there is Something wrong in your installation – el3ien Oct 29 '19 at 15:54
  • some strange thing happened...my code actually works.. when i press x++ button it actually change its x coordiante by 10% of parent's size but changes are seen when we maximize the screen or we resize the screen....Is this a kind of bug or i have to refresh a screen???? – Yash Patel Oct 30 '19 at 06:42
  • 1
    Bro i solved it by calling floatlayout.do_layout() at end of the method....Thanks for your time and help bruh – Yash Patel Oct 30 '19 at 06:46
  • @YashPatel ok yea maybe because you do it in python. Kv is a bit easier that way. Good luck – el3ien Oct 30 '19 at 10:00