Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zichkoding/how-to-create-and-color-a-button-in-a-.py-file-with-kivy
If you need to know how to create and color a button with Kivy in your Python code this is the repository to look for.
https://github.com/zichkoding/how-to-create-and-color-a-button-in-a-.py-file-with-kivy
button button-widget front-end-development kivy kivy-widget python python3
Last synced: about 1 month ago
JSON representation
If you need to know how to create and color a button with Kivy in your Python code this is the repository to look for.
- Host: GitHub
- URL: https://github.com/zichkoding/how-to-create-and-color-a-button-in-a-.py-file-with-kivy
- Owner: ZichKoding
- Created: 2021-07-08T14:28:07.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-07-08T15:00:31.000Z (over 3 years ago)
- Last Synced: 2024-12-18T19:09:27.388Z (about 1 month ago)
- Topics: button, button-widget, front-end-development, kivy, kivy-widget, python, python3
- Language: Python
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# **How to create and color a button in a .py file with Kivy**
Today we will make a button with Kivy and learn how to change its color. Before we start, remember to use the virtual environment to play with the Kivy module to prevent any mishaps with other modules.
Alright, it's time to get down to some coding. First, we will start off with making a simple button.
Input:
```python
import kivy
from kivy.app import App
from kivy.uix.button import Button
class ButtonApp(App)
def build(self):
# Make a variable for the Button Class being called from Kivy
button = Button(text="Hello World!")
return button
if __name__ == '__main__':
ButtonApp().run()
```Output:
![image of basic button](https://github.com/ZichKoding/How-to-create-and-color-a-button-in-a-.py-file-with-Kivy/blob/main/HelloWorldButtonpyno-color.png)
This is a button that does nothing once it's pressed, but it's okay because we are more worried about the design parts of this app than the logic. The next part is going to show how to change the color. So, what we need to do is in our variable, button, we need to go into the `Button()` method and add the keyword argument `background_color`.
It is pretty simple to make sense out of this color system. This color system is based on RGB values, so to set the color to blue would be `background_color = (0, 0, 1, 1)`. To understand what is going on, the tuple stands for (Red, Green, Blue, Opacity) and the 0's stand for 0% (or 0/255) while the 1's stand for 100% (or 255/255). The last value in the tuple (1) stands for opacity, which is also percentage-based.
Input:
```python
import kivy
from kivy.app import App
from kivy.uix.button import Button
class ButtonApp(App):
def build(self):
# Make a variable for the Button class being called from Kivy
button = Button(text="Hello World!",
background_color=(0,0,1,1)
)
return button
if __name__ == '__main__':
ButtonApp().run()
```
Output:![image of blue button](https://github.com/ZichKoding/How-to-create-and-color-a-button-in-a-.py-file-with-Kivy/blob/main/Blue-Button.png)
Alright, we have a blue button! If you wanted a different shade of blue, you can always refer to an HTML color picker off of Google and divide each RGB value by 255. For instance, if we wanted a pink button the code would be `background_color=(255/255, 19/255, 230/255, 1)`.
Another way of creating and coloring a button using the Kivy design language is by making a .kv file, but I will cover that in a different shot. For now, this will be enough for creating and changing the color of a button in a .py file with Kivy.