【问题标题】:position gridlayout inside gridlayout kivy在gridlayout kivy中定位gridlayout
【发布时间】:2020-04-15 19:52:03
【问题描述】:

我正在尝试在 kivy 中将一个 gridlayout(包含 4 个文本输入)定位在另一个 gridlayout(在我的情况下是 rootwidget- ResgistrationWindow)的中心,但似乎没有任何效果。

这是我的 .kv 文件,根小部件是网格布局

<RegistrationWindow>
cols:1
canvas.before:
    Rectangle:
        size: self.size
        pos:self.pos
        source:"emotion.jpg"

GridLayout:
    cols: 2
    size_hint: None,None
    size:root.width,root.height/14
    Label:
        text: "Button 1"
        size_hint_x:0.95
    Button:
        text: "X"
        size_hint_x: 0.05

GridLayout:
    cols:1
    size_hint: None, None
    size:root.width, root.height/2
    TextInput:
        multiline:False
    TextInput:
        multiline:False
    TextInput:
        multiline:False
    TextInput:
        multiline:False

这是我的 .py 文件

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.lang.builder import Builder


class RegistrationWindow(GridLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)


kv = Builder.load_file("emotions.kv")


class RegistrationApp(App):
    def build(self):
        return RegistrationWindow()


if __name__ == "__main__":
    RegistrationApp().run()

【问题讨论】:

    标签: python kivy grid-layout


    【解决方案1】:

    我认为您的问题是缩进问题。由于您想在 RegistrationWindow 中添加 GridLayouts,因此您的 .kv 文件应该看起来更像这样:

    (您可以根据需要进一步重新排列 GridLayout,或者进一步缩进 GridLayout 以将其放入另一个 GridLayout。)

    <RegistrationWindow>
        cols:1
        canvas.before:
            Rectangle:
                size: self.size
                pos:self.pos
                source:"emotion.jpg"
    
        GridLayout:
            cols: 2
            size_hint: None,None
            size:root.width,root.height/14
            Label:
                text: "Button 1"
                size_hint_x:0.95
            Button:
                text: "X"
                size_hint_x: 0.05
    
        GridLayout:
            cols:1
            size_hint: None, None
            size:root.width, root.height/2
            TextInput:
                multiline:False
            TextInput:
                multiline:False
            TextInput:
                multiline:False
            TextInput:
                multiline:False
    

    【讨论】:

    • 它实际上是缩进的。我在粘贴代码时犯了一个错误。你知道为什么它仍然会给我这个定位问题吗?谢谢
    • 您可以发布您的错误或指定您想要的内容吗?它似乎对我有用。
    • 我试图将最后一个网格布局放置在根小部件的中心。不过我现在明白了。非常感谢
    • @aisha lawal 我也有同样的问题。既然您能够解决您的问题,请您发布您的解决方案吗?
    猜你喜欢
    • 2022-01-05
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 2014-12-28
    相关资源
    最近更新 更多