Can I access a widget attribute from a function that's outside of the base class

2,196 views
Skip to first unread message

Tyro

unread,
Jun 2, 2017, 10:57:31 AM6/2/17
to Kivy users support
I'm not sure if I'm using the right terminology, but I'm trying to figure out if it's possible to have a function outside of the main (base?) class access widget attributes.  I think the code and kv file below show what I'm asking (better than I'm asking).  Thanks for any help.

Python file:

import kivy

kivy.require('1.9.0')

from kivy.app import App

from kivy.uix.boxlayout import BoxLayout

 

class AppLayout(BoxLayout):

    def process_button(self):

        self.labl.text = 'hello'

 

class AttemptApp(App):

    def build(self):

        return AppLayout()

 

def SomeFunction():

    #How do I (or can I) reference the labl widget in this function?:

    #print (labl.text)

 

myApp = AttemptApp()

myApp.run()


kv file:

 

<AppLayout>:

    id: attempt

    labl: labl

 

    BoxLayout:

        Label:

            id: labl

            font_size: 40

        Button:

            text: 'print hello'

            on_press: attempt.process_button()

            font_size: 40

Andreas Ecker

unread,
Jun 3, 2017, 9:20:20 AM6/3/17
to kivy-...@googlegroups.com
Getting a reference of the Kivy.App instance you could use the static method `App.get_running_app()` of the kivy App class.

Another static method is provided by the kivy.Window class (WindowBase) for to determine the root widget (AppLayout) of your app: `Window.get_parent_window()`

Alternatively (but considered as bad style), you could always use a global variable to store a reference to your labl widget.

FYI: the attempt id that is used in Button.on_press of your kv file can be replaced with root (resulting in the line: `on_press: root.process_button()`.

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tyro

unread,
Jun 3, 2017, 7:22:32 PM6/3/17
to Kivy users support
Thank you, Andreas.
Could I ask you to be a bit more explicit about how I would use any of these options--how would I use them to see or change labl.text from within SomeFunction? 

Andreas Ecker

unread,
Jun 6, 2017, 11:51:07 AM6/6/17
to kivy-...@googlegroups.com
For your request I had to slightly change your example code:
- added needed imports Window and Builder (the latter for to put your kv file into your Python file).
- removed unneeded ids: AppLayout.labl and .attempt.
- added a call of your SomeFunction() function in your button press method process_button() (for to see the text value and changes on it at the console window).
- added ids property into your process_button() callback method.
- added print() calls to show the actual and changed text property values on the console.

And here is the code that demonstrates how to access and change the text property of your Label:

```

import kivy
kivy.require('1.9.0')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.core.window import Window

Builder.load_string('''
<AppLayout>:

    BoxLayout:
        Label:
            id: labl
            font_size: 40
        Button:
            text: 'print hello'
            on_press: root.process_button()
            font_size: 40
''')


class AppLayout(BoxLayout):
    def process_button(self):
        self.ids.labl.text = 'hello'
        SomeFunction()



class AttemptApp(App):
    def build(self):
        return AppLayout()


def SomeFunction():
    # using Window.get_parent_window()
    win_ref = Window.get_parent_window().children[0]
    print(win_ref, win_ref.ids.labl.text)
    # .. change labl.text
    win_ref.ids.labl.text = 'changed via Window ref'

    # using App.get_running_app()
    app_ref = App.get_running_app()
    print(app_ref.root, app_ref.root.ids.labl.text)
    # .. change labl.text
    win_ref.ids.labl.text = 'changed via App ref'

    # display last change at console
    print(app_ref.root.ids.labl.text)


myApp = AttemptApp()
myApp.run()

```

HTH
Message has been deleted

Tyro

unread,
Jun 7, 2017, 11:14:54 AM6/7/17
to Kivy users support
I'm very grateful, Andreas.  Now I can proceed to adapt my program for Kivy and then, I hope, generate an APK.  (I hope you'll have a look periodically, because, inevitably, I'll get stuck again.)
Reply all
Reply to author
Forward
0 new messages