How do I use a variable from one class function in another class function

139 views
Skip to first unread message

Eric B

unread,
Mar 9, 2020, 5:20:15 PM3/9/20
to Kivy users support
The RBox class gives me a reason, and the get_reason function gets called whenever a radio button is pressed so I know there's a reason that can get printed. When I run the pressed function though from a button, the reason doesn't get printed, please help.

class RBox(GridLayout):
def get_reason(self):
reason = self.ids.reason.text
print(reason)


class FormScreen(BoxLayout):
def pressed(self):
        RBox().get_reason() 

Elliot Garbus

unread,
Mar 9, 2020, 5:43:08 PM3/9/20
to kivy-...@googlegroups.com

It is easier to answer these questions if you attach a complete runnable example.

 

The problem below is the RBox().

class FormScreen(BoxLayout):
    def pressed(self):

        RBox().get_reason

 

When you say RBox() you are creating a new RBox object, you want to access the existing RBox object.  You can do this by giving the RBox an id in KV.

 

RBox:

    id: my_rbox

 

Then change FormScreen to something like:

 

class FormScreen(BoxLayout):
    def pressed(self):

        app = App.get_running_app()

        app.root.ids.my_rbox.get_reason() 

--
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+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/4da9c9ed-098a-4068-9995-65bd161901c9%40googlegroups.com.

 

Message has been deleted

Eric B

unread,
Mar 9, 2020, 6:03:56 PM3/9/20
to Kivy users support
My message got deleted?
Heres the runnable code:

class RBox(GridLayout):
def get_reason(self):
reason = self.ids.reason.text
        return reason

class FormScreen(BoxLayout):
def pressed(self):


class MainApp(App):
def build(self):
return FormScreen()


if __name__ == "__main__":
MainApp().run()


<RBox>:
cols: 2
text: ''
group: ''
RCheckBox:
id: cb
group: root.group
on_active: root.get_reason()
RLabel:
on_press: cb._do_press()
text: root.text
width: root.width*0.5
id: reason

Elliot Garbus

unread,
Mar 9, 2020, 7:17:36 PM3/9/20
to kivy-...@googlegroups.com

This code is not complete.  There are no imports.  There are other obvious errors in the code.

 

From: Eric B
Sent: Monday, March 9, 2020 3:04 PM
To: Kivy users support

Subject: [kivy-users] Re: How do I use a variable from one class function inanother class function

My message got deleted?

Heres the runnable code:


class RBox(GridLayout):
   
def get_reason(self):
        reason =
self.ids.reason.text
       
return
reason
This c
class FormScreen(BoxLayout):
   
def pressed(self):

--

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+...@googlegroups.com.

Eric B

unread,
Mar 9, 2020, 7:24:33 PM3/9/20
to Kivy users support
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout



class RBox(GridLayout):
def get_reason(self):
reason = self.ids.reason.text
        return reason


class FormScreen(BoxLayout):
def pressed(self):



class MainApp(App):
def build(self):
return FormScreen()


if __name__ == "__main__":
MainApp().run()
<RBox>:
cols: 2
text: ''
group: ''
    CheckBox:
id: cb
group: root.group
on_active: root.get_reason()
    Label:
on_press: cb._do_press()
text: root.text
width: root.width*0.5
id: reason
<FormScreen>:
    Button:
        text: "Submit"
        on_press: root.pressed()

Elliot Garbus

unread,
Mar 9, 2020, 9:51:59 PM3/9/20
to kivy-...@googlegroups.com

It is hard for me to tell what you are trying to do.  Here is an example I put together for another developer.  I’ve highlighted a few pieces of code.  Note <AnotherClass@BoxLayout> defines the class.

AnotherClass: instances the class. 

Each kivy ‘rule’ has it’s own ids dictionary. see print(f'And another layer down: {app.root.ids.instance_ac.ids}')

 

The id in kv code, at the time kv code is parsed, gets turned into a dictionary of ids,  the dictionary associates the id with the widget.



 

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder

