本文介绍了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).

来自窗口小部件文档:

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

10-24 05:03