Kivy StackLayout不能堆叠FloatLayouts。

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

Kivy StackLayout not stacking floatLayouts

问题

我正在尝试堆叠多个FloatLayout实例,每个实例内部都有一个按钮。我使用FloatLayout,因为我想稍后调整按钮在FloatLayout中的水平相对位置,同时保持垂直位置不变。当我使用pos_hint来定位按钮在FloatLayout中时,一切都正常,但我想使用pos = (x, y),这会导致所有按钮重叠在一起,所以我只能看到一个按钮... 如果有人能告诉我我哪里出错了,我将不胜感激!

我认为按钮正在采用窗口的位置而不是父部件的位置,是这样吗?

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button

class BreaksPanel(FloatLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.size_hint = None, None
        self.width = 5000 
        self.height = 36 

        btn = Button(text='My Button', size_hint=(None, None), size=(75, 36), pos=(0, 0))  # pos_hint={"x": 0, "y": 0}) works fine but not pos = (0,0)
        self.add_widget(btn)

class BreaksWindow(StackLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.orientation = "tb-lr" 
        for i in range(10):
            B = BreaksPanel()
            self.add_widget(B)

class Example(App):
    def build(self):
        layout = BreaksWindow()
        return layout

if __name__ == '__main__':
    Example().run()

请注意,我已经更新了代码中的引号,以确保它在Python中是有效的。不要忘记在Kivy应用程序中使用正确的坐标系统来设置按钮的位置。

英文:

I am trying to stack several instances of a floatlayout, each with one button inside. I am using flaotlayout as I want to later adjust the relative position of the button horizontaly across the float layout while keeping the vertical position. When I use pos_hint to position the button in the float layout it works fine but I want to use pos = (x,y) and this results in all buttons overlappying so i can only see one... I would appreciate if anyone could tell me where I am going wrong!

i think the button is taking the pos of the window rather then the parent widget, is this the case??

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button



class BreaksPanel(FloatLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.size_hint = None, None
        self.width = 5000 
        self.height = 36 

        btn = Button(text='My Button', size_hint=(None, None), size=(75, 36), pos = (0,0) )  # pos_hint={"x":0, "y":0}) works fine but not pos = (0,0)
        self.add_widget(btn)



class BreaksWindow(StackLayout):

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        self.orientation = "tb-lr" 
        for i in range(10):
            B = BreaksPanel()
            self.add_widget(B)



class Example(App):
    def build(self):
        layout = BreaksWindow()
        return layout


if __name__ == '__main__':
    Example().run()

答案1

得分: 1

问题出在你对FloatLayout的使用上。在FloatLayout中,你可以将其子元素放置在任何位置,而不仅仅限制在FloatLayout的内部。因此,你的Buttons都被定位在相同的位置(0,0)。我认为你期望FloatLayout的行为类似于RelativeLayout,其中(0,0)的位置是布局的右下角。所以一个简单的解决方法就是将BreaksPanel改成继承自RelativeLayout

class BreaksPanel(RelativeLayout):
英文:

The problem is your use of FloatLayout. In a FloatLayout, you can position its children anywhere and that is not restricted to the inside of the FloatLayout. So your Buttons are all positioned at the same pos (0,0). I think you are expecting the FloatLayout to act like a RelativeLayout, where pos of (0,0) is the lower right of the Layout. So a simple fix is to just make the BreaksPanel extend RelativeLayout:

class BreaksPanel(RelativeLayout):

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

发表评论

匿名网友

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

确定