我正在使用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)
运行此程序时,我希望按钮文本为红色,但如上所述,它仍为浅蓝色。任何帮助表示赞赏!
最佳答案
问题
当前,即使具有属性theme_cls.primary_color
和theme_text_color: 'Custom'
,MDRectangleFlatIconButton和MDRoundFlatIconButton小部件的文本颜色也将始终默认为text_color: [1,0,0,1]
。
解
临时解决方案如下:
在您的kv文件中,替换import语句,kivymd.button.MDRectangleFlatIconButton
与您的自定义
button.py,即button.MDRectangleFlatIconButton
要么从my GitHub拷贝button.py
或从/usr/local/lib/python3.7/dist-packages/kivymd
或~/KivyMD
(由git clone https://github.com/HeaTTheatR/KivyMD.git
创建)获取button.py的副本,或下载并提取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
输出量
KivyMD GitHub