在 Kivy 中使用 ToggleButton - 图像大小调整问题

问题描述

我正在尝试将一些 Kivy 小部件组合成一个组,然后能够使用具有不同设置的组小部件。这在某些小部件尺寸下似乎可以正常工作,但在较小尺寸下会失效。

我在 Windows 10 上使用 Kivy 2.0.0、Python 3.7.4 (Pycharm IDE)。

这是显示问题的屏幕截图:screenshot of app running

这是我的 .kv 文件

<AZPlusMinusValue@BoxLayout>
    background_color: 1,1,1
    required_height: '30dp'
    font_name: 'Times.ttf'
    font_size: '20sp'
    label_text: 'Offset:'
    label_width: 0
    toggle_width: 0
    input_width: 0

    canvas.before:
        Color:
            rgba: root.background_color
        Rectangle:
            pos: self.pos
            size: self.size

    orientation: 'horizontal'
    size_hint: None,None
    height: self.required_height

    on_toggle_width:
        self.width = self.toggle_width + self.label_width + self.input_width

    Label:
        text: root.label_text
        font_name: root.font_name
        font_size: root.font_size
        color: 0,1
        size_hint: None,1
        text_size: None,None
        size: self.texture_size
        halign: 'center'
        valign: 'center'
        padding: 2,2
        on_texture_size:
            root.label_width = self.width

    BoxLayout:
        canvas.before:
            Color:
                rgba: 0,1
            Rectangle:
                pos: self.pos
                size: self.size

        orientation: 'vertical'
        size_hint: None,1
        padding: 2,2,2
        spacing: 2

        ToggleButton:
            group: 'delta'
            allow_no_selection: False
            size_hint: None,.3
            on_size:
                self.width = self.height
                self.parent.width = self.width + self.parent.padding[1]*2
                root.toggle_width = self.parent.width

        ToggleButton:
            group: 'delta'
            size_hint: None,.3
            state: 'down'
            on_size:
                self.width = self.height
                self.parent.width = self.width + self.parent.padding[1]*2
                root.toggle_width = self.parent.width

<Interface@BoxLayout>:
    canvas.before:
        Color:
            rgba: 1,.5
        Rectangle:
            pos: self.pos
            size: self.size

    BoxLayout:
        canvas.before:
            Color:
                rgba: 0,1
            Rectangle:
                pos: self.pos
                size: self.size

        size_hint: 1,None
        pos_hint: {'x':0,'top':0.75}
        height: '30dp'

        AZPlusMinusValue:
            background_color: 1,1
            required_height: '90dp'
            label_text: 'At 90dp:'

        AZPlusMinusValue:
            background_color: 1,1
            required_height: '60dp'
            label_text: 'At 60dp:'

        AZPlusMinusValue:
            background_color: 0,1
            required_height: '30dp'
            label_text: 'At 30dp:'

Interface:

这是 .py 代码

#! python3

from kivy.app import App
from kivy.uix.Boxlayout import BoxLayout

class Interface(BoxLayout):
    pass

class seekerApp(App):

    def build(self):
        ui = Interface()
        return ui


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


解决方法

ToggleButton 有一个默认的 border

border = ListProperty([16,16,16])

被描述为:

边框用于 :类:~kivy.graphics.vertex_instructions.BorderImage 图形指令。与 background_normalbackground_down。可用于自定义背景。

It must be a list of four values: (bottom,right,top,left). Read the
BorderImage instruction for more information about how to use it.

`border` is a :class:`~kivy.properties.ListProperty` and defaults to
(16,16)

这限制了 ToggleButton 可以有多小。您可以通过添加以下内容来消除 border

        border: [0,0]

发送给您的 ToggleButtun 中的每个 kv