Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a function once the form is loaded Kivy

Tags:

python

kivy

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()
like image 342
chromebookbob Avatar asked Jul 09 '14 21:07

chromebookbob


People also ask

How do I run a program on Kivy?

Create an application¶ Creating a kivy application is as simple as: sub-classing the App class. implementing its build() method so it returns a Widget instance (the root of your widget tree) instantiating this class, and calling its run() method.

What is build function in Kivy?

Kivy apps have a default build() method, which you can see here; it just returns an empty widget. Generally kivy has two methods to create the root widget tree, either through overriding build() or by defining a root widget in a kv file. For more information see the documentation on creating an application.

How many ways can the .KV file be loaded into code or application?

Loading KV Files There are two methods to load a KV file to a Kivy applicatoin. By Name Matching During application start, Kivy looks for a KV file that matches the application class name.

What is Kivy with respect to Python?

Kivy is a free and open source Python framework for developing mobile apps and other multitouch application software with a natural user interface (NUI). It is distributed under the terms of the MIT License, and can run on Android, iOS, Linux, macOS, and Windows.


2 Answers

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()
like image 181
Enteleform Avatar answered Sep 30 '22 00:09

Enteleform


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.

like image 26
inclement Avatar answered Sep 29 '22 22:09

inclement