Abeer Joshi
Abeer Joshi

Reputation: 11

How do I create a button, and assign a function to it? Python, Kivy

I am a beginner in Python and Kivy. I want to create a simple program that should have an exit button on a window that must exit the app when pressed. Hi there, please help me. It's a request, please keep the code simple for a beginner. ^_^

Upvotes: 0

Views: 198

Answers (1)

mohammad.alqashqish
mohammad.alqashqish

Reputation: 113

Okay, I don't know how your code is, but this code could be copy-pasted as is and it will work (just import the necessary modules from kivy though):

.py

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

class yourscreen(FloatLayout):
    def __init__(self, **kwargs):
        #NEEDED
        super(yourscreen, self).__init__(**kwargs)
        self.button = Button(
            text='exit',
            size_hint=(0.5, 0.2)
        )

        self.button.bind(on_release= lambda x:self.exit())

        self.add_widget(self.button)

    def exit(self):
        return MyApp().stop()

class MyApp(App):
    def build(self):
        return yourscreen()

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

You could pull out what you need which is MyApp().stop()

Upvotes: 1

Related Questions