使用 pos_hint 定位小部件(例如 Scatter)后,如何获取当前的 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()