You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用KivyMD和Python创建自定义玩家分数组件?

玩家分数显示Widget问题修复与功能实现

1. 容器选择:用MDCard实现带圆角的分数栏

你之前尝试用RoundedRectangle做背景,但它是图形指令,无法直接容纳其他组件。推荐用MDCard,它自带背景色、圆角设置,还能作为容器嵌套水平布局来放置三个组件:

  • 外层MDCard负责设置圆角和背景色
  • 内部嵌套水平方向的MDBoxLayout,用来排列减号按钮、分数标签、加号按钮

2. 文本大小设置问题修复

  • text_size是控制文本换行的区域尺寸,不是字体大小,你混淆了这个属性的作用。
  • MDLabel的font_style会预设字体大小,若要自定义字体大小,要么去掉font_style属性直接设置font_size;要么保留font_style的同时,通过theme_text_color: "Custom"启用自定义字体大小。

3. 替换为自定义图标

MDIconButton支持使用本地图片作为图标,只需把icon属性换成source,指定图片路径即可,示例:

MDIconButton:
    source: "minus_icon.png"  # 替换为你的自定义减号图片路径

4. 实现分数增减功能

在Python代码中,获取MDLabel的text属性,转换为整数后进行增减操作,再赋值回去即可。


修改后的完整代码

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

kv = '''
MDScreen:
    MDBoxLayout:
        orientation: "vertical"
        size_hint: (1, 1)
        spacing: dp(10)
        padding: dp(5)

        # 使用MDCard作为分数栏容器,实现圆角背景
        MDCard:
            size_hint: (1, .06)
            radius: [dp(18), ] * 4  # 设置圆角
            md_bg_color: "orange"
            
            MDBoxLayout:
                orientation: "horizontal"
                size_hint: (1, 1)
                padding: dp(5)
                spacing: dp(20)
                
                # 自定义减号按钮
                MDIconButton:
                    source: "minus_icon.png"  # 替换为你的自定义图片路径
                    _no_ripple_effect: False
                    on_release: app.minus_score()

                MDLabel:
                    id: score
                    text: "281"
                    halign: "center"
                    font_size: "24sp"  # 自定义字体大小
                    theme_text_color: "Custom"
                    text_color: "black"

                # 自定义加号按钮
                MDIconButton:
                    source: "plus_icon.png"  # 替换为你的自定义图片路径
                    theme_icon_color: "Custom"
                    icon_color: "white"
                    md_bg_color: "blue"
                    _no_ripple_effect: True
                    on_release: app.plus_score()

        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 minus_score(self, *args):
        # 实现分数减少逻辑
        current_score = int(self.screen.ids.score.text)
        self.screen.ids.score.text = str(current_score - 1)

    def plus_score(self, *args):
        # 实现分数增加逻辑
        current_score = int(self.screen.ids.score.text)
        self.screen.ids.score.text = str(current_score + 1)

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

内容的提问来源于stack exchange,提问作者Timson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 06:23:33