Hello!
Can you give me an example of Button widget in Kivy language?
I can creat the button but don't know how to use it's callback. I mean how can i set a command for the Button in Kivy?

I think i should use on_press keyword, but i don't know how to use it exactly. I need a simple example. For example i want when i press the button, a "Hello World" apperas on the label widget. (All in kivy language).

Which tutorial are you using?

Well, i'm trying to learn from every where. If h find any video i watch it, i search the net and try to learn from example codes. I didn't find any special and great tutorial for that.

I checked that link but as i said, it's not clear for me how to use it exactly.
I know i should create a .py file and a .kv file, but i think that link is about only what we can use in .py file only.
I mean some tutorials use kivy in one .py file only and don't teach the standard way of kivy programming wich should use a .kv file beside the .py file and changing the .py file according to that .kv file.

Well, i don't know it was clear or not.
I nead a simple and complete example, a .py file and a .kv file to gather to see how they interact.

This is how I would do it ...

''' kivy_button_label101.py
create a touch sensitive button with Python module kivy
add a label to show action

info:
http://kivy.org/docs/api-kivy.uix.button.html
http://kivy.org/docs/api-kivy.uix.label.html
http://kivy.org/docs/api-kivy.uix.boxlayout.html
'''

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout


class TestApp(App):
    def build(self):
        layout = BoxLayout(orientation='vertical')
        # use a (r, g, b, a) tuple
        blue = (0, 0, 1.5, 2.5)
        red = (2.5, 0, 0, 1.5)

        btn =  Button(text='Touch me!', background_color=blue, font_size=120)        
        btn.bind(on_press=self.callback)
        self.label = Label(text="------------", font_size='50sp')
        layout.add_widget(btn)
        layout.add_widget(self.label)
        return layout

    def callback(self, event):
        print("button touched")  # test
        self.label.text = "button touched"


TestApp().run()

Thank you @vegaseat.

I have some question again:
Look at line 24. self.label = what does the "self" word do here?

And line 29. def callback(self, event): what the "self" keyword is for? I don't know much about the SELF keyword.
And also what does the "event" keyword do here?

Thanks for this simple example, this is one way, we just have one .py file. Can you give me an example of using one .py file and one .kv file beside togather, that they are interacting togather, do you know what i mean?
If i want to create this program in that way with both .kv and .py file togather, i can't do it. The standard form of coding in kivy language is using a .py file and a .kv file togather, right?!

I would be happy if you could give me an example of that way too.
Thanks for this one, it helps.

You need to learn the basics of a Python class!
bind() creates an event that is send to callback()
I don't use .kv files for simple programs, just another special syntax to manage and errors are hard to trace!

As a GUI toolkit Kivy is pretty clumsy. I would use it only if you want to use your touch sensitive screen or for Android applications.

But Tkinter is really ugly so i prefer to use Kivy, it's more beautiful than Tkinter! And also very easy to code!

I know this is an old post, but just wanted to add a variant example. I found this on the Internet somewhere, but don't recall where. What I don't quite understand (yes, I'm a noobie) is this example, like the one above, there is no if name == "main": window.run() *statement. I thought that was required.

Anyway, here is the example:

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
kivy.require('1.10.0')

class MyWindowApp(App):

def __init__(self):
    super(MyWindowApp, self).__init__()

    self.btn = Button(text= "DISARMED")
    self.btn.font_size =150
    self.btn.background_color= (0,255,0,1)
    self.lbl = Label(text='Read Me!')
    self.status = "DISARMED"

def build(self):
    self.btn.bind(on_press=self.clk)
    layout = BoxLayout()
    layout.orientation = 'vertical'
    layout.add_widget(self.lbl)
    layout.add_widget(self.btn)

    return layout

def clk(self, obj):
    print ('I have been clicked')
    self.lbl.text = 'I have been read!'
    if self.status == "ARMED":
        self.status = "DISARMED"
        self.btn.text = "DISARMED"
        self.btn.background_color = (0,255,0,1)
    else:
        self.status = "ARMED"
        self.btn.text = "ARMED"
        self.btn.background_color = (255,0,0,1)

window = MyWindowApp()
window.run()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.