Python 当pos_提示以编程方式更改时,为什么kivy不更新小部件的位置?
我想构建一个屏幕,在该屏幕上,按下以下按钮时,标签的大小和位置会发生变化:Python 当pos_提示以编程方式更改时,为什么kivy不更新小部件的位置?,python,widget,kivy,kivy-language,Python,Widget,Kivy,Kivy Language,我想构建一个屏幕,在该屏幕上,按下以下按钮时,标签的大小和位置会发生变化: 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 到目前为止我已经试过了 from kivy.app import App from kivy
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
到目前为止我已经试过了
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()
这是kv文件
<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--")
lbl:lbl
浮动布局:
尺寸:根。宽度,根。高度
标签:
id:lbl
文字:“你好”
尺寸提示:0.5,0.5
on_pos:root.print_pos_change(self,self.pos)
画布:
颜色:
rgba:0,0,1,1
矩形:
pos:self.pos
大小:self.size
网格布局:
科尔斯:2
尺寸提示:1,0.1
按钮:
文本:“x++”
按以下键:root.callback(“x++”)
按钮:
正文:“x--”
按以下键:root.callback(“x--”)
现在这里的问题是,在更改期间,位置既没有更改,也没有调用print\u pos\u change
我知道我可以直接使用self.lbl.x
和self.lbl.y
,但我想使用self.lbl.pos\u hint
更改它。我该怎么做
这是UI的ss,
我在方法callback
的末尾为floatlayout
使用了do\u layout()
,但是按钮现在也随着标签一起移动??如何解决这个问题
为什么size_hint正常工作而pos_hint不正常?背后有什么逻辑吗
我想在每次按下
x++
按钮时,将pos\u hint[“x”]
属性的增量增加0.1。有很多方法可以做到这一点。尝试在pos提示的右侧和顶部使用数字属性
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()
有很多方法可以做到这一点。
尝试在pos提示的右侧和顶部使用数字属性
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()
你能想出一个最小的可运行的例子吗。只有一个按钮和一个标签,只在一个方向上移动。只有主要问题是必要的。不是整个程序,因为它不相关,只是需要查看更多的代码和图像,不包含您的主要问题。这只是一个快速帮助的建议。@el3ien我已将其缩减为两个按钮,分别与
pos\u hint
有关。您能为这一点想出最小的可运行示例吗。只有一个按钮和一个标签,只在一个方向上移动。只有主要问题是必要的。不是整个程序,因为它不相关,只是需要查看更多的代码和图像,不包含您的主要问题。只是一个快速帮助的建议。@el3ien我已将其缩减为两个按钮,用于pos\u hint
感谢您清除此内容。它起作用了。我可能会问你是从哪里学的??有好的教程或手册吗??我搜索了youtube,但没有找到关于kivy的全部教程。你很好。我想我主要使用的是api参考。但也有一些很好的资源。就像youtube上的一个很好的系列节目一样,感谢你澄清了这件事。它起作用了。我可能会问你是从哪里学的??有好的教程或手册吗??我搜索了youtube,但没有找到关于kivy的全部教程。你很好。我想我主要使用的是api参考。但也有一些很好的资源。就像youtube上的一个好的系列节目一样