gpt4 book ai didi

python - 如何在 Kivy/KivyMD 中更改 MDRectangleFlatIconButton 文本颜色

转载 作者:行者123 更新时间:2023-12-01 07:28:25 24 4
gpt4 key购买 nike

我正在使用 Kivy 和 KivyMD 创建布局,并希望更改 MD 按钮中显示的文本颜色,但颜色仍然停留在浅蓝色。

我在下面的代码中包含了我尝试过的示例。

.py代码

import kivy, kivymd

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.theming import ThemeManager


class ButtonColorApp(App):

theme_cls = ThemeManager()
title='RUCES'

def build(self):
self.theme_cls.theme_style = "Dark"
sm = ScreenManager()
sm.add_widget(IntroPage(name="intro_page"))
return sm

class IntroPage(Screen):

#funcs and vars
pass

def main():
but = ButtonColorApp()
but.run()

if __name__ == "__main__":
main()

.kv代码

#: import MDRectangleFlatIconButton kivymd.button.MDRectangleFlatIconButton
#: import MDLabel kivymd.label.MDLabel

<MyButton@MDRectangleFlatIconButton>:
text_size: self.size * 3
theme_text_color: 'Custom'
font_size: 20
md_bg_color: (0,0,.4,1)
canvas.before:
Color:
rgba: (0,0,0,1)
Line:
width: 0.5
rectangle: (self.x, self.y, self.width, self.height)


<IntroPage>:

BoxLayout:

orientation: "vertical"

MyButton:
size_hint_x: 1
theme_text_color: 'Custom'
text: "Colour Me!"
text_color: (1,0,0,1)

当我运行此命令时,我希望按钮文本为红色,但如上所述,它仍然是浅蓝色。任何帮助表示赞赏!

最佳答案

问题

目前,MDRectangleFlatIconButtonMDRoundFlatIconButton 小部件的文本颜色将始终默认为 theme_cls.primary_color即使有属性,theme_text_color: 'Custom'text_color: [1,0,0,1] .

解决方案

临时解决办法如下:

  1. 在您的 kv 文件中,替换 import 语句, kivymd.button.MDRectangleFlatIconButton与您定制的button.pybutton.MDRectangleFlatIconButton
  2. my GitHub 获取 button.py 的副本
  3. 或者从 /usr/local/lib/python3.7/dist-packages/kivymd 获取 button.py 的副本,或~/KivyMD (由 git clone https://github.com/HeaTTheatR/KivyMD.git 创建),或下载并解压 KivyMD Zip 文件 ( Download ZIP ),然后应用以下更改

替换:

        theme_text_color: 'Custom'
text_color: root.theme_cls.primary_color

与:

        theme_text_color: root.theme_text_color   
text_color: root.text_color

片段:button.py - kv

<MDRectangleFlatIconButton>
...
theme_text_color: 'Custom'
text_color: root.theme_cls.primary_color

BoxLayout:
...

MDIcon:
...

MDLabel:
..
theme_text_color: root.theme_text_color
text_color: root.text_color
markup: root.markup

<MDRoundFlatIconButton>
...
theme_text_color: 'Custom'
text_color: root.theme_cls.primary_color

BoxLayout:
...
MDIcon:
...

MDLabel:
...
theme_text_color: root.theme_text_color
text_color: root.text_color
markup: root.markup

输出

Result

KivyMD GitHub

关于python - 如何在 Kivy/KivyMD 中更改 MDRectangleFlatIconButton 文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57331114/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com