[英]How to change MDRectangleFlatIconButton text colour in Kivy/KivyMD
我正在使用 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)
当我运行它时,我希望按钮文本为红色,但如上所述它仍然是浅蓝色。 任何帮助表示赞赏!
目前, MDRectangleFlatIconButton和MDRoundFlatIconButton小部件的文本颜色将始终默认为theme_cls.primary_color
即使具有属性theme_text_color: 'Custom'
和text_color: [1,0,0,1]
。
临时解决方法如下:
kivymd.button.MDRectangleFlatIconButton
替换为您自定义的button.py,即button.MDRectangleFlatIconButton
/usr/local/lib/python3.7/dist-packages/kivymd
或~/KivyMD
(由git clone https://github.com/HeaTTheatR/KivyMD.git
创建) ~/KivyMD
button.py的副本,或下载并解压缩 KivyMD Zip 文件(下载 ZIP ),并应用以下更改代替:
theme_text_color: 'Custom'
text_color: root.theme_cls.primary_color
和:
theme_text_color: root.theme_text_color
text_color: root.text_color
<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
另一种解决方法是使用标记语言,例如:
.kv
文件中,将"id"
分配给您的MDButton
MDFlatButton:
id: info_btn
.py
文件中self.ids.info_btn.text = '[color=#00ffcc]Info[/color]'
self.ids.info_btn.markup = True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.