Python Kivy boxlayout意外行为

huangapple go评论56阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年7月11日 04:06:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定