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()
ScreenManagement:
LoScreen
CtScreen
<LoScreen>:
......
<CtScreen>:
......
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.
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()
#: 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()
super().__init__(**kwargs)
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/dd20e10a-d310-4271-ac83-066a5a2646c1%40googlegroups.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.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/c6165a80-9863-46e2-a5e2-ead479e6eaea%40googlegroups.com.
😎 Well done!
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/2abf93e2-9bda-4247-a3fc-3f39514cfc99n%40googlegroups.com.