kivy: change background color to white

前端 未结 2 911
一生所求
一生所求 2020-12-11 00:31

I\'d like to have an app with black buttons and labels, and with white text, and thus, would like to have white space separating these widgets. I suppose that in order to do

相关标签:
2条回答
  • 2020-12-11 01:22

    A simple way is to simply draw a big white rectangle behind your root widget. For instance, in kivy language you could do

    <YourRootWidget>:
        canvas.before:
            Color:
                rgba: 1, 1, 1, 1
            Rectangle:
                pos: self.pos
                size: self.size
    

    I think you can also actually directly set the colour that kivy clears the window background with, which is exposed as Window.clearcolor. You would do this with

    from kivy.core.window import Window
    Window.clearcolor = (1, 1, 1, 1)
    

    You would probably need to put this before anything else in your app, as it won't affect anything if run after the window has been created.

    0 讨论(0)
  • 2020-12-11 01:27

    I have created a module for this purpose. Please check: Details on Github

    #Change background color of a kivy layout
    #Place the CustomGraphics.py file to a folder
    #code starts here
    
    import sys
    sys.path.append([path to CustomGraphics.py])
    from CustomModules import CustomGraphics
    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.label import Label
    
    class TestApp(App):
        def build(self):
            layout = BoxLayout(orientation='vertical', size=(Window.width, Window.height))
            label = Label(text="Remember my name: It's Smruti Ranjan Gochhayat")
            layout.add_widget(label)
            CustomGraphics.SetBG(layout, bg_color=[1,0,0,1])
            return layout
    if __name__ == '__main__':
        TestApp().run()
        
    #code ends here
    

    I wish it is helpful for some people

    0 讨论(0)
提交回复
热议问题