[파이썬] Kivy `Button` 위젯

In this blog post, we will explore the Kivy Button widget and how to use it in Python to create interactive user interfaces. The Button widget is one of the most commonly used widgets in Kivy for capturing user input.

What is Kivy?

Kivy is an open-source Python library used for developing multi-touch applications. It is known for its simplicity and ease of use, making it a popular choice for both beginners and experienced developers. With its powerful set of tools and widgets, Kivy allows you to create cross-platform applications that run on Windows, macOS, Linux, Android, and iOS.

The Button widget

The Button widget is used to create clickable buttons in Kivy. It can display text, images, or both, and can be customized in terms of size, color, and style. When a Button is clicked, it triggers an event that can be handled to perform a specific action.

Creating a simple Button in Kivy

To create a Button widget in Kivy, we first need to import the necessary modules:

from kivy.app import App
from kivy.uix.button import Button

Next, we create a class that inherits from App, which is the base class for Kivy applications:

class MyApp(App):
    def build(self):
        button = Button(text="Click me!")
        return button

In the build() method, we create an instance of the Button widget and set its text property to “Click me!”. Finally, we return the Button instance, which will be the root widget of our application.

To run the application, we instantiate the MyApp class and call its run() method:

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

When the button is clicked, nothing will happen by default. In the next section, we will see how to handle button events.

Handling button events

To perform an action when a Button is clicked, we need to define a callback function and bind it to the on_press or on_release event of the button.

def on_button_press(instance):
    print("Button pressed!")

button.bind(on_press=on_button_press)

In this example, we define a function called on_button_press that takes an instance argument. When the button is pressed, this function will be called and the message “Button pressed!” will be printed to the console.

By binding the on_press event of the button to our callback function, we ensure that the function is executed whenever the button is pressed.

Customizing the Button

The Button widget provides various properties and methods that allow you to customize its appearance. You can change the button’s size, color, font, and add background images or icons.

Here are some examples of customizing the Button:

button = Button(
    text="Click me!",
    size_hint=(0.5, 0.2),
    background_color=(0.2, 0.4, 0.6, 1),
    font_size=20,
    background_normal='button_normal.png',
    background_down='button_pressed.png'
)

In the above example, we set the size_hint property to control the size of the button relative to its parent widget. We also change the background_color to a custom RGB value, set the font_size to 20, and provide background images for the normal and pressed states of the button.

Conclusion

The Button widget is a fundamental element in Kivy for capturing user input and creating interactive interfaces. In this blog post, we have explored how to create a simple Button, handle button events, and customize its appearance.

Kivy provides a wide range of widgets and features that make it easy to develop cross-platform applications with rich user interfaces. Give it a try and start building your own interactive applications today!

Happy coding!