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): … Read More
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()
Edited by vegaseat
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!
Edited by vegaseat
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:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
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()
Hi guys. I am trying to create a program where the user inputs an integer and the program will cout the nth prime number. Unfortunatly when I try to cout ...
Is C similar to C# (C-sharp); or are they completely different ? I was wondering if I should begin learning C# and the transition to C will be a tad ...
Write a program that displays all numbers divisible by 3 and 4 within a range entered by user. Display five numbers per line. Numbers are separated by exactly two spaces. ...