MDRectangleFlatButton(s) 不采用它们在 GridLayout 或 MDGridLayout 中应该具有的大小。 Kivy 的按钮工作正常

Posted

技术标签:

【中文标题】MDRectangleFlatButton(s) 不采用它们在 GridLayout 或 MDGridLayout 中应该具有的大小。 Kivy 的按钮工作正常【英文标题】:The MDRectangleFlatButton(s) don't take the size they are supposed to have in either GridLayout or MDGridLayout. Kivy's Button(s) work fine 【发布时间】:2021-10-07 02:29:56 【问题描述】:

我确信代码和图片是不言自明的。 如果我将 'size_hint: 1, 1' 添加到其中的每一个,MDRectangleFlatButton(s) 将完美地占据整个 GridLayout。但随后它会变成一个不同的问题 '[CRITICAL] [Clock ] 警告,在下一帧之前完成了太多迭代。检查您的代码,或增加 Clock.max_iteration 属性' 应用程序变得非常滞后,所以我认为这不是正确的方法。我最近刚接触 Kivy,所以也许我还没有真正了解尺寸调整的工作原理或 KivyMD 在尺寸方面与 Kivy 有何不同。任何答案,文档将不胜感激。谢谢!

ma​​in.py

#! /usr/bin/python3

from kivy.uix.widget import Widget
from kivy.utils import get_color_from_hex

from kivymd.app import MDApp


class MainWidget(Widget):
    pass


class CalculatorApp(MDApp):
    pass


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

calculator.kv

MainWidget:

#: import hex kivy.utils.get_color_from_hex

<MainWidget>:

    MDBoxLayout: 
        id: main_box_layout 

        size: root.width, root.height 
        orientation: 'vertical'

#        TextInput:
#            id: calc_screen
#            
#            halign: 'left'
#            text: 'Screen'
        
        GridLayout: 
            canvas.after:
                Color:
                    rgba: hex('#3399FF4C')

                Rectangle:
                    pos: self.pos
                    size: self.size

            id: calc_buttons

            cols: 5
            rows: 4

            # 1
            Button:
                text: '%'
            # 2
            Button:
                text: '%'

            # 3
            Button:
                text: '%'

            # 4
            Button:
                text: '%'

            # 5
            Button:
                text: '%'

            # 6
            Button:
                text: '%'

            # 7
            Button:
                text: '%'

            # 8
            Button:
                text: '%'

            # 9
            Button:
                text: '%'

            # 10
            Button:
                text: '%'

            # 11
            Button:
                text: '%'

            # 12
            Button:
                text: '%'

            # 13
            Button:
                text: '%'

            # 14
            Button:
                text: '%'

            # 15
            Button:
                text: '%'

            # 16
            Button:
                text: '%'

            # 17
            Button:
                text: '%'

            # 18
            Button:
                text: '%'

            # 19
            Button:
                text: '%'

            # 20
            Button:
                text: '%'

        MDGridLayout: 
            id: calc_buttons

            cols: 5
            rows: 4

            # 1
            Button:
                text: '%'
            # 2
            Button:
                text: '%'

            # 3
            Button:
                text: '%'

            # 4
            Button:
                text: '%'

            # 5
            Button:
                text: '%'

            # 6
            Button:
                text: '%'

            # 7
            Button:
                text: '%'

            # 8
            Button:
                text: '%'

            # 9
            Button:
                text: '%'

            # 10
            Button:
                text: '%'

            # 11
            Button:
                text: '%'

            # 12
            Button:
                text: '%'

            # 13
            Button:
                text: '%'

            # 14
            Button:
                text: '%'

            # 15
            Button:
                text: '%'

            # 16
            Button:
                text: '%'

            # 17
            Button:
                text: '%'

            # 18
            Button:
                text: '%'

            # 19
            Button:
                text: '%'

            # 20
            Button:
                text: '%'

        GridLayout: 
            canvas.after:
                Color:
                    rgba: hex('#FF66994C')

                Rectangle:
                    pos: self.pos
                    size: self.size

            id: calc_buttons

            cols: 5
            rows: 4

            # 1
            MDRectangleFlatButton:
                text: '%'
            # 2
            MDRectangleFlatButton:
                text: '%'

            # 3
            MDRectangleFlatButton:
                text: '%'

            # 4
            MDRectangleFlatButton:
                text: '%'

            # 5
            MDRectangleFlatButton:
                text: '%'

            # 6
            MDRectangleFlatButton:
                text: '%'

            # 7
            MDRectangleFlatButton:
                text: '%'

            # 8
            MDRectangleFlatButton:
                text: '%'

            # 9
            MDRectangleFlatButton:
                text: '%'

            # 10
            MDRectangleFlatButton:
                text: '%'

            # 11
            MDRectangleFlatButton:
                text: '%'

            # 12
            MDRectangleFlatButton:
                text: '%'

            # 13
            MDRectangleFlatButton:
                text: '%'

            # 14
            MDRectangleFlatButton:
                text: '%'

            # 15
            MDRectangleFlatButton:
                text: '%'

            # 16
            MDRectangleFlatButton:
                text: '%'

            # 17
            MDRectangleFlatButton:
                text: '%'

            # 18
            MDRectangleFlatButton:
                text: '%'

            # 19
            MDRectangleFlatButton:
                text: '%'

            # 20
            MDRectangleFlatButton:
                text: '%'

        MDGridLayout: 
            md_bg_color: hex('66FF994C')
            id: calc_buttons

            cols: 5
            rows: 4

            # 1
            MDRectangleFlatButton:
                text: '%'
            # 2
            MDRectangleFlatButton:
                text: '%'

            # 3
            MDRectangleFlatButton:
                text: '%'

            # 4
            MDRectangleFlatButton:
                text: '%'

            # 5
            MDRectangleFlatButton:
                text: '%'

            # 6
            MDRectangleFlatButton:
                text: '%'

            # 7
            MDRectangleFlatButton:
                text: '%'

            # 8
            MDRectangleFlatButton:
                text: '%'

            # 9
            MDRectangleFlatButton:
                text: '%'

            # 10
            MDRectangleFlatButton:
                text: '%'

            # 11
            MDRectangleFlatButton:
                text: '%'

            # 12
            MDRectangleFlatButton:
                text: '%'

            # 13
            MDRectangleFlatButton:
                text: '%'

            # 14
            MDRectangleFlatButton:
                text: '%'

            # 15
            MDRectangleFlatButton:
                text: '%'

            # 16
            MDRectangleFlatButton:
                text: '%'

            # 17
            MDRectangleFlatButton:
                text: '%'

            # 18
            MDRectangleFlatButton:
                text: '%'

            # 19
            MDRectangleFlatButton:
                text: '%'

            # 20
            MDRectangleFlatButton:
                text: '%'

应用运行的图片

编辑 我试图在按钮之前的 GridLayout 中计算每个按钮的大小。按钮的初始大小仍然是它们的默认大小。当我调整应用程序的窗口大小时它可以工作。那么如何才能使它们的初始大小与我计算的一样呢?

calculator.kv

MainWidget:

#: import hex kivy.utils.get_color_from_hex

<MainWidget>:

    MDBoxLayout: 
        id: main_box_layout 

        size: root.width, root.height 
        orientation: 'vertical'

        TextInput:
            id: calc_screen
            
            font_size: dp(25)
            size_hint: 1, .2
            halign: 'left'
            text: '0'
        
        MDGridLayout: 
            id: calc_buttons

            orientation: 'lr-bt'
            cols: 5
            rows: 4

            button_width: self.width / 5
            button_height: self.height / 4

            # 1
            MDRectangleFlatButton:
                text: '0'
                font_size: dp(20)
                size: self.parent.button_width, self.parent.button_height

                on_release: root.on_release_number_operator_button(self, self.text)

            # 2
            MDRectangleFlatButton:
                text: '.'
                font_size: dp(20)
                size: self.parent.button_width, self.parent.button_height
                on_release: root.on_release_number_operator_button(self, self.text)

            # 3
            MDRectangleFlatButton:
                text: '('
                font_size: dp(20)
                size: self.parent.button_width, self.parent.button_height
                on_release: root.on_release_number_operator_button(self, self.text)

            # 4
            MDRectangleFlatButton:
                text: ')'
                font_size: dp(20)
                size: self.parent.button_width, self.parent.button_height
                on_release: root.on_release_number_operator_button(self, self.text)

            # 4.5 cuz i forgot it
            MDRectangleFlatButton:
                text: '='
                font_size: dp(20)
                size: self.parent.button_width, self.parent.button_height
                on_release: root.on_release_equal()

            # 5
            MDRectangleFlatButton:
                text: '1'
                font_size: dp(20)
                size: self.parent.button_width, self.parent.button_height
                on_release: root.on_release_number_operator_button(self, self.text)

            # 6
            MDRectangleFlatButton:
                text: '2'
                font_size: dp(20)
                size: self.parent.button_width, self.parent.button_height
                on_release: root.on_release_number_operator_button(self, self.text)

            # 7
            MDRectangleFlatButton:
                text: '3'
                font_size: dp(20)
                size: self.parent.button_width, self.parent.button_height
                on_release: root.on_release_number_operator_button(self, self.text)

            # 8
            MDRectangleFlatButton:
                text: '+'
                font_size: dp(20)
                size: self.parent.button_width, self.parent.button_height
                on_release: root.on_release_number_operator_button(self, self.text)

            # 9
            MDRectangleFlatButton:
                text: '-'
                font_size: dp(20)
                size: self.parent.button_width, self.parent.button_height
                on_release: root.on_release_number_operator_button(self, self.text)

            # 10
            MDRectangleFlatButton:
                text: '4'
                font_size: dp(20)
                size: self.parent.button_width, self.parent.button_height
                on_release: root.on_release_number_operator_button(self, self.text)

            # 11
            MDRectangleFlatButton:
                text: '5'
                font_size: dp(20)
                size: self.parent.button_width, self.parent.button_height
                on_release: root.on_release_number_operator_button(self, self.text)

            # 12
            MDRectangleFlatButton:
                text: '6'
                font_size: dp(20)
                size: self.parent.button_width, self.parent.button_height
                on_release: root.on_release_number_operator_button(self, self.text)

            # 13
            MDRectangleFlatButton:
                text: 'x'
                font_size: dp(20)
                size: self.parent.button_width, self.parent.button_height
                on_release: root.on_release_number_operator_button(self, '*')

            # 14
            MDRectangleFlatButton:
                text: ':'
                font_size: dp(20)
                size: self.parent.button_width, self.parent.button_height
                on_release: root.on_release_number_operator_button(self, '/')

            # 15
            MDRectangleFlatButton:
                text: '7'
                font_size: dp(20)
                size: self.parent.button_width, self.parent.button_height
                on_release: root.on_release_number_operator_button(self, self.text)

            # 16
            MDRectangleFlatButton:
                text: '8'
                font_size: dp(20)
                size: self.parent.button_width, self.parent.button_height
                on_release: root.on_release_number_operator_button(self, self.text)

            # 17
            MDRectangleFlatButton:
                text: '9'
                font_size: dp(20)
                size: self.parent.button_width, self.parent.button_height
                on_release: root.on_release_number_operator_button(self, self.text)

            # 18
            MDRectangleFlatButton:
                text: 'DEL'
                font_size: dp(20)
                size: self.parent.button_width, self.parent.button_height
                on_release: root.on_release_delete()

            # 19
            MDRectangleFlatButton:
                text: 'AC'
                font_size: dp(20)
                size: self.parent.button_width, self.parent.button_height
                on_release: root.on_release_all_clear()

调整窗口大小之前

调整窗口大小后

【问题讨论】:

【参考方案1】:

这是 KivyMD Buttons 的“功能”。其中许多Buttonssize_hint 设置为(None, None),其大小由KivyMd 设置。

来自documentation:

您不能对 KivyMD 按钮使用 size_hint_x 参数(宽度 按钮的个数是自动设置的)!

但是,如果需要增加按钮的宽度,您可以 可以使用参数increment_width:

MDFlatButton:
    text: "MDFLATBUTTON"
    increment_width: "164dp"

【讨论】:

谢谢。所以我不应该尝试让 KivyMD 的按钮大小自适应? 当然你可以让它们适应大小。只是让您知道为什么必须使用size_hint: 1,1。不知道为什么你会收到Clock 警告,或者你的应用为什么滞后,因为我没有看到这种行为。

以上是关于MDRectangleFlatButton(s) 不采用它们在 GridLayout 或 MDGridLayout 中应该具有的大小。 Kivy 的按钮工作正常的主要内容,如果未能解决你的问题,请参考以下文章

在kivy中设置动态类

如何将参数传递给 kivy 按钮的绑定函数

+-r, +-s 的所有排列

象棋相关

象棋相关

s-s-rS 报告 - 从没有 s-s-rS 服务器的 C# 导出