为什么根窗口小部件与窗口的大小不同?

2024-02-10

我正在尝试使用自定义小部件GridLayout,但结果总是在角落里出现一个很小的网格,而不是在整个窗口中扩展的网格。

示例代码:

import kivy
kivy.require('1.5.1')

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout



class MyWidget(Widget):
    def __init__(self):
        super(MyWidget, self).__init__()

        grid_layout = GridLayout(cols=3)
        grid_layout.add_widget(Button(text='A'))
        grid_layout.add_widget(Button(text='B'))
        grid_layout.add_widget(Label(text='text'))
        grid_layout.add_widget(Label(text='other'))
        grid_layout.add_widget(Button(text='text'))
        self.add_widget(grid_layout)


class MyApp(App):
    def build(self):
        float = 
        return MyWidget()


MyApp().run()

Since Widget的默认值size_hint is (1,1)它应该在整个窗口中展开,并且GridLayout还。为什么这没有发生? 我怎样才能得到我想要的结果?


`class MyWidget(Widget):`

您的根小部件是MyWidget它继承自一个Widget而不是来自其中一种布局,因此无法控制其子级的大小如上所述here http://kivy.org/docs/api-kivy.uix.layout.html#understanding-size-hint-property-in-widget,“size_hint 是一个值的元组,由layouts管理孩子的体型”。

您的根小部件确实占据了窗口的整个空间。您可以通过向画布添加一个矩形来测试这一点MyWidget像这样::

with self.canvas.before:
    Color(1,0,0,1)
    Rectangle(pos=self.pos, size=self.size)

你应该熟悉画布,canvas.before 和 canvas.after http://kivy.org/docs/api-kivy.graphics.instructions.html#kivy.graphics.instructions.Canvas。它们基本上是指令组,前组在小部件的画布指令之前绘制,后组在之后绘制。

Kivy 中一个关键的不同之处是小部件的大小调整/布局会延迟到下一帧,因此如果您只是将上面的代码片段添加到代码中,如下所示:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.graphics import *


class MyWidget(Widget):

    def __init__(self, **kwargs):
        # I'd make sure to pass of kwargs through to the super
        # as there are widgets's that process their initial
        # arguments through.
        super(MyWidget, self).__init__(**kwargs)

        grid_layout = GridLayout(cols=3)
        grid_layout.add_widget(Button(text='A'))
        grid_layout.add_widget(Button(text='B'))
        grid_layout.add_widget(Label(text='text'))
        grid_layout.add_widget(Label(text='other'))
        grid_layout.add_widget(Button(text='text'))
        self.add_widget(grid_layout)
        with self.canvas.before:
            Color(1,0,0,1)
            Rectangle(pos=self.pos, size=self.size)


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

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

这只会在小部件的初始位置和大小处显示一个红色矩形,当时它的默认位置和大小分别为 (0, 0) 和 (100, 100)。

为了使红色矩形符合小部件的大小,我们应该bind http://kivy.org/docs/api-kivy.event.html#kivy.event.EventDispatcher.bind它的大小与小部件的大小相同,如下所示::

...
        grid_layout.add_widget(Button(text='text'))
        self.add_widget(grid_layout)
        with self.canvas.before:
            Color(1,0,0,1)
            self.rect = Rectangle(pos=self.pos, size=self.size)
        self.bind(size=self.update_rect)

    def update_rect(self, instance, value):
        self.rect.pos = self.pos
        self.rect.size = self.size

class MyApp(App):
    def build(self):
...

正如上面代码的输出所示,您的小部件占据了整个窗口的大小。但是,这并不能解决您的问题,子布局仍然保持其原始位置和原始大小。这是因为小部件无法控制其子级的大小,如上所述。

您在这里有两种选择,要么更新小部件的子部件的大小和位置,就像更新矩形一样(多个子部件很快就会变得复杂),要么使用其中一个布局作为您的根小部件。

这也可以在 kv 中完成,如下所示::

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder

Builder.load_string('''
# this is the rule for MyWidget that defines
# what MyWidget looks like i.e.drawing
# instructions and widgets etc 
<MyWidget>:
    canvas.before:
        Color:
            rgba: 1, 0, 0, 1
        Rectangle:
            # this implicitly binds the size of the
            # rect to the size of the widget
            size: self.size
            # self here still refers to the widget as Rectangle is only a
            # graphics instruction and not a widget
            pos: self.pos
    GridLayout:
        cols: 3
        # root here refers to the `MyWidget`, bind the size of the
        # GridLayout to the size of your root widget
        size: root.size
        Button:
            text: 'A'
        Button:
            text: 'B'
        Label:
            text: 'text'
        Label:
            text: 'other'
        Button:
            text: 'text'        
''')


class MyWidget(Widget):
    pass


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

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

上面的示例将子窗口小部件的大小绑定到其父窗口小部件的大小。我仍然建议使用布局作为根小部件,并且不要对嵌套布局犹豫不决。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么根窗口小部件与窗口的大小不同? 的相关文章

随机推荐