英文:
Python Kivy boxlayout unexpected behavior
问题
我正在尝试创建一个用户界面(UI),其中FloatLayout占据屏幕顶部的80%,而BoxLayout占据底部的20%。但实际上,FloatLayout占据了底部的80%,而上面有一片空白区域。
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
class UI(Screen):
def __init__(self, **kwargs):
super(UI, self).__init__(**kwargs)
layout = BoxLayout(orientation='vertical')
float_layout = FloatLayout()
float_layout.size_hint_y = 0.8
float_layout.add_widget(Button(text='Float Layout'))
box_layout = BoxLayout()
box_layout.size_hint_y = 0.2
box_layout.add_widget(Button(text='Box Layout'))
layout.add_widget(float_layout)
layout.add_widget(box_layout)
self.add_widget(layout)
class VNG(App):
def build(self):
return UI()
if __name__ == "__main__":
VNG().run()
我尝试使用不同的定位选项,但始终无法获得所需的效果。如果更改添加布局的顺序,可以消除任何黑色空间,但这会颠倒我想要的效果。
英文:
I am trying to get a UI where the floatlayout takes up the top 80% of the screen, and a boxlayout takes out the bottom 20%. Intsead I get the floatlayout taking taking up the bottom 80%, with a empty space taking up the top 20%.
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
class UI(Screen):
def __init__(self, **kwargs):
super(UI, self).__init__(**kwargs)
layout = BoxLayout(orientation='vertical')
float_layout = FloatLayout()
float_layout.size_hint_y = 0.8
float_layout.add_widget(Button(text='Float Layout'))
box_layout = BoxLayout()
box_layout.size_hint_y = 0.2
box_layout.add_widget(Button(text='Box Layout'))
layout.add_widget(float_layout)
layout.add_widget(box_layout)
self.add_widget(layout)
class VNG(App):
def build(self):
return UI()
if __name__ == "__main__":
VNG().run()
I tried using different positioning options but never got what I wanted. Flipping the order in which I add the layouts removes any black space, but it is inverted of what I want.
答案1
得分: 0
当您使用FloatLayout
时,您必须定位其子元素。任何Widget
的默认位置是(0, 0)
,这会将您的Button
定位在BoxLayout
的后面。一个简单的解决方法是使用RelativeLayout
,它将其子元素相对于自身的位置进行定位,因此(0, 0)
位置将与RelativeLayout
重合。
英文:
When you use a FloatLayout
, you must position its children. The default position of any Widget
is (0, 0)
, which is positioning your Button
behind the BoxLayout
. An easy fix is to use a RelativeLayout
, which positions its children relative to it own position, so a (0, 0)
position will be coincident with the RelativeLayout
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论