You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

KivyMD ScreenManager多屏幕背景色异常:深色背景变白无法修改

问题:ScreenManager中设置的屏幕背景颜色不生效,显示为纯白色

尝试在KivyMD应用中通过ScreenManager添加多个屏幕,但启动后设置的深色背景(如md_bg_color: [22/255, 24/255, 28/255, 1])变为纯白色,无法修改。

原Python代码

from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager
from kivymd.uix.screen import Screen
from kivy.core.window import Window
from kivymd.uix.dialog import MDDialog
from kivymd.uix.label import MDLabel, MDIcon
from kivymd.uix.button import MDFlatButton, MDRectangleFlatButton
from kivymd.uix.textfield import MDTextField
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.screenmanager import ScreenManager


class FirstWindow(Screen):
    pass

class SecondiWindow(Screen):
    pass

class WindowManager(ScreenManager):
    pass

class MyApp(MDApp):
    def build(self):
        self.theme_cls.primary_palette = "Orange"
        self.theme_cls.primary_hue = 'A700'
        return
    

if __name__ == '__main__':
    MyApp().run()

原Kivy代码

WindowManager:
    FirstWindow:
    SecondiWindow:

<SecondiWindow>:
    name: 'second'  
    md_bg_color: [22/255, 24/255, 28/255, 1]           
    MDBottomNavigation:
        MDBottomNavigationItem:
            name: "screen-1"
            text: 'chat'
            icon: 'chat'
            MDLabel: 
                text:"JAHF"
                halign: 'center'
        MDBottomNavigationItem:
            name: "screen-2"
            text: 'chadsdsadt'
            icon: "coffee"
            MDLabel: 
                text:"JAHFdsdsdds"
                halign: 'center'
 
<FirstWindow>:  
    name: "first"
    md_bg_color: [24/255, 24/255, 28/255, 1]
    MDCard:    
        size_hint: None,None
        size: 400,500
        pos_hint: {'center_x': 0.5,'center_y': 0.5}
        elevation: 3
        md_bg_color: [30/255, 30/255, 32/255, 1]
        orientation: 'vertical'
        padding: (0,0,0,20)

        RelativeLayout:

            MDLabel:
                id: logo
                text: "TIKTOK+"
                text_size: self.size
                color: [205/255, 75/255, 8/255, 1]
                font_size: 30
                family_name: 'Helvetica'
                bold: True
                markup: True
                valign: 'top'
                halign: 'center'
                padding: (0,20,0,0)
                        
            MDTextField:
                id: user
                hint_text: 'Enter Username'
                helper_text: "or click on forgotten username or password"
                helper_text_mode: "on_focus"
                helper_text_color: 1,1,1,1
                hint_text_color_normal: 1,1,1,1
                line_color_normal: 1,1,1,1
                icon_right_color_normal: 1,1,1,1
                icon_right: "account"
                size_hint_x: None
                width: 300
                font_size: 18
                pos_hint: {'center_x': 0.5, 'center_y': 0.7}
                        
                        
            MDTextField:
                id: password
                hint_text_color_normal: 1,1,1,1
                line_color_normal: 1,1,1,1
                icon_right_color_normal: 1,1,1,1
                hint_text: 'Enter Username'
                icon_right: "eye-off"
                size_hint_x: None
                width: 300
                font_size: 18
                pos_hint: {'center_x': 0.5, 'center_y': 0.55}
                password: True

            MDRectangleFlatButton:
                id: login
                text: 'Login'
                pos_hint: {'center_x': 0.7, 'center_y': 0.4}
                size_hint: 0.3, None

            MDRectangleFlatButton:
                id: register
                text: 'Registrati'
                pos_hint: {'center_x': 0.3, 'center_y': 0.4}
                on_release: app.root.current = "second"
                
            MDLabel:
                text: "------------------   or   ------------------"
                text_size: self.size
                color: [1,1,1,1]
                font_size: 15
                family_name: 'Helvetica'
                pos_hint: {'center_x': 0.5, 'center_y': 0.78}
                    
                halign: 'center'

            MDRoundFlatIconButton:
                id: google
                icon: "google"
                text: 'Continua con Google'
                icon_color: "white"
                text_color: "white"
                icon_right: "eye-off"
                pos_hint: {'center_x': 0.5, 'center_y': 0.2}
                size_hint: 0.7, None

解决方法

核心问题分析

  1. build方法未返回根组件:原代码中MyApp.build()没有返回WindowManager实例,导致Kivy无法加载kv中定义的界面,默认使用空白白色背景。
  2. 未加载kv代码:代码中没有调用Builder.load_string()或Builder.load_file()加载kv界面定义,导致所有布局和样式设置不生效。
  3. 类名拼写不匹配:Python中定义的SecondiWindow与kv中的<SecondiWindow>虽然拼写一致,但属于笔误(应为SecondWindow),统一命名更规范。

修改后的完整代码

from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager
from kivymd.uix.screen import Screen
from kivy.lang import Builder


class FirstWindow(Screen):
    pass

class SecondWindow(Screen):
    pass

class WindowManager(ScreenManager):
    pass

# 加载kv界面代码
Builder.load_string("""
WindowManager:
    FirstWindow:
    SecondWindow:

<SecondWindow>:
    name: 'second'  
    md_bg_color: [22/255, 24/255, 28/255, 1]           
    MDBottomNavigation:
        MDBottomNavigationItem:
            name: "screen-1"
            text: 'chat'
            icon: 'chat'
            MDLabel: 
                text:"JAHF"
                halign: 'center'
        MDBottomNavigationItem:
            name: "screen-2"
            text: 'chadsdsadt'
            icon: "coffee"
            MDLabel: 
                text:"JAHFdsdsdds"
                halign: 'center'
 
<FirstWindow>:  
    name: "first"
    md_bg_color: [24/255, 24/255, 28/255, 1]
    MDCard:    
        size_hint: None,None
        size: 400,500
        pos_hint: {'center_x': 0.5,'center_y': 0.5}
        elevation: 3
        md_bg_color: [30/255, 30/255, 32/255, 1]
        orientation: 'vertical'
        padding: (0,0,0,20)

        RelativeLayout:

            MDLabel:
                id: logo
                text: "TIKTOK+"
                text_size: self.size
                color: [205/255, 75/255, 8/255, 1]
                font_size: 30
                family_name: 'Helvetica'
                bold: True
                markup: True
                valign: 'top'
                halign: 'center'
                padding: (0,20,0,0)
                        
            MDTextField:
                id: user
                hint_text: 'Enter Username'
                helper_text: "or click on forgotten username or password"
                helper_text_mode: "on_focus"
                helper_text_color: 1,1,1,1
                hint_text_color_normal: 1,1,1,1
                line_color_normal: 1,1,1,1
                icon_right_color_normal: 1,1,1,1
                icon_right: "account"
                size_hint_x: None
                width: 300
                font_size: 18
                pos_hint: {'center_x': 0.5, 'center_y': 0.7}
                        
                        
            MDTextField:
                id: password
                hint_text_color_normal: 1,1,1,1
                line_color_normal: 1,1,1,1
                icon_right_color_normal: 1,1,1,1
                hint_text: 'Enter Password'
                icon_right: "eye-off"
                size_hint_x: None
                width: 300
                font_size: 18
                pos_hint: {'center_x': 0.5, 'center_y': 0.55}
                password: True

            MDRectangleFlatButton:
                id: login
                text: 'Login'
                pos_hint: {'center_x': 0.7, 'center_y': 0.4}
                size_hint: 0.3, None

            MDRectangleFlatButton:
                id: register
                text: 'Registrati'
                pos_hint: {'center_x': 0.3, 'center_y': 0.4}
                on_release: app.root.current = "second"
                
            MDLabel:
                text: "------------------   or   ------------------"
                text_size: self.size
                color: [1,1,1,1]
                font_size: 15
                family_name: 'Helvetica'
                pos_hint: {'center_x': 0.5, 'center_y': 0.78}
                    
                halign: 'center'

            MDRoundFlatIconButton:
                id: google
                icon: "google"
                text: 'Continua con Google'
                icon_color: "white"
                text_color: "white"
                pos_hint: {'center_x': 0.5, 'center_y': 0.2}
                size_hint: 0.7, None
""")

class MyApp(MDApp):
    def build(self):
        self.theme_cls.primary_palette = "Orange"
        self.theme_cls.primary_hue = 'A700'
        return WindowManager()
    

if __name__ == '__main__':
    MyApp().run()

额外修正细节

  • 修正了密码输入框的hint_text错误(从"Enter Username"改为"Enter Password")
  • 移除了MDRoundFlatIconButton上多余的icon_right: "eye-off"属性
  • 统一了类名拼写,避免后续维护混淆

内容的提问来源于stack exchange,提问作者WhoamI

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 20:14:54