python - 为什么 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()
推荐阅读
- php - 样式文件不包含在 php laravel 中
- power-automate - 在 Power Automate 中,如何转换长格式日期时间字符串?
- excel - 从 Excel 更新 MS Word 页脚表中的页码
- jsp - 如何替换jstl中的字符?
- python - 没有返回任何内容时的 Numpy Docstrings
- python - 在 API 响应中附加服务器名称——Django rest 框架
- wordpress - 来自后端的 wp_handle_upload()
- r - 使用 sink() 错误地打印到 .txt
- r - R包中的非标准遥控器包INLA
- amazon-web-services - 在 EC2 中运行并且希望只有一个用户可以访问的服务器