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

KivyMD按钮边框颜色无法修改问题求助:使用line_color属性无效

Fixing MDRoundFlatButton Line Color Not Working in KivyMD

I’ve run into this exact issue before! The problem here is two-fold: your buttons are still using the default KivyMD theme for their border color, and the default border width is set to 0 (so even if the color was correct, you wouldn’t see it). Here’s how to fix it:

Key Changes Needed:

  • Add theme_line_color: "Custom" to both buttons to override the default theme and activate your custom line_color setting.
  • Set a line_width value greater than 0 (e.g., line_width: 2) to make the border visible.

Modified Working Code:

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

Root="""
#:import hex kivy.utils.get_color_from_hex
BoxLayout:
    id:Layout_1
    padding: 20
    spacing: 20
    MDRoundFlatButton:
        size_hint: None, None
        size: 220, 50  # Explicit size for better visibility
        text:"MDRoundFlatButton"
        theme_text_color: "Custom"
        text_color: 0, 0, 0, 1  # Black text for contrast with white background
        font_size:16
        id:btn
        theme_line_color: "Custom"  # Enable custom border color
        line_color: 1, 1, 0, 1     # Yellow border
        line_width: 2              # Set border thickness
        md_bg_color:1, 1, 1, 1     # White background
    MDRectangleFlatButton:
        text: "MDRECTANGLEFLATBUTTON"
        theme_text_color: "Custom"
        text_color: 1, 0, 0, 1
        theme_line_color: "Custom"  # Enable custom border color
        line_color: 1, 1, 0, 1     # Yellow border
        line_width: 2              # Set border thickness
"""

class Main(MDApp):
    def build(self):
        return Builder.load_string(Root)

Main().run()

Explanation:

  • theme_line_color: "Custom" tells KivyMD to ignore the default theme’s border color and use your specified line_color instead.
  • line_width: 2 sets the thickness of the border—without this, the border has zero width and remains invisible, even if the color is correctly set.
  • I added padding/spacing to the layout and adjusted the text color to black to make the button content more readable against the white background and yellow border.

This should give you the yellow border you’re expecting on both buttons!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 15:27:42