【问题标题】:How to correctly scale MDIconButton?如何正确缩放 MDIconButton?
【发布时间】:2019-04-09 03:42:49
【问题描述】:

我想根据我的画笔大小缩放MDIconButtonPushMatrix/PullMatrix 似乎适用于画布中的所有后续小部件,而不仅仅是预期的按钮小部件。

我发现库存标准 kivy Button 小部件不会出现这种情况,但希望继续使用 kivymd MDIconButton 进行动画和其他行为/敷料。

目前我在顶部使用一个标签来为我的按钮添加额外的细节,这使得这个实现在我看来是最直接的。 我想问题是由MDIconButton 继承自其中的一种行为引起的,但还不能准确地隔离出哪一种。

from kivy.app import App
from kivy.lang import Builder

from kivymd.button import MDRaisedButton, MDIconButton

class TestApp(App):
    def build(self):
        return Builder.load_string('''
BoxLayout:
    orientation: 'vertical'

    BoxLayout:
        size_hint_y: None
        height: dp(42)
        orientation: 'horizontal'

        ##Button:
        MDIconButton:
            _scale: 1
            on_release: self._scale = (((self._scale*3) + 1) % 3) / 3
            ##text: 'brush'
            icon: 'brush'
            theme_text_color: 'Custom'
            text_color: 1,1,1,1
            canvas.before:
                PushMatrix
                Scale:
                    origin: self.center
                    x: self._scale or 1.
                    y: self._scale or 1.
            canvas.after:
                PopMatrix

    Widget:
        id: palette
        size_hint_y: None
        height: dp(42)
        canvas.before:
            Color:
                rgb: 1,0,0
            Rectangle:
                size: self.size
                pos: self.pos

    Widget:
        id: sketchpad
        canvas.before:
            Color:
                rgb: 1,1,0
            Rectangle:
                size: self.size
                pos: self.pos
''')


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

MDIconButton icon: 'brush' 应在 3 种尺寸之间循环,其余小部件保持正常尺寸(将 MDIconButton 替换为 Button 并将 icon: 更改为 text: 时出现正确行为)。

有没有更好/不同的方法来改变图标大小,完全避免这个问题?

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    您的问题询问如何正确缩放MDIconButton。我不能声称这是correct,但这里有一个 hack 可以完成它。注意icon只是字体中的一个字符,所以可以通过调整字体大小来调整它的大小。为此,我扩展了BoxLayout(只是因为它是您的root)以包含set_font_size() 方法:

    class MyBoxLayout(BoxLayout):
        def set_font_size(self, *args):
            butt = self.ids.mdIconButt
            label = butt.ids.content
    
            # adjust font size for the icon
            label.font_size *= 1.1
    
            # adjust the size of the buttons containers
            butt.height *= 1.1
            butt.width *= 1.1
            butt.parent.height *= 1.1
    

    然后,在您的kv 字符串中:

    MyBoxLayout:
        orientation: 'vertical'
    
        BoxLayout:
            size_hint_y: None
            height: dp(42)
            orientation: 'horizontal'
    
            ##Button:
            MDIconButton:
                id: mdIconButt  # id to make it easy to find this widget
                on_release: root.set_font_size()  # call the new method
                icon: 'brush'
                theme_text_color: 'Custom'
                text_color: 1,1,1,1
        .
        .
        .
    

    请注意,这是在摆弄 MDIconButton 内部结构,因此预计它会因对 kivyMD 的任何更改而中断。

    您实际上可以通过简单地将on_release 替换为MDIconButton 来完成大部分操作:

    on_release: self.ids.content.font_size *= 1.1
    

    然后只有图标大小发生变化,MDIconButton 及其容器没有变化。

    【讨论】:

      猜你喜欢
      • 2020-01-21
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 2019-08-19
      • 2014-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多