使用pos_hint定位小部件(例如分散)之后,如何获得当前的x,y位置(pos)?
例如:
wid.pos = (250, 350)
print wid.pos  <----- # it print (200, 350). Correct.
wid.pos_hint = {'top':0.9, 'right':0.5}  # moved the widget to other position using pos_hint.
print wid.pos  <----- # it sill print (200, 350) eventhough the widget position has changed.编辑:示例代码
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.scatter import Scatter
Builder.load_string(""" 
<Icon@Scatter>: 
    size_hint: .06, .08
    Image:
        size: root.size
        allow_stretch: True
        keep_ratio: True
""")
class Icon(Scatter):
    def __init__(self, **kwargs):
        self.pos = (200, 200)
        self.move()
        super(Icon, self).__init__(**kwargs)
    def move(self):
        print "BEFORE: "
        print self.pos      # print 200, 200
        self.pos_hint = {'top':0.9, 'right':0.5} # assume now Scatter has moved to x800 y500.
        print "AFTER: "
        print self.pos      # it still print 200, 200 :(
class GameApp(App):
    def build(self):
        return Icon()
if __name__ == '__main__':
    GameApp().run()发布于 2013-08-04 17:54:54
问题是,窗口(以及布局本身)在分配布局(如size_hint或pos_hint)所遵循的值后,不会立即刷新。它们是在窗口刷新后更新的(直到方法结束)
您基本上可以显式地调用方法do_layout。文档中说,“当需要布局时,通过触发器调用此方法”。我不得不说,我不确定是否明确地调用它会导致一些问题,因为这种使用没有文档化。它对我有用,但要小心:
wid.pos = (250, 350)
print wid.pos  # it prints (200, 350). Correct.
wid.pos_hint = {'top':0.9, 'right':0.5} 
print wid.pos  # it sill print (200, 350) 
wid.do_layout()
print wid.pos  # it should work now当您让窗口刷新时,即在您实际看到Scatter (或任何其他Widget)已经移动之后,这是不必要的。
编辑:问题中代码的更正版本
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.scatter import Scatter
from kivy.uix.floatlayout import FloatLayout
Builder.load_string(""" 
<Icon>: 
    size_hint: .06, .08
    Image:
        size: root.size
        allow_stretch: True
        keep_ratio: True
<BaseLayout>:
    icon: _icon
    Icon:
        id: _icon
    Button: 
        text: "Press me"
        size_hint: .1,.05
        on_press: _icon.move()
""")
class Icon(Scatter):
    def __init__(self, **kwargs):
        self.pos = (200, 200)
        super(Icon, self).__init__(**kwargs)
    def move(self):
        print "BEFORE: "
        print self.pos      # print 200, 200
        self.pos_hint = {'top':0.9, 'right':0.5} # assume now Scatter has moved to x800 y500.
        self.parent.do_layout()
        print "AFTER: "
        print self.pos      # it still print 200, 200 :(
class BaseLayout(FloatLayout):
    pass
class GameApp(App):
    def build(self):
        return BaseLayout()
if __name__ == '__m
ain__':
    GameApp().run()https://stackoverflow.com/questions/18042448
复制相似问题