kv =
"""
<AnotherClass@BoxLayout>
    Label:
        id: label
        text: 'Here i am'
    Button:
        id: button
        text: 'I do nothing'


Tab1BoxLayout:
    id: box10
    orientation: 'vertical'

    Label:
        id: label10
        text: 'Second tab content area'
    Button:
        id: button10
        text: 'print ids'
        on_release:
            root.print_from_python()
            print(f'Print from KV: {app.root.ids}')
            print(f'And another layer down: {app.root.ids.instance_ac.ids}')
       

    BoxLayout:
        id: box20
        orientation: 'vertical'

        Label:
            id: label20
            text: 'Label 2'
        AnotherClass:
            id: instance_ac

"""


class Tab1BoxLayout(BoxLayout):
   
def print_from_python(self):
        app = App.get_running_app()
       
print(f'ids from Python {app.root.ids}')
       
print(f'This is the same, because self is the root widget: {self.ids}')
       
print(f'For this case, the app.root.ids dictionary and the self.ids dictionary is the same: {app.root.ids is self.ids}')


class AppRootProblemApp(App):
   
def build(self):
       
return Builder.load_string(kv)


if __name__ == '__main__':
    AppRootProblemApp().run()

 

 

 

 

From: Eric B
Sent: Monday, March 9, 2020 4:24 PM
To: Kivy users support
Subject: [kivy-users] Re: How do I use a variable from one class function inanother class function

 

from kivy.app import App

--

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+...@googlegroups.com.

Elliot Garbus

unread,
Mar 10, 2020, 3:53:17 PM3/10/20
to kivy-...@googlegroups.com

I made a few modifications.

I instanced the root widget FormScreen in kv.  I put 2 instances of RBox under FormScreen.

RLabel, RCheckBox, were not defined so I replaced them.

Added print messages to illustrate what is going on.

 

 

 

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder

kv =
"""

<RBox>:
    cols: 2
    text: ''
    group: ''
    # RCheckBox:  # Not Defined

    CheckBox:
        id: cb
        group: root.group
        on_active: root.get_reason()
    # RLabel:  # Not Defined
    Button:                   # Label does not have an on_press, changed to button
        id: reason
        on_press:           
            cb._do_press()
            app.root.pressed()  # added call to pressed()
        text: root.text
       

FormScreen: # Root widget
    RBox:
        text: 'Test RBox 1'
        group: 'test'
    RBox:
        id:my_rbox
        text: 'Text RBox 2'
        group: 'test'
"""



class RBox(GridLayout):
   
def get_reason(self):
        reason =
self
.ids.reason.text
       
print(f'get reason called, reason = {reason}')
       
return reason


class FormScreen(BoxLayout):
   
def pressed(self):
       
print('pressed called')
        app = App.get_running_app()
        app.root.ids.my_rbox.get_reason()
       
print(f'app.root.ids = {app.root.ids}')
       
print(f'app.root.ids.my_rbox.ids = {app.root.ids.my_rbox.ids}')


class Main_1App(App):
   
def build(self):
        
return Builder.load_string(kv)

if __name__ == "__main__":
    Main_1App().run()

 

 

 

 

From: Eric B
Sent: Tuesday, March 10, 2020 12:09 PM
To: Kivy users support
Subject: [kivy-users] Re: How do I use a variable from one class function inanother class function

 


When I tried that I got this error: KeyError: 'my_rbox' and AttributeError: 'super' object has no attribute '__getattr__'

Here is the runnable code:

#Python Code
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
class RBox(GridLayout):
   
def get_reason(self):
        reason =
self.ids.reason.text
       
return reason

class FormScreen(BoxLayout):
   
def pressed(self):
        app = App.get_running_app()
        app.root.ids.my_rbox.get_reason()
 
class MainApp(App):
   
def build(self):
       
return FormScreen()

if __name__ == "__main__":
    MainApp().run()
 
#.kv Code
<RBox>:
    cols: 2
    text: ''
    group: ''
    id: my_rbox
    RCheckBox:

        id: cb
        group: root.group
        on_active: root.get_reason()
    RLabel:

        on_press: cb._do_press()
        text: root.text
        width: root.width*0.5
        id: reason

--

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+...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages