首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >应用pos_hint后获取小部件XY位置

应用pos_hint后获取小部件XY位置
EN

Stack Overflow用户
提问于 2013-08-04 11:34:27
回答 1查看 2.2K关注 0票数 2

使用pos_hint定位小部件(例如分散)之后,如何获得当前的x,y位置(pos)?

例如:

代码语言:javascript
运行
复制
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.

编辑:示例代码

代码语言:javascript
运行
复制
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()
EN

回答 1

Stack Overflow用户

发布于 2013-08-04 17:54:54

问题是,窗口(以及布局本身)在分配布局(如size_hintpos_hint)所遵循的值后,不会立即刷新。它们是在窗口刷新后更新的(直到方法结束)

您基本上可以显式地调用方法do_layout。文档中说,“当需要布局时,通过触发器调用此方法”。我不得不说,我不确定是否明确地调用它会导致一些问题,因为这种使用没有文档化。它对我有用,但要小心:

代码语言:javascript
运行
复制
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)已经移动之后,这是不必要的。

编辑:问题中代码的更正版本

代码语言:javascript
运行
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18042448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档