- Kivy: change background color to white
- Kivy: change background color to white
- Two Ways To Change Background Colors
- Change widget background color in Kivy
- How to change the colours of the background of the screen in kivy app module of python?
- Changing background color in kivy [duplicate]
- Two Ways To Change Background Colors – Python Kivy GUI Tutorial #11
- John Elder
Kivy: change background color to white
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. Please check: Details on Github I wish it is helpful for some people Question: As the title suggests, I want to be able to change the background color of a gridlayout widget in Kivy I’m using the following code: I am expecting a blue gridlayout where each column is completing filled by one label.
Kivy: change background color to white
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 so, it would be necessary to convert the background from the default color, which is black, to white. What is the best way of accomplishing this? Thank you!
A simple way is to simply draw a big white rectangle behind your root widget. For instance, in kivy language you could do
: 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.
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
How to add background colour to a Label in Kivy?, Using Kivy, how can you change the background colour of a label in Python and not by using the Kv language? with self.canvas: Color (1., 0, 0) …
Two Ways To Change Background Colors
In this video I’ll show you two different ways to change the background color of your app with Kivy and Python.Changing the background color of your app is a
Change widget background color in Kivy
As the title suggests, I want to be able to change the background color of a gridlayout widget in Kivy
I’m using the following code:
from kivy.utils import get_color_from_hex from kivy.graphics import Color, Rectangle from kivy.lang import Builder from kivy.base import runTouchApp from kivy.uix.gridlayout import GridLayout Builder.load_string(''' #:import utils kivy.utils : GridLayout: canvas.before: Color: rgb: utils.get_color_from_hex("#39B3F2") Rectangle: size: self.size pos: self.pos rows: 1 cols: 2 pos_hint: size_hint: 1, 1 Label: text: "First Label" id: Label_1 Label: text: "Second Label" id: Label_2 ''') class BlueGrid(GridLayout): pass runTouchApp(BlueGrid())
I am expecting a blue gridlayout where each column is completing filled by one label. Instead, I get the output below, where everything is concentrated in the bottom left corner: output gridlayout
I have also tried running the «BlueGrid» class without the runTouchapp, with the same result:
class MyApp(App): def build(self): return BlueGrid() if __name__ == "__main__": MyApp().run() print(kivy.__file__)
What am I missing? Why are the two labels and the Rectangle in the Gridlayout all positioned on top of each other?
The reason for this, is that the default size of the GridLayout is not enough to display both of the labels.
Also the root layout of your app might be better to be a FloatLayout or a BoxLayout , so it can occupy all of the window.
So one solution would be to set class BlueGrid(FloatLayout):
Another solution would be to keep it as a GridLayout but set its child GridLayout to a bigger size like this:
: GridLayout: size_hint: None, 1 width: 300
Did you notice the warning:
[WARNING] have no cols or rows set, layout is not triggered.
That is because you have defined BlueGrid as an extension of GridLayout , but did not specify cols or rows in your kv for BlueGrid . You can correct that by just adding a cols specification:
But because BlueGrid is a GridLayout , do you really want another GridLayout inside it? Perhaps a better solution is:
#:import utils kivy.utils : canvas.before: Color: rgb: utils.get_color_from_hex("#39B3F2") Rectangle: size: self.size pos: self.pos rows: 1 cols: 2 pos_hint: size_hint: 1, 1 Label: text: "First Label" id: Label_1 Label: text: "Second Label" id: Label_2
Kivy Tabbed Panel won’t change background color, TabbedPanel: do_default_tab: False background_color:1,1,1,1 TabbedPanelItem: text: ‘Main’ Related: I am aware others have struggled with …
How to change the colours of the background of the screen in kivy app module of python?
I have a developed kivy app in python but I have an issue: Sometimes the black background is too dark and I wonder if it’s possible that in Kivy the attribute Screen has a property that could change to negative colours, I mean, like some mobile phones that you can choose to change all the colours of the mobile to negative, solving the problem of the colours. I’m new about developing with Kivy, so I don’t know about the potentiality of Kivy about solving this problem. I’ve been looking for a few days and I haven’t found out anything. If someone knows how to fix this incovenience it would be very helpful.
You can set background color to a certain value globally by using Window.clearcolor :
from kivy.core.window import Window Window.clearcolor = (.9, .9, .9, 1)
You can also set it manually per screen (or pretty much any widget) by drawing on its canvas.
: canvas.before: Color: rgba: 1, 1, 1, 1 Rectangle: pos: self.pos size: self.size
Or change Window.clearcolor each time you enter a screen that needs different background, preferably overwriting on_pre_enter method, and setting it back in on_leave .
How to set backgroud color to BoxLayout in kivy?, In kivy you are reasonably free to create your own widget graphical representation. For that you have to create a subclass inheriting from BoxLayout …
Changing background color in kivy [duplicate]
I would like to change the background (black color) to a different color in kivy. But the Color specification in kv file is not recognized.
from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.lang import Builder class MatrixCalcLayout(BoxLayout): def calculations(self): pass class ConfusionMatrixCalcApp(App): pass if __name__ == '__main__': ConfusionMatrixCalcApp().run()
MatrixCalcLayout: : canvas: Color: rgba: 0.5, 0.5, 0.5, 0.5 orientation: 'vertical' BoxLayout: Label: BoxLayout: Button: BoxLayout: Button: BoxLayout: Button:
After Color you need to draw something, in your case, a Rectangle
canvas: Color: rgba: 0.5, 0.5, 0.5, 0.5 Rectangle: #woohoo. size: self.size pos: self.pos
How to set Kivy TextInput background color depending, Kivy has the properties background_active and background_normal for setting a TextInput widget’s background when it is in focus and when it is not …
Two Ways To Change Background Colors – Python Kivy GUI Tutorial #11
In this video I’ll show you two different ways to change the background color of your app with Kivy and Python.
Changing the background color of your app is a pretty fundamental thing in GUI programming and with Kivy it’s pretty easy. I’ll show you how to do it in your Kivy language file using a Canvas and a Rectangle, and I’ll also show you a second way to do it in your actual python file using kivy.core.window
Python Code: bg.py
GitHub Code: bg.py
from kivy.app import App from kivy.uix.widget import Widget from kivy.properties import ObjectProperty from kivy.lang import Builder from kivy.core.window import Window # Designate Our .kv design file Builder.load_file('bg.kv') class MyLayout(Widget): pass class AwesomeApp(App): def build(self): Window.clearcolor = (1,0,0,1) return MyLayout() if __name__ == '__main__': AwesomeApp().run()
Kivy Design Code: bg.kv
GitHub Code: bg.kv
canvas.before: Color: rgba: (0,0,1,1) Rectangle: pos: self.pos size: self.size BoxLayout: orientation: "vertical" size: root.width, root.height padding: 50 spacing: 20 Button: text: "Hello World!" Button: text: "Goodbye World!"
John Elder
John is the CEO of Codemy.com where he teaches over 100,000 students how to code! He founded one of the Internet’s earliest advertising networks and sold it to a publicly company at the height of the first dot com boom. After that he developed the award-winning Submission-Spider search engine submission software that’s been used by over 3 million individuals, businesses, and governments in over 42 countries. He’s written several Amazon #1 best selling books on coding, and runs a popular Youtube coding channel.