Kivy:将原始窗口小部件添加到GridLayout(与Image,Button等相对) [英] Kivy: Add raw Widget to GridLayout (as opposed to Image, Button, etc)

查看:87
本文介绍了Kivy:将原始窗口小部件添加到GridLayout(与Image,Button等相对)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仍在学习Kivy的来龙去脉,但我遇到了一个有趣的问题.假设我有一个GridLayout,如下所示:

Still learning the ins and outs of Kivy, but I'm running into an interesting problem. Let's say I have a GridLayout, like so:

class MainApp(App):

    def build(self):
        self.root = GridLayout(cols=2)

        for i in range(4):
            self.root.add_widget(Button(text='Button {}'.format(i)))

        return self.root

我得到了(如预期的那样):

I get this (as expected):

但是,当我尝试使每个象限成为动态容器而不是普通的ButtonLabelImage等容器时,小部件(如下所示):

However, when I try to make each of those quadrants dynamic containers instead of plain old Button, Label, Image, etc, widgets (like so):

class Container(Widget):

    def __init__(self, *args, **kwargs):
        _id = kwargs.pop('button_id')
        super(Container, self).__init__(*args, **kwargs)

        self.add_widget(Button(text="Button {}".format(_id)))


class MainApp(App):

    def build(self):
        self.root = GridLayout(cols=2)

        for i in range(4):
            self.root.add_widget(Container(button_id=i))

        return self.root

我明白了:

请注意,无论窗口大小如何,每个小部件都位于左下角并保持较小的尺寸.

Note that regardless of the size of the window, each widget sits in the bottom left corner and maintains a small size.

添加可完成此工作的嵌入式Kivy小部件类型有什么用,但不能将Widget对象用作其他Widget对象的存储桶?

What is it about adding the baked-in Kivy widget types that makes this work, but using Widget objects as buckets of other Widget objects doesn't work?

推荐答案

尝试从kivy.uix.layout.Layout或其子类之一(例如kivy.uix.layout.BoxLayout)继承您的Container类.

Try inheriting your Container class from kivy.uix.layout.Layout or one of its subclasses (e.g. kivy.uix.layout.BoxLayout).

来自窗口小部件文档:

小部件的默认大小为(100,100).仅当父级是 http:时,此设置才会更改: //kivy.org/docs/api-kivy.uix.layout.html#kivy.uix.layout.Layout .例如,如果您在按钮内添加标签,则标签将不会继承按钮的大小或位置,因为该按钮不是布局:它只是另一个小部件.

The default size of a widget is (100, 100). This is only changed if the parent is a http://kivy.org/docs/api-kivy.uix.layout.html#kivy.uix.layout.Layout. For example, if you add a Label inside a Button, the label will not inherit the button’s size or position because the button is not a Layout: it’s just another Widget.

这篇关于Kivy:将原始窗口小部件添加到GridLayout(与Image,Button等相对)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