Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我如何获得widget';s/layout';Kivy的实际尺寸是多少?_Layout_Python 3.x_Widget_Kivy_Python 3.4 - Fatal编程技术网

我如何获得widget';s/layout';Kivy的实际尺寸是多少?

我如何获得widget';s/layout';Kivy的实际尺寸是多少?,layout,python-3.x,widget,kivy,python-3.4,Layout,Python 3.x,Widget,Kivy,Python 3.4,我在BoxLayout中有此布局(以及其他元素): 我如何得到它的实际尺寸打印(self.size)显示默认大小(100100),尽管它不是真的。小部件的大小取决于您检查的时间。创建小部件时不计算大小,而是在布局中放置小部件时计算大小。例如: from kivy.app import App from kivy.lang import Builder from kivy.uix.layout import Layout kv = ''' <Foo>: on_touch_do

我在BoxLayout中有此布局(以及其他元素):


我如何得到它的实际尺寸<代码>打印(self.size)显示默认大小(100100),尽管它不是真的。

小部件的大小取决于您检查的时间。创建小部件时不计算大小,而是在布局中放置小部件时计算大小。例如:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.layout import Layout

kv = '''
<Foo>:
    on_touch_down: print(self.size)
'''
Builder.load_string(kv)

class Foo(Layout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        print (self.size)

class MyApp(App):
    def build(self):
        return Foo()

MyApp().run()
首先,使用默认大小值创建
Foo
。然后它被显示出来,所以正确的大小值被计算并分配给size属性,这将触发
update
方法。任何其他大小更新(例如,如果重新缩放窗口)也将触发此方法。自动执行此操作,这是推荐的方法


最后,我不知道您想要实现什么,但我怀疑您现在并不真正需要这个值。

如果我需要构造函数中的实际大小,该怎么办?例如,内部带有self.canvas:?
class PlayField(Layout):  # Or should it be Widget?
    pass
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.layout import Layout

kv = '''
<Foo>:
    on_touch_down: print(self.size)
'''
Builder.load_string(kv)

class Foo(Layout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        print (self.size)

class MyApp(App):
    def build(self):
        return Foo()

MyApp().run()
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.layout import Layout

class Foo(Layout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.bind(pos=self.update)
        self.bind(size=self.update)
        self.update()

    def update(self, *args):
        print(self.size)
        self.canvas.clear() 
        with self.canvas:
            pass

class MyApp(App):
    def build(self):
        return Foo()

MyApp().run()