admin管理员组文章数量:1122832
I have tried this in both the kvfile and in the python code. I am using themes and try to change the backgroundcolor to either white or black depending on theme style dark or light.
In the kvfile it messes up all of the themecolors and in the python code nothing happens at all.
I have built a small app which allows you to set a theme and dark or light. A button leads you to a new screen where the topappbar is. You hav to install kivymd 2.0.1(dev0) to be able to run the app.
pip install .zip)
palette.kv
WindowManager:
MainScreen
NextScreen
<MainScreen>
name: "mainscreen"
theme: current_theme
md_bg_color: app.theme_cls.backgroundColor
MDFloatLayout:
MDTextField:
id: current_theme
disabled: True
multiline: False
size_hint_x: .4
on_text: root.current_theme = self.text
pos_hint: {"x": 0.1, "center_y": 0.8}
MDTextFieldHintText:
text: "Current Theme"
MDTextField:
id: counter_field
disabled: True
multiline: False
size_hint_x: .1
on_text: root.counter = self.text
pos_hint: {"x": 0.1, "center_y": 0.7}
MDTextFieldHintText:
text: "Counter"
MDLabel:
pos_hint: {'x': .6, 'center_y': 0.9}
font_style: "Label"
text: "Dark or light"
MDSwitch:
id: theme_switch
pos_hint: {'center_x': .6, 'center_y': .8}
on_active: root.toggle_theme(self.active)
# 'self.active' returnerar True om switchen är aktiverad, annars False
MDButton:
style: "outlined"
pos_hint: {"center_x": 0.4, "center_y": 0.15}
size_hint: 0.4, 0.1
on_press:
root.goto_back()
MDButtonText:
text: "Back"
MDButton:
style: "outlined"
pos_hint: {"center_x": 0.6, "center_y": 0.15} # Adjust y position to be below the ScrollView
size_hint: 0.4, 0.1
on_press:
root.goto_forward()
MDButtonText:
text: "Forward"
MDButton:
style: "outlined"
pos_hint: {"center_x": 0.8, "center_y": 0.15} # Adjust y position to be below the ScrollView
size_hint: 0.4, 0.1
on_press:
root.goto_next_screen()
MDButtonText:
text: "Next Window"
<NextScreen>
name: "nextscreen"
md_bg_color: app.theme_cls.backgroundColor
MDTopAppBar:
id: topappbar
type: "small"
size_hint_x: .8
pos_hint: {"center_x": .5, "center_y": 0.9}
MDTopAppBarLeadingButtonContainer:
MDActionTopAppBarButton:
icon: "arrow-left"
on_press: root.goto_main_screen()
MDTopAppBarTitle:
text: "TopAppBar"
pos_hint: {"center_x": .5}
from kivymd.app import MDApp
from kivymd.uix.screen import MDScreen
from kivymd.uix.screenmanager import MDScreenManager
from kivy.properties import StringProperty
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.properties import NumericProperty
palette = [
'Aliceblue', 'Antiquewhite', 'Aqua', 'Aquamarine', 'Azure', 'Beige', 'Bisque', 'Black', 'Blanchedalmond',
'Blue', 'Blueviolet', 'Brown', 'Burlywood', 'Cadetblue', 'Chartreuse', 'Chocolate', 'Coral',
'Cornflowerblue', 'Cornsilk', 'Crimson', 'Cyan', 'Darkblue', 'Darkcyan', 'Darkgoldenrod', 'Darkgray',
'Darkgrey', 'Darkgreen', 'Darkkhaki', 'Darkmagenta', 'Darkolivegreen', 'Darkorange', 'Darkorchid',
'Darkred', 'Darksalmon', 'Darkseagreen', 'Darkslateblue', 'Darkslategray', 'Darkslategrey',
'Darkturquoise', 'Darkviolet', 'Deeppink', 'Deepskyblue', 'Dimgray', 'Dimgrey', 'Dodgerblue',
'Firebrick', 'Floralwhite', 'Forestgreen', 'Fuchsia', 'Gainsboro', 'Ghostwhite', 'Gold', 'Goldenrod',
'Gray', 'Grey', 'Green', 'Greenyellow', 'Honeydew', 'Hotpink', 'Indianred', 'Indigo', 'Ivory', 'Khaki',
'Lavender', 'Lavenderblush', 'Lawngreen', 'Lemonchiffon', 'Lightblue', 'Lightcoral', 'Lightcyan',
'Lightgoldenrodyellow', 'Lightgreen', 'Lightgray', 'Lightgrey', 'Lightpink', 'Lightsalmon',
'Lightseagreen', 'Lightskyblue', 'Lightslategray', 'Lightslategrey', 'Lightsteelblue', 'Lightyellow',
'Lime', 'Limegreen', 'Linen', 'Magenta', 'Maroon', 'Mediumaquamarine', 'Mediumblue', 'Mediumorchid',
'Mediumpurple', 'Mediumseagreen', 'Mediumslateblue', 'Mediumspringgreen', 'Mediumturquoise',
'Mediumvioletred', 'Midnightblue', 'Mintcream', 'Mistyrose', 'Moccasin', 'Navajowhite', 'Navy',
'Oldlace', 'Olive', 'Olivedrab', 'Orange', 'Orangered', 'Orchid', 'Palegoldenrod', 'Palegreen',
'Paleturquoise', 'Palevioletred', 'Papayawhip', 'Peachpuff', 'Peru', 'Pink', 'Plum', 'Powderblue',
'Purple', 'Red', 'Rosybrown', 'Royalblue', 'Saddlebrown', 'Salmon', 'Sandybrown', 'Seagreen',
'Seashell', 'Sienna', 'Silver', 'Skyblue', 'Slateblue', 'Slategray', 'Slategrey', 'Snow',
'Springgreen', 'Steelblue', 'Tan', 'Teal', 'Thistle', 'Tomato', 'Turquoise', 'Violet', 'Wheat',
'White', 'Whitesmoke', 'Yellow', 'Yellowgreen'
]
class MainScreen(MDScreen):
current_theme = StringProperty("")
counter = NumericProperty(0)
darkorlight = "Light"
def on_enter(self, *args):
self.theme_cls.primary_palette = palette[0]
self.current_theme = self.theme_cls.primary_palette
Clock.schedule_once(self.set_ids_current_theme, 0)
Clock.schedule_once(self.set_ids_counter, 0)
#self.set_ids_counter()
def set_ids_current_theme(self, *args):
if 'current_theme' in self.ids:
self.ids.current_theme.text = self.current_theme
else:
print("current_theme ID not found.")
def set_ids_counter(self, *args):
self.ids.counter_field.text = str(self.counter)
def goto_forward(self):
if self.counter < 146:
self.counter = self.counter + 1
self.set_palette()
self.set_ids_current_theme()
self.set_ids_counter()
def goto_back(self):
if self.counter > 0:
self.counter = self.counter - 1
self.set_palette()
self.set_ids_current_theme()
self.set_ids_counter()
def toggle_theme(self, is_active):
Clock.schedule_once(lambda dt: self.apply_theme(is_active), 0.3)
def apply_theme(self, is_active):
# Växlar mellan 'Dark' och 'Light' tema baserat på switchens läge
if is_active:
self.theme_cls.theme_style = "Dark"
else:
self.theme_cls.theme_style = "Light"
def set_palette(self):
self.theme_cls.primary_palette = palette[self.counter]
self.current_theme = self.theme_cls.primary_palette
def goto_next_screen(self):
next_screen = self.manager.get_screen("nextscreen") # Hämta referens till NewSiteScreen
self.manager.current = "nextscreen"
self.manager.transition.direction = "right"
class NextScreen(MDScreen):
def on_pre_enter(self):
print("on start")
# Vänta tills gränssnittet är färdiginitierat innan färger sätts
Clock.schedule_once(self.set_appbar_color, 0.1)
def set_appbar_color(self, *args):
# Get set theme
style = self.theme_cls.theme_style
# Set backgroundcolor for topappbar
if style == "Dark":
self.ids.topappbar.md_bg_color = [1, 1, 1, 1]# White background
self.ids.topappbar.specific_text_color = [0, 0, 0, 1] # Black text
else:
# Style == "Light
self.ids.topappbar.md_bg_color = [0, 0, 0, 1] # Black background
self.ids.topappbar.specific_text_color = [1, 1, 1, 1]# White text
print(
f"Theme Style: {style}, AppBar BG: {self.ids.topappbar.md_bg_color}, Text: {self.ids.topappbar.specific_text_color}")
def goto_main_screen(self):
main_screen = self.manager.get_screen("mainscreen")
self.manager.current = "mainscreen"
self.manager.transition.direction = "right"
class WindowManager(MDScreenManager):
pass
class Palette(MDApp):
def build(self):
Window.borderless = False
if __name__ == "__main__":
Palette().run()
id: topappbar
type: "small"
size_hint_x: .8
pos_hint: {"center_x": .5, "center_y": 0.9}
MDTopAppBarLeadingButtonContainer:
MDActionTopAppBarButton:
icon: "arrow-left"
on_press: root.goto_main_screen()
MDTopAppBarTitle:
text: "TopAppBar"
pos_hint: {"center_x": .5}
本文标签: background colorIn python 312 and kivymd201 change backgroundcolor in the topappbarStack Overflow
版权声明:本文标题:background color - In python 3.12 and kivymd2.0.1 change backgroundcolor in the topappbar - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736309056a1933881.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论