【问题标题】:Why changing pos_hint property not changing the position of widget at runtime?为什么更改 pos_hint 属性不会在运行时更改小部件的位置?
【发布时间】:2019-10-29 11:46:11
【问题描述】:

我正在创建 on_press 移动另一个按钮的按钮。 这是它的观点...

我不能发布图片,但我已经给出了链接

我正在更改按钮事件上的pos_hint 属性,但小部件没有根据它移动。实际上它甚至没有移动一个像素:) 是的,我知道我可以更改 widget.xwidget.y 属性以移动小部件,但我想知道是否可以使用 pos_hint 更改它

编辑:我正在使用 python 3.x 和 debain 操作系统

我尝试将 button.pos_hint 分配给字典,然后更改它并再次将其分配给 button.pos_hint 这就是我要说的

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文件-->

<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--")

我如何使用pos_hint 来改变按钮小部件的位置???

解决方案::::> 调用 layout_instance.do_layout() 以使更改出现在屏幕上... 就像这样:

#change co-ordinates you want on widget_instance
#then call widget_instance's parent layout's do_layout() method

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    您应该在pos_hint 中使用"right" 而不是"x"

    这个例子有效:

    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()
    

    【讨论】:

    • 当我们按下按钮时,没有人它不动......我想使用pos_hint动态更改按钮的位置我已经修改了方法just以将right增加0.1 on每按一次,但还是不行
    • @YashPatel 运行此示例。如果它不动,则说明您的安装有问题
    • 发生了一些奇怪的事情......我的代码实际上可以工作......当我按下 x++ 按钮时,它实际上将其 x 坐标更改了父级大小的 10%,但是当我们最大化屏幕或调整大小时会看到变化屏幕....这是一种错误还是我必须刷新屏幕????
    • 兄弟,我通过在方法结束时调用 floatlayout.do_layout() 解决了它....感谢您的时间和帮助 bruh
    • @YashPatel 好吧,可能是因为你是在 python 中做的。 Kv 这样更容易一些。祝你好运
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多