【问题标题】:Use of ToggleButton in Kivy - image resizing problem在 Kivy 中使用 ToggleButton - 图像大小调整问题
【发布时间】:2021-06-10 18:52:07
【问题描述】:

我正在尝试将一些 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, 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, 0, 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, 0, 1
            Rectangle:
                pos: self.pos
                size: self.size

        orientation: 'vertical'
        size_hint: None, 1
        padding: 2, 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, 0, 0, .5
        Rectangle:
            pos: self.pos
            size: self.size

    BoxLayout:
        canvas.before:
            Color:
                rgba: 0, 0, 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, 1, 1
            required_height: '90dp'
            label_text: 'At 90dp:'

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

        AZPlusMinusValue:
            background_color: 0, 1, 1, 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()


【问题讨论】:

  • 谢谢,我确实注意到了边框属性,但错误地认为它仅在指定了边框图像时才适用。 KV 语言非常强大,但也很脆弱……

标签: python kivy widget size togglebutton


【解决方案1】:

ToggleButton 有一个默认的border

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

描述为:

边框用于 :class:~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, 16, 16)

这限制了ToggleButton 的大小。您可以通过添加来消除border

        border: [0,0,0,0]

kv 中的每个ToggleButtun

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 2012-10-17
    • 2017-01-04
    • 1970-01-01
    相关资源
    最近更新 更多