12

I have a kivy program in python which has a textbox and a few buttons. I have written the ui in kivy language and I need to run a function which updates the text box and waits for the user to press a button. Is there an on_load property I can use or some sort of thing to run this function when all the widgets have been loaded. The .kv file:

<MainGui>:
   id: layout1
   orientation: 'horizontal'
   #I would like for some sort of event like below to run my function:
   on_load: root.myfunction()
chromebookbob
  • 193
  • 1
  • 3
  • 8

3 Answers3

15
from kivy.app           import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label     import Label

class DemoApp(App):

    def build(self):
        self.layout = layout = BoxLayout()
        layout.label = Label(text="INITIAL TEXT")
        layout.add_widget(layout.label)
        return(self.layout)

    def on_start(self, **kwargs):
        self.layout.label.text = "APP LOADED"

DemoApp().run()
Enteleform
  • 3,753
  • 1
  • 15
  • 30
6

I'm not sure what exactly you mean by 'loaded' (that is, what aspect of the widget creation is important to your function), but perhaps you could use on_parent which occurs when the widget is added to another widget.

inclement
  • 29,124
  • 4
  • 48
  • 60
  • what I mean by loaded is the window shows up on the screen with all the widgets, then I want it to run some code. It seems that 'on_parent' is boolean, so I can't run a function off it, thanks for the suggestion – chromebookbob Jul 10 '14 at 08:31
  • 2
    Why can't you run a function when a boolean changes? – inclement Jul 10 '14 at 09:52
  • because I'd have to use a while loop to check if it's true which would prevent the program from running until it was true. The widgets wouldnt load. – chromebookbob Jul 10 '14 at 11:34
  • No, you should use kivy's event system to do stuff when the boolean changes, not reinvent that wheel with your own loop. As I said, you could bind to the `on_parent` event, or put stuff in the `on_parent` method. – inclement Jul 10 '14 at 13:30
  • 2
    The `on_parent` event seems to fire twice for a widget that's sitting in the `.kv` file. I share @chromebookbob's desire to start with blank labels and buttons in a `.kv` file that I fill programmatically right at the beginning. This is not particularly obvious. – John Baber-Lucero Sep 17 '15 at 02:34
  • @JohnBaber-Lucero I've added a print function on every widgetd defined in the `.kv` file and didn't notice twice prints. Snippet? – m3nda Jul 19 '16 at 13:44
  • @JohnBaber-Lucero for blank labels you can just create a custom `value: ""` then set `text: self.value`, you have have dinamyc names, so you can also set them from a config/saved values. – m3nda Jul 19 '16 at 13:45
-1
from kivy.app import App
from kivy.uix.label import Label
from kivy.clock import Clock
import time


class Time (Label):
    def update (self, *args):
       self.text = time.strftime('%H:%M:%S')


class TimeApp (App):
    def build(self):
        time = Time()
        Clock.schedule_interval(time.update, 1) #replace here
        return time
if __name__ == '__main__':
    app = TimeApp()
    app.run()

update func in Time Class loaded when the app will begin you can define any functions or classes and replace it, you can add more class and functions too but remember that build return one object so manage your program in one object finally