使用KivyMD和Python创建自定义Widget

t2a7ltrp  于 2023-11-20  发布在  Python
关注(0)|答案(1)|浏览(130)

我在想,如果我想创建一个小部件,显示一个球员的分数,如上传的图像所示,我该怎么做?我知道有一种方法可以使用纹理和图标来获得结果,但它只是不适合我。我希望能够使用自定义图像的图标出现在窗口小部件的左侧和右侧。此外,我希望能够根据玩家的活动增加或减少分数。a score widget
我已经通读了文档和主题,如MDLabels和MDDropdownMenu列表项,但它就是不工作。
这是我的代码。它工作正常,在大多数情况下。然而,我找不到小部件来放置三个小部件(MDIconButtonMDLabelMDIconButton)。此外,我注解掉了这行text_size: (20, 20)和这一行font_size: "40sp",因为我仍在试图弄清楚如何使它们工作。如果你也能帮助,那就太好了。

from kivymd.app import MDApp
from kivy.lang import Builder

kv = '''

MDScreen:

    MDBoxLayout:
        orientation: "vertical"
        size_hint: (1, 1)
        spacing: dp(10)
        padding: dp(5)

        MDBoxLayout:
            orientation: "horizontal"
            size_hint: (1, .06)
            md_bg_color: "orange"

            # I'm not sure why this isn't working
            # I was thinking of putting the three widgets 
            # (MDIconButton, MDLabel, MDIconButton) in here
            # RoundedRectangle:
            #   pos: (5, 1400)
            #   size: (100, 20)
            #   radius: [18, ] * 4

            MDIconButton:
                icon: "language-python"
                _no_ripple_effect: False
                on_release: app.python_message()

            MDLabel:
                id: score
                text: "281"
                # text_size: (20, 20)
                halign: "center"
                font_style: "Button"

            MDIconButton:
                icon: "plus"
                theme_icon_color: "Custom"
                icon_color: "white"
                md_bg_color: "blue"
                # font_size: "40sp"
                _no_ripple_effect: True
                on_release: app.plus_sign_action()

        MDBoxLayout:
            size_hint: (1, .94)
            md_bg_color: "black"

            MDLabel:
                text: "Other Widgets"
                bold: True
                halign: "center"
                theme_text_color: "Custom"
                text_color: "orange"


'''

class Example(MDApp):
    def build(self):
        self.screen = Builder.load_string(kv)

        self.theme_cls.theme_style = "Dark"
        self.theme_cls.primary_palette = "Orange"

        return self.screen

    def python_message(self, *args):
        pass

    def plus_sign_action(self, *args):
        pass

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

字符串

flseospp

flseospp1#

如果我理解你的问题,你可以通过创建一个基于MDBoxLayout的自定义小部件来获得一个外观合理的小部件。该类可以定义为:

class MyCustomWidget(MDBoxLayout):
    # these are the attributes to set in the kv
    left_icon = StringProperty('')
    left_action = ObjectProperty(None)
    text = StringProperty(' ')
    right_icon = StringProperty('')
    right_action = ObjectProperty(None)

字符串
kv中添加了一条规则:

<MyCustomWidget>:
    orientation: "horizontal"
    adaptive_size: True  # Use adaptive size
    md_bg_color: "orange"

    MDIconButton:
        icon: root.left_icon
        _no_ripple_effect: False
        on_release: root.left_action() if root.left_action else None

    Label:  # MDLabel gives odd behavior when length of text increases
        text: root.text
        font_size: 40
        size_hint: None, None
        size: self.texture_size
        pos_hint: {'center_y': 0.5}

    MDIconButton:
        icon: root.right_icon
        theme_icon_color: "Custom"
        icon_color: "white"
        md_bg_color: "blue"
        _no_ripple_effect: True
        on_release: root.right_action() if root.right_action else None


然后,您可以在kv中使用它来替换MDBoxLayout

MyCustomWidget:
        id: custom
        left_icon: 'language-python'
        left_action: app.python_message
        text: "281"
        right_icon: 'plus'
        right_action: app.plus_sign_action


你的plus_sign_action()方法可以修改分数,例如:

def plus_sign_action(self, *args):
    score = int(self.root.ids.custom.text)
    score += 1
    self.root.ids.custom.text = str(score)

相关问题