简体   繁体   English

删除 Kivy 应用程序 python 中小部件之间的空间

[英]Remove space between widgets in Kivy app python

I've made this little Kivy python app:我做了这个小 Kivy python 应用程序:

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.core.window import Window

class TestApp(App):
    def build(self):   
        self.local = "Test message"        
        # build window
        self.window = GridLayout()
        self.window.cols = 1
        self.window.size_hint = (0.7, 0.9)
        self.window.pos_hint = {"center_x": 0.5, "center_y":0.5}
        # icon
        self.window.add_widget(Image(source="captus.png"))        
        # label
        self.charlbl = Label(text=self.local, color=(0, 0, 0, 1))
        self.window.add_widget(self.charlbl)        
        # background color
        Window.clearcolor = (1, 1, 1, 1)
        return self.window

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

I'm trying to create a basic app that will have an image, a text and a slider button.我正在尝试创建一个基本的应用程序,它将有一个图像、一个文本和一个 slider 按钮。

The problem I'm facing here is that the widgets have a big separation:我在这里面临的问题是小部件有很大的分离:

在此处输入图像描述

How can I remove that blank space between both Widgets?如何删除两个小部件之间的空白区域?

EDIT:编辑:

I've made some changes while getting an answer I was testing:在得到我正在测试的答案时,我做了一些更改:

.py file: .py 文件:

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

class PhoneApp(App):
    def build(self):
        return MyWidget()

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

.kv file: .kv 文件:

#:kivy 1.0.9
<MyWidget>:
    GridLayout:
        cols: 1

        Image:
            source: "captus.png"
        
        Label:
            text: "Test msg"
            color: 0, 0, 0, 1
            size_hint_y: None
            height: self.texture_size[1]

The "space" is actually the size of the Label . “空间”实际上是Label的大小。 A GridLayout divides its space equally among it's children, if there are no other constraints, So, in your case, the Label gets the same space as the Image .如果没有其他限制, GridLayout会将其空间平均分配给它的子级,因此,在您的情况下, Label获得与Image相同的空间。 If any of the children have explicitly set sizes, then those children get their set sizes, and the rest of the children share the remaining space.如果任何孩子明确设置了大小,那么这些孩子将获得他们设置的大小,并且 rest 个孩子共享剩余空间。 So, if you set the size of the Label to something smaller, then that "space" will appear smaller.因此,如果您将Label的大小设置为更小的值,那么该“空间”将显得更小。 The easiest way to do that is by using the kivy language.最简单的方法是使用 kivy 语言。 Here is a modified version of your code that does that:这是执行此操作的代码的修改版本:

from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window

kv = '''
GridLayout:
    cols: 1
    size_hint: (0.7, 0.9)
    pos_hint: {"center_x": 0.5, "center_y": 0.5}
    
    Image:
        source: 'captus.png'
    
    Label:
        text: app.local
        color: 0, 0, 0, 1
        size_hint_y: None
        height: self.texture_size[1]
'''

class TestApp(App):
    def build(self):
        self.local = "Test message"
        # background color
        Window.clearcolor = (1, 1, 1, 1)
        return Builder.load_string(kv)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM