问题描述
我在my.kv
文件中有以下代码:
I have the following code in my.kv
file:
<UIWidget>:
AnchorLayout:
anchor_x: 'right'
anchor_y: 'top'
Button:
size_hint: None, None
text: 'Build'
UI Widget是一个空的Widget子类.
UI Widget is an empty Widget subclass.
class UIWidget(Widget):
pass
我的根窗口小部件将UIWidget加载到自身中,如下所示:
My root widget loads UIWidget into itself as follows:
class SimplifiedGameWidget(Widget):
def __init__(self, **kwargs):
super().__init__(self, **kwargs)
self.layout = RelativeLayout(size_hint=(None, None))
self.layout.size = (1024, 768)
ui_widget = UIWidget()
ui_widget.size_hint = (1, 1)
self.layout.add_widget(ui_widget)
self.add_widget(self.layout)
class MyApp(App):
def build(self):
return SimplifiedGameWidget()
(完整代码: http://pastie.org/10791067 )
我希望得到的东西:
- GameWidget> RelativeLayout> UIWidget,其大小均按窗口大小
- 屏幕右上方的UIWidget子级的AnchorLayout
- AnchorLayout中的按钮,其大小为右上角的默认大小.
我实际上得到的是:
- GameWidget> RelativeLayout> UIWidget,其大小均按窗口大小
- 位于屏幕左下方的UIWidget子级的AnchorLayout,尺寸为100x100px
- AnchorLayout中按钮的默认大小,左下角为100x100px
我在做什么错了?
推荐答案
Widget
不会影响其子代的大小或位置,无论是否带有size_hint
/pos_hint
-这是Layout
s.由于Widget
的默认位置为(0, 0)
,因此AnchorLayout
结束于左下角,默认大小为(100, 100)
.我可能会改为将SimplifiedGameWidget
扩展为FloatLayout
,并使UIWidget
直接扩展AnchorLayout
而不是包含一个:
Widget
s don't affect the size or position of their children, with or without size_hint
/pos_hint
- this is the purpose of Layout
s. Since the default position of a Widget
is (0, 0)
, the AnchorLayout
ends up in the lower-left corner, with the default size (100, 100)
. I would maybe switch the SimplifiedGameWidget
to extend FloatLayout
instead, and make UIWidget
extend AnchorLayout
directly instead of containing one:
<UIWidget>:
anchor_x: 'right'
...
<SimplifiedGameWidget>:
UIWidget
和:
class UIWidget(AnchorLayout):
pass
class SimplifiedGameWidget(FloatLayout):
pass
这篇关于为什么我的Kivy AnchorLayout卡在左下角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!