I want to ask how kivy passes values in different classes.

55 views
Skip to first unread message

yi tian

unread,
Aug 15, 2019, 9:39:22 AM8/15/19
to Kivy users support


Such code
Py
from kivy.uix.screenmanager import ScreenManager,Screen

from kivy.lang.builder import Builder


class LoScreen(Screen):
   
...

     name
= 'a'
class CtScreen(Screen):
   
...


class ScManagement(ScreenManager):
   
pass

class ChAPP(App):


CtAPP().run()



Kv file.
ScreenManagement:
   
LoScreen
   
CtScreen
<LoScreen>:
   
......

<CtScreen>:
   
......


In the LoScreen class, there is a variable 'name'
I want to use the value of 'name' in the CtScreen class

how should I do it?        thankyou

Can you only declare a global variable outside the class?

Elliot Garbus

unread,
Aug 15, 2019, 11:12:27 AM8/15/19
to kivy-...@googlegroups.com

If you create a small runnable example you will get a better quality of answer.  There are a number of ways to solve these kinds of problems.

One option is to create kivy properties at the app level.  These can be easily accessed across your python code and kivy code.

 

from kivy.properties import StringProperty

class ChAPP(App):

    my_app_string = StringProperty(‘Initial Value’)

 

 

This can be accessed from any python code by getting the current running app:

 

From kivy.app import App

class SomeOtherClass:

 

    def a_class_method(self):

        app_ptr = App.get_running_app()

     app_ptr.my_app_string = ‘New Value’

 

or in KV

Button:

    text: app.my_app_string

 

There is another example taking a different approach at this thread, see the solution one up from the bottom of the thread.

https://groups.google.com/forum/?utm_medium=email&utm_source=footer#!topic/kivy-users/5ul5wLs5vsY

--
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/bd6daf5c-a4a7-4ca5-97d7-681a088d289e%40googlegroups.com.

 

yi tian

unread,
Aug 17, 2019, 1:26:34 AM8/17/19
to Kivy users support
my main.py
from kivy.app import App

from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.lang.builder import Builder


class LoScreen(Screen):

   
def __init__(self, **kwargs):
       
self.named = ''
       
super().__init__(**kwargs)
       
with open("last_login.txt", "r") as f:
            d
= f.read()
           
self.named = d


class CtScreen(Screen,LoScreen):
   
def swith(self):
       
print(LoScreen().named)



class ScManagement(ScreenManager):
   
pass

class ChAPP(App):

   
def build(self):
       
return Builder.load_file('main.kv')


ChAPP().run()


and  main.kv
#: import GridLayout kivy.uix.gridlayout.GridLayout
#: import BoxLayout kivy.uix.boxlayout.BoxLayout


ScManagement:
   
LoScreen
   
CtScreen

<LoScreen>:
    name
: 'lo'
   
GridLayout:
        cols
:2
       
Label:
            text
: 'name'
       
TextInput:
            id
:id_name
            text
: root.named
            multiline
:False
       
Label:
       
Button:
            text
: 'login'
            on_press
: root.manager.current = 'ch'
<CtScreen>:
    name
: 'ch'
   
Button:
        text
: 'login'
        on_release
: root.swith()

I want to use the value of ‘named’ in the CtScreen class


yi tian

unread,
Aug 17, 2019, 1:38:27 AM8/17/19
to Kivy users support
I see some examples that will have this sentence.
super().__init__(**kwargs)

Why must I write this sentence?

Elliot Garbus

unread,
Aug 17, 2019, 9:35:14 AM8/17/19
to kivy-...@googlegroups.com

Here are some minimal changes to get access to a variable across screens.  This is no passing a value.

 

A few important concepts:

You are defining your screens when in the KV code you put:

<LoScreen> this is defining the class.

When you write LoScreen: you are creating an instance of that class.  You may have many instances of one class.

I moved the names of the screens from the class definition to the instances.

 

I accessed a variable in a different screen, by chaining a set of pointers from the app pointer to root

class CtScreen(Screen):   #  class CtScreen(Screen,LoScreen):
   
def swith(self):
        app = App.get_running_app()                 # Gets the a pointer to the app
        named = app.root.get_screen(
'lo').named     # goes through the app pointer to find the variable named
       
print('read from the app pointer:', named)

 

In python what look like parameters in the class definition are the parents the class is inheriting from.

 

Here is an article on object orientated programming in Python: https://realpython.com/python3-object-oriented-programming/

 

 

 

From: yi tian
Sent: Friday, August 16, 2019 10:26 PM
To: Kivy users support

--

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.

main.kv
ch_test.py

Elliot Garbus

unread,
Aug 17, 2019, 9:46:18 AM8/17/19
to kivy-...@googlegroups.com

When you create an __init__ method in a class, you are overriding the default constructor, and so the class must explicitly call the constructor the parent class.

Super() is used to refer to parent classes without naming them explicitly, thus making the code more maintainable.

 

class MyClass(MyParent):  # MyClass inherits from the parent class, MyParent

   def __init__(self, **kwargs):

     …

            super().__init__(**kwargs)  # this is the same as MyParent.__init__(**kwargs)

            …

 

Read:

https://rhettinger.wordpress.com/2011/05/26/super-considered-super/

https://docs.python.org/3/library/functions.html#super

 

From: yi tian
Sent: Friday, August 16, 2019 10:38 PM
To: Kivy users support

--

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.

David Taylor

unread,
Oct 26, 2020, 6:00:31 PM10/26/20
to Kivy users support
Hi,

This post was really helpful to me and its almost solved my problem.  I have a small Kivy application that has a User class that gets instantiated when the user logs in.  The class contains various attributes for the user (e.g. username, email address, profile, etc).  I want to be able to access the class instance and its attributes in various kivy screens.  For example in the "Profile" page I want to display the various attributes and allow the user to change.  

In your example you have a StringProperty that you initialise, change the value of and display.  Is there a way to define an attribute that holds the user class?  

David Taylor

unread,
Oct 26, 2020, 6:08:57 PM10/26/20
to Kivy users support
I solved the problem.  I was using ObjectProperty but the changes were not getting saved.  I just read the documentation again and spotted something I had missed.  By default changes to ObjectProperty attributes are not re-bound.  Luckily there is a way to make it save using:

user ObjectProperty(rebind=True)

Elliot Garbus

unread,
Oct 26, 2020, 7:19:54 PM10/26/20
to kivy-...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages