在 kivymd 中更改弹出窗口 window 上的 MDRaisedButton 的文本颜色?
change text color of a MDRaisedButton on a popup window in kivymd?
我是新手,所以我有一个愚蠢的问题。但它对我不起作用。
当我第一次点击这个程序时,它会打开一个带有两个按钮的弹出按钮。
我想为每个按钮设置不同的文本颜色和不同的背景颜色。
我正在尝试使用 KivyMD。因此,我很乐意为这两个按钮使用 MDRaidButton。
我想要一个带有黑色文本颜色和蓝色背景的按钮,另一个按钮带有白色和黑色背景。
非常感谢你的帮助。
Python 文件
'''
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.config import Config
Config.set('graphics', 'width', '600')
Config.set('graphics', 'height', '750')
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.factory import Factory
from kivy.uix.screenmanager import ScreenManager
from kivy.properties import ObjectProperty
from kaki.app import App
from kivy.factory import Factory
import os
from kivymd.uix.datatables import MDDataTable
from kivy.uix.popup import Popup
from kivy.graphics import Rectangle, Color
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.metrics import dp
Builder.load_file('new_window_mo.kv')
class Dex(Popup):
ptName = ObjectProperty(None)
ptAdminWeight = ObjectProperty(None)
ptIdealWeight = ObjectProperty(None)
def on_open(self):
self.ptName.focus = True
self.data_tables = None
def calculate(self):
ptName = self.ptName.text
ptAdminWeight = self.ptAdminWeight.text
ptIdealWeight = self.ptIdealWeight.text
self.data_tables = MDDataTable(
size_hint=(0.9, 0.8),
column_data=[
("No.", dp(30)),
("User", dp(30)),
("Password", dp(30)),
],
row_data=[
(
"1",
"The pitonist",
"Strong password",
),
(
"2",
"The c++ lover",
"Save me!!!:)",
),
]
)
self.ids.data_layout.add_widget(self.data_tables)
def print(self):
ptName = self.ptName.text
ptAdminWeight = self.ptAdminWeight.text
ptIdealWeight = self.ptIdealWeight.text
printme(self, ptName, ptAdminWeight, ptIdealWeight)
def clear(self):
self.ptName.text =""
self.ptAdminWeight.text =""
self.ptIdealWeight.text = ""
class Remi(Popup):
def build(self):
return self.root
class test1(Popup):
def build(self):
return self.root
class MyLayout(Widget):
pass
class AwesomeApp(MDApp):
def build(self):
return MyLayout()
if __name__ == '__main__':
AwesomeApp().run()
'''
kivy 文件
'''
#:import Factory kivy.factory.Factory
<Dex>:
ptName : ptName
ptAdminWeight : ptAdminWeight
ptIdealWeight : ptIdealWeight
auto_dismiss: False
size_hint: 1, 1
#pos_hint: {"x":0.2, "top":0.9}
title: "Dex - Weight-Based"
canvas.before:
Color:
rgba: (1,0,0,1)
Rectangle:
pos:self.pos
size:self.size
MDGridLayout: #전체 블록 1칼럼 4row
adaptive_height: True
md_bg_color: app.theme_cls.primary_color
md_bd_color: app.theme_cls.accent_palette
cols: 1
rows: 4
size: root.width, root.height
size_hint:1,1
MDBoxLayout: #첫 번째 약 이름
adaptive_height: True
md_bg_color: app.theme_cls.primary_color
size_hint: 1, .4
orientation: "vertical"
padding: dp(0), dp(30), dp(0), dp(10)
spacing: dp(20)
MDLabel:
text:"Dex"
halign: "center"
font_size: '45sp'
MDLabel:
text:"Pre"
halign: "center"
font_size: '30sp'
#환자이름,몸무게 정보 넣기
MDGridLayout:
rows: 3
adaptive_height: True
size_hint:1, None
height: self.minimum_height
#spacing: dp(15)
padding: dp(50), dp(10), dp(50), dp(20)
MDTextField:
id: ptName
#input_filter: "str"
hint_text: "Patient Name"
line_color_focus: 0,0,0,1
font_size: '30sp'
focus: True
write_tab: False
MDTextField:
id:ptAdminWeight
input_filter: "float"
hint_text: "Admission Weight"
required: True
font_size: '30sp'
line_color_focus: 0.9,0.75,0,1
write_tab: False
MDTextField:
id:ptIdealWeight
input_filter: "float"
hint_text: "Idea Weight"
required: True
font_size: '30sp'
line_color_focus: 0.9,0.75,0,1
write_tab: False
MDGridLayout: #buttons
cols: 4
rows: 1
size_hint: 1, .25
pos_hint: 1, None
pos_hint: {'center_y':0.5}
padding: "20dp", "0dp", "20dp", "15dp"
spacing: "20dp"
MDRoundFlatIconButton:
text: "Calculate"
icon: "calculator"
pos_hint: {"center_y": .5}
font_size: '20sp'
size_hint: .3, 1
#theme_text_color: "Custom"
text_color: 1,1,1,1
icon_color: 1,1,1,1
line_color: 1,1,1,1
line_width: 2
on_release: root.calculate()
write_tab: False
MDRoundFlatIconButton:
text: "Print"
icon: "printer"
pos_hint: {"center_y": .5}
font_size: '20sp'
size_hint: .3, 1
#theme_text_color: "Custom"
text_color: 1,1,1,1
icon_color: 1,1,1,1
line_color: 1,1,1,1
line_width: 2
write_tab: False
on_release: root.print()
MDRoundFlatIconButton:
text: "New"
icon: "close-thick"
font_size: '20sp'
size_hint: .3, 1
pos_hint: {'center_y': 0.5}
text_color: 1,1,1,1
icon_color: 1,1,1,1
line_color: 1,1,1,1
line_width: 2
on_release: root.clear()
BoxLayout:
id: data_layout
canvas:
Color:
rgba: [1,1,.1,.6]
Rectangle:
pos:self.pos
size:self.size
<Remi>:
auto_dismiss: False
size_hint: 1, 1
title: "Remi Weight-Based "
canvas.before:
Color:
rgba: (0,1,0,1)
Rectangle:
pos:self.pos
size:self.size
BoxLayout:
orientation: "vertical"
size:root.width, root.height
Label:
text: "label 1"
Button:
text: "Close"
font_size: 24
on_release: root.dismiss()
<MyLayout>
BoxLayout:
orientation:"vertical"
size: root.width, root.height
#md_bg_color: app.theme_cls.primary_color
MDRaisedButton: #This is the problem area
#id: data_scr
text: "Dex"
font_size: 32
theme_text_color: "Custom"
#text_color: 0,0,0,1
#color: [0,0,0,1]
#background_color: app.theme_cls.primary_color
#line_color: [0,0,0,1]
#theme_text_color: root.theme_text_color
text_color: (0,0,0,1)
size_hint: 1,.5
on_press: Factory.Dex().open()
#Button:
MDRaisedButton:
text: "Remifentanyl"
font_size: 32
size_hint: 1,.5
on_press: Factory.Remi().open()
'''
使用 md_bg_color
设置背景颜色应该没有任何问题。但是当您尝试设置 text_color
时可能会出现一些问题,尤其是对于值 # [0.0, 0.0, 0.0, 0.87]
, [0.0, 0.0, 0.0, 1.0]
和 [1.0, 1.0, 1.0, 1.0]
(这看起来很尴尬!)。除了这些值,您可以自由设置 rgba
格式的任何有效颜色代码。
因此,对于黑色文本,您可以像 kvlang
中的 text_color: 0, 0, 0, 0.99
那样设置它(但这可能会在未来的版本中改变;我在 1.0.0 中没有找到任何内容)。或者为了保持向后兼容性,也许您可以设置 theme_text_color: "Primary"
等
#实际上在 KivyMD v0.104.2 的 source code 中有一个 code-block 用于那些限制值。
我是新手,所以我有一个愚蠢的问题。但它对我不起作用。
当我第一次点击这个程序时,它会打开一个带有两个按钮的弹出按钮。
我想为每个按钮设置不同的文本颜色和不同的背景颜色。
我正在尝试使用 KivyMD。因此,我很乐意为这两个按钮使用 MDRaidButton。
我想要一个带有黑色文本颜色和蓝色背景的按钮,另一个按钮带有白色和黑色背景。
非常感谢你的帮助。
Python 文件
'''
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.config import Config
Config.set('graphics', 'width', '600')
Config.set('graphics', 'height', '750')
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.factory import Factory
from kivy.uix.screenmanager import ScreenManager
from kivy.properties import ObjectProperty
from kaki.app import App
from kivy.factory import Factory
import os
from kivymd.uix.datatables import MDDataTable
from kivy.uix.popup import Popup
from kivy.graphics import Rectangle, Color
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.metrics import dp
Builder.load_file('new_window_mo.kv')
class Dex(Popup):
ptName = ObjectProperty(None)
ptAdminWeight = ObjectProperty(None)
ptIdealWeight = ObjectProperty(None)
def on_open(self):
self.ptName.focus = True
self.data_tables = None
def calculate(self):
ptName = self.ptName.text
ptAdminWeight = self.ptAdminWeight.text
ptIdealWeight = self.ptIdealWeight.text
self.data_tables = MDDataTable(
size_hint=(0.9, 0.8),
column_data=[
("No.", dp(30)),
("User", dp(30)),
("Password", dp(30)),
],
row_data=[
(
"1",
"The pitonist",
"Strong password",
),
(
"2",
"The c++ lover",
"Save me!!!:)",
),
]
)
self.ids.data_layout.add_widget(self.data_tables)
def print(self):
ptName = self.ptName.text
ptAdminWeight = self.ptAdminWeight.text
ptIdealWeight = self.ptIdealWeight.text
printme(self, ptName, ptAdminWeight, ptIdealWeight)
def clear(self):
self.ptName.text =""
self.ptAdminWeight.text =""
self.ptIdealWeight.text = ""
class Remi(Popup):
def build(self):
return self.root
class test1(Popup):
def build(self):
return self.root
class MyLayout(Widget):
pass
class AwesomeApp(MDApp):
def build(self):
return MyLayout()
if __name__ == '__main__':
AwesomeApp().run()
'''
kivy 文件
'''
#:import Factory kivy.factory.Factory
<Dex>:
ptName : ptName
ptAdminWeight : ptAdminWeight
ptIdealWeight : ptIdealWeight
auto_dismiss: False
size_hint: 1, 1
#pos_hint: {"x":0.2, "top":0.9}
title: "Dex - Weight-Based"
canvas.before:
Color:
rgba: (1,0,0,1)
Rectangle:
pos:self.pos
size:self.size
MDGridLayout: #전체 블록 1칼럼 4row
adaptive_height: True
md_bg_color: app.theme_cls.primary_color
md_bd_color: app.theme_cls.accent_palette
cols: 1
rows: 4
size: root.width, root.height
size_hint:1,1
MDBoxLayout: #첫 번째 약 이름
adaptive_height: True
md_bg_color: app.theme_cls.primary_color
size_hint: 1, .4
orientation: "vertical"
padding: dp(0), dp(30), dp(0), dp(10)
spacing: dp(20)
MDLabel:
text:"Dex"
halign: "center"
font_size: '45sp'
MDLabel:
text:"Pre"
halign: "center"
font_size: '30sp'
#환자이름,몸무게 정보 넣기
MDGridLayout:
rows: 3
adaptive_height: True
size_hint:1, None
height: self.minimum_height
#spacing: dp(15)
padding: dp(50), dp(10), dp(50), dp(20)
MDTextField:
id: ptName
#input_filter: "str"
hint_text: "Patient Name"
line_color_focus: 0,0,0,1
font_size: '30sp'
focus: True
write_tab: False
MDTextField:
id:ptAdminWeight
input_filter: "float"
hint_text: "Admission Weight"
required: True
font_size: '30sp'
line_color_focus: 0.9,0.75,0,1
write_tab: False
MDTextField:
id:ptIdealWeight
input_filter: "float"
hint_text: "Idea Weight"
required: True
font_size: '30sp'
line_color_focus: 0.9,0.75,0,1
write_tab: False
MDGridLayout: #buttons
cols: 4
rows: 1
size_hint: 1, .25
pos_hint: 1, None
pos_hint: {'center_y':0.5}
padding: "20dp", "0dp", "20dp", "15dp"
spacing: "20dp"
MDRoundFlatIconButton:
text: "Calculate"
icon: "calculator"
pos_hint: {"center_y": .5}
font_size: '20sp'
size_hint: .3, 1
#theme_text_color: "Custom"
text_color: 1,1,1,1
icon_color: 1,1,1,1
line_color: 1,1,1,1
line_width: 2
on_release: root.calculate()
write_tab: False
MDRoundFlatIconButton:
text: "Print"
icon: "printer"
pos_hint: {"center_y": .5}
font_size: '20sp'
size_hint: .3, 1
#theme_text_color: "Custom"
text_color: 1,1,1,1
icon_color: 1,1,1,1
line_color: 1,1,1,1
line_width: 2
write_tab: False
on_release: root.print()
MDRoundFlatIconButton:
text: "New"
icon: "close-thick"
font_size: '20sp'
size_hint: .3, 1
pos_hint: {'center_y': 0.5}
text_color: 1,1,1,1
icon_color: 1,1,1,1
line_color: 1,1,1,1
line_width: 2
on_release: root.clear()
BoxLayout:
id: data_layout
canvas:
Color:
rgba: [1,1,.1,.6]
Rectangle:
pos:self.pos
size:self.size
<Remi>:
auto_dismiss: False
size_hint: 1, 1
title: "Remi Weight-Based "
canvas.before:
Color:
rgba: (0,1,0,1)
Rectangle:
pos:self.pos
size:self.size
BoxLayout:
orientation: "vertical"
size:root.width, root.height
Label:
text: "label 1"
Button:
text: "Close"
font_size: 24
on_release: root.dismiss()
<MyLayout>
BoxLayout:
orientation:"vertical"
size: root.width, root.height
#md_bg_color: app.theme_cls.primary_color
MDRaisedButton: #This is the problem area
#id: data_scr
text: "Dex"
font_size: 32
theme_text_color: "Custom"
#text_color: 0,0,0,1
#color: [0,0,0,1]
#background_color: app.theme_cls.primary_color
#line_color: [0,0,0,1]
#theme_text_color: root.theme_text_color
text_color: (0,0,0,1)
size_hint: 1,.5
on_press: Factory.Dex().open()
#Button:
MDRaisedButton:
text: "Remifentanyl"
font_size: 32
size_hint: 1,.5
on_press: Factory.Remi().open()
'''
使用 md_bg_color
设置背景颜色应该没有任何问题。但是当您尝试设置 text_color
时可能会出现一些问题,尤其是对于值 # [0.0, 0.0, 0.0, 0.87]
, [0.0, 0.0, 0.0, 1.0]
和 [1.0, 1.0, 1.0, 1.0]
(这看起来很尴尬!)。除了这些值,您可以自由设置 rgba
格式的任何有效颜色代码。
因此,对于黑色文本,您可以像 kvlang
中的 text_color: 0, 0, 0, 0.99
那样设置它(但这可能会在未来的版本中改变;我在 1.0.0 中没有找到任何内容)。或者为了保持向后兼容性,也许您可以设置 theme_text_color: "Primary"
等
#实际上在 KivyMD v0.104.2 的 source code 中有一个 code-block 用于那些限制值。