为什么kivy的小部件的大小不是真实的



有我的代码:

class MyGame(Widget):
def prepare_game(self):
print(self.height, self.width)
class MyApp(App):
def build(self):
game = MyGame()
game.prepare_game()
return game
MyApp().run()

输出是100 100,但事实并非如此。当我想调用方法prepare_game((一次时,我能找出小部件的实际大小吗?

这是因为在上面的示例中没有设置小部件的大小。在这种情况下,你会得到默认的大小,即100,100。更新后的大小总是可以通过on_size方法找到,如下例所示。

from kivy.app import App
from kivy.uix.widget import Widget
class MyGame(Widget):        
def prepare_game(self):
print(self.height, self.width)
def on_size(self, *args):
print(self.size)
class MyApp(App):
def build(self):
game = MyGame()
game.prepare_game()
return game
MyApp().run()

最新更新