You can access any Class attribute by walking the widget tree in kivy, and using ids.
You can start with app.root and build a chain to touch any class instance attribute.
Accessing screens you would use the get_screen() method of the screenmanager.
You put attributes in the App class and they can easily be accessed from anywhere in your code.
In kv: app.my_attribute
In Python:
app = App.get_running_app()
app.my_attribute = ‘new value’
--
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/7642ccf3-4545-47c2-8b13-aa3c74bcdbean%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/621f8ed6.1c69fb81.7a507.5ac5SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
You have not provided enough information for me to help you. Lets assume a widget tree:
Root:
ScreenManager:
id: sm
LoginScreen:
name: 'login_screen'
ScreenManager:
id: screen_manager
ReadFromLogin:
<ReadFromLogin@ Screen>:
Button:
text: 'Get Login Info'
on_release: print(app.root.ids.sm.get_screen('login_screen).login_screen_attribute)
If this is not helpful, share your code or a simplified widget tree.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CALgom8pXRtV859a9dqH-djto-zAPdZr%3DCv%2BveRw1Ts99%2BqWcfQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/621fd305.1c69fb81.46f96.eefdSMTPIN_ADDED_MISSING%40gmr-mx.google.com.
To access the user variable from any where in kv:
app.root.get_screen('tela_login').user
app.root is the root widget, in this case this is the screen manager, because this is the root widget, we do not need to use the ids.
app.root.get_screen('tela_login') provides the screen widget and
app.root.get_screen('tela_login').user provides the access the user attribute.
In python you can do effectively the same thing, start by getting the app with the call to App.get_running_app()
app = App.get_running_app() # I like to create a variable called app so the “kv string” looks the same in python and kv
app.root.get_screen('tela_login').user
I often find it helpful to print out these elements to check that I am addressing the correct widget.
Lets try to do this the other way – lets assume you want to access a variable from the screen ‘nova_tella’
app.root.get_screen(‘main’).ids.screen_manager.get_screen("nova_tela").ids.rv2 # this is the recycleview on the screen.
Let me know how this works out for you.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CALgom8pB0D6RYHNp1YOJntnJbe%2BYuxoCtXEvPoK3SDTyayJmYQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/62200370.1c69fb81.3faac.4d3eSMTPIN_ADDED_MISSING%40gmr-mx.google.com.
Ahh… just use MDApp.get_running_app()
Inside the AppDesktop you can just use self, self is the AppDesktop.
Don’t run this code at the class level, put it in a method.
From: Elias Coutinho
Sent: Wednesday, March 2, 2022 6:26 PM
To: kivy-...@googlegroups.com
Subject: Re: [kivy-users] Global Variable on kivy
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CALgom8pfpgzX1SNTZyU2T15gk1dvNdQN9cV9Vs5%3DfJHWKVT4mQ%40mail.gmail.com.
Here is an example. (this code may look familiar). Take a look at the on_start method and the series of print statements.
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
KV = '''
<ContentNavigationDrawer>:
ScrollView:
MDList:
OneLineListItem:
text: "Screen 1"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "scr 1"
OneLineListItem:
text: "Screen 2"
on_press:
root.nav_drawer.set_state("close")
root.screen_manager.current = "scr 2"
<LabelMDScreen@MDScreen>:
MDLabel:
id: label
text: "Screen 2"
halign: "center"
BoxLayout:
orientation: 'vertical'
MDToolbar:
id: toolbar
# pos_hint: {"top": 1}
elevation: 10
title: "MDNavigationDrawer"
left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
MDNavigationLayout:
# x: toolbar.height
ScreenManager:
id: screen_manager
MDScreen:
name: "scr 1"
# size_hint_y: None
# height: root.height - toolbar.height
md_bg_color: app.theme_cls.primary_light
MDLabel:
text: "Screen 1"
halign: "center"
LabelMDScreen:
name: "scr 2"
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
screen_manager: screen_manager
nav_drawer: nav_drawer
'''
class ContentNavigationDrawer(MDBoxLayout):
screen_manager = ObjectProperty()
nav_drawer = ObjectProperty()
class TestNavigationDrawer(MDApp):
def build(self):
return Builder.load_string(KV)
def on_start(self):
print(f'{self =}')
print(f'{MDApp.get_running_app() =}')
print(f'{self.root.ids = }')
print(f'{self.root.ids.screen_manager =}')
print(f'{self.root.ids.screen_manager.get_screen("scr 2") =}')
print(f'{self.root.ids.screen_manager.get_screen("scr 2").ids =}')
print(f'{self.root.ids.screen_manager.get_screen("scr 2").ids.label =}')
print(f'{self.root.ids.screen_manager.get_screen("scr 2").ids.label.text =}')
TestNavigationDrawer().run()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/62202154.1c69fb81.ce357.342fSMTPIN_ADDED_MISSING%40gmr-mx.google.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/622027dc.1c69fb81.3285e.ad93SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
Here is one way to do it, using a kivy property, called user.
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import Screen
from kivy.properties import StringProperty
'''
text: app.user
halign: "center"
Screen:
name: "scr 2"
MDLabel:
text: "Screen 2"
halign: "center"
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
screen_manager: screen_manager
nav_drawer: nav_drawer
<TelaLogin>:
name: 'screen_login'
md_bg_color: app.theme_cls.primary_color
# MDLabel:
# text: "Here I would login. and has a variable called user. The value of this variable must be read from anywhere in the application."
# halign: "center"
MDTextField:
hint_text: 'Enter User Name'
on_text: app.user = self.text
(MDApp):
user = StringProperty()
def build(self):
return Builder.load_string(KV)
TestNavigationDrawer().run()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CALgom8qGv%3DzvTVKHxqLfq695107rY01Nhhwxw08D5f58O2H2kA%40mail.gmail.com.
Here is another way to do it writing directly to the kivy property.
'''
id: user
halign: "center"
Screen:
name: "scr 2"
MDLabel:
text: "Screen 2"
halign: "center"
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
screen_manager: screen_manager
nav_drawer: nav_drawer
<TelaLogin>:
name: 'screen_login'
md_bg_color: app.theme_cls.primary_color
# MDLabel:
# text: "Here I would login. and has a variable called user. The value of this variable must be read from anywhere in the application."
# halign: "center"
MDTextField:
hint_text: 'Enter User Name'
on_text: app.root.get_screen('main').ids.user.text = self.text
(MDApp):
def build(self):
return Builder.load_string(KV)
TestNavigationDrawer().run()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/62213915.1c69fb81.5180a.e267SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
And a third way to do it, moving the string property to the main screen.
'''
id: user
halign: "center"
text: root.user
Screen:
name: "scr 2"
MDLabel:
text: "Screen 2"
halign: "center"
MDNavigationDrawer:
id: nav_drawer
ContentNavigationDrawer:
screen_manager: screen_manager
nav_drawer: nav_drawer
<TelaLogin>:
name: 'screen_login'
md_bg_color: app.theme_cls.primary_color
# MDLabel:
# text: "Here I would login. and has a variable called user. The value of this variable must be read from anywhere in the application."
# halign: "center"
MDTextField:
id: user_name
hint_text: 'Enter User Name'
MDFlatButton:
pos_hint: {'center_x': .5, 'center_y': .3}
text: 'Simulating correct login'
md_bg_color: 1, 0, 1, 1
on_release:
root.manager.get_screen('main').user = user_name.text
root.login()
MDFlatButton:
pos_hint: {'center_x': .5, 'center_y': .2}
text: "MDFLATBUTTON"
md_bg_color: 1, 0, 1, 1
on_release: app.root.current = 'screen_singup'
<TelaSingUp>:
name: 'screen_signup'
MDLabel:
text: "merely owns nothing"
halign: "center"
ScreenManager:
id: screen_manager_login
TelaLogin:
name: 'screen_login'
MainScreen:
name: 'main'
TelaSingUp:
name: 'screen_singup'
'''
class ContentNavigationDrawer(BoxLayout):
screen_manager = ObjectProperty()
nav_drawer = ObjectProperty()
class MainScreen
(Screen):
user = StringProperty()
class TelaLogin(Screen):
def login(self):
self.manager.current = 'main'
class TelaSingUp(Screen):
pass
class TestNavigationDrawer(MDApp):
def build(self):
return
Builder.load_string(KV)
def on_start(self):
print(MDApp.get_running_app().root)
TestNavigationDrawer().run()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/62213a8a.1c69fb81.d8dc6.583eSMTPIN_ADDED_MISSING%40gmr-mx.google.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/62213915.1c69fb81.5180a.e267SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/6221403f.1c69fb81.d10e8.f31bSMTPIN_ADDED_MISSING%40gmr-mx.google.com.
On Mar 3, 2022, at 6:07 PM, Elias Coutinho <coutinh...@gmail.com> wrote:
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CALgom8qjhaA049E-6LR7LdrtfwKtSTL2Y9327a__Ou7LNW%3D%2BpA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/15CFCD4C-46A0-45A1-A445-5D86375E4C66%40cox.net.
Line 205 is the kv rule definition of the ElementCard class. You need to identify the instance of the class that you want to change.
Lets assume a app structure as follows:
RootWidget
ScreenManager
Id: sm
Screen:
name: ‘card_screen’
BoxLayout:
ElementCard: # this is an instance of the ElementCard class, defined by the kv rule.
id: ec
You would access the ElementCard Instance, ec from a method in App as:
def app_method(self)
card = self.root.ids.sm.get_screen(‘card_screen’).ids.ec
The key thing to think about is you want to modify an instance – not the rule. You need to address the instance of the class you want to modify.
I am unable to run the project.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CALgom8pysQqP9-Jcf5qmAzNz8rTtaoEwvTSp6QbNn-9WKMNKEw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/622e07f4.1c69fb81.42327.cbfaSMTPIN_ADDED_MISSING%40gmr-mx.google.com.
MDApp.theme_cls.primary_dark
This syntax would only work if theme_cls is a class level attribute vs an instance attribute. I have not looked to see how it is implemented, but I suspect it is an instance level attribute.
Inside an MDApp method use:
self.theme_cls.primary_dark
In other classes…
app = MDApp.get_running_add()
app.theme_cls. primary_dark
To debug this issue:
Thus:
self.root.ids.sm.get_screen('main').ids.screen_manager.get_screen('new_screen').ids.ec.theme_cls.primary_dark
I received the response below:
self.root.ids.sm.get_screen('main').ids.screen_manager.get_screen('new_screen').ids.ec.theme_cls.primary_dark
File "kivy/properties.pyx", line 864, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
print out each part the “kv string” to make sure it is what you expect.
print(self.root.ids)
print(self.root.ids.sm)
print(self.root.ids.sm.get_screen('main'))
print(self.root.ids.sm.get_screen('main').ids)
print(self.root.ids.sm.get_screen('main').ids.screen_manager) …
If the ids dict is empty it can mean that your are executing this code prior to the kv code being processed. This will happen if you are accessing the ids in a __init__ method. If this is the case move the code to an on_kv_post() method.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CALgom8o%2BO8V1NXrUtabGPZukGcY4wLVd_%3Dud%3DFRZxF75AFO0mQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/622e32d5.1c69fb81.87b45.353eSMTPIN_ADDED_MISSING%40gmr-mx.google.com.
On Mar 13, 2022, at 1:17 PM, Elias Coutinho <coutinh...@gmail.com> wrote:
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CALgom8qpGtrpsZdNLU1iTtst%3D07D2Q78yn-tkmZ6_aNh%3DKeHzw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/8B22370A-967F-4B88-95A4-8D0F20E30A02%40cox.net.
On Mar 14, 2022, at 5:53 AM, Elias Coutinho <coutinh...@gmail.com> wrote:
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CALgom8pCJk%3DyHb31qpGbvaaY2TYZtr7cg6d4G4pRc1uG-J531w%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/DEF4BB97-CEBB-4156-8CE3-E2F1D3FF44F9%40cox.net.
Here is the key problem your root widget is a ScreenManager, self.root is the screenmanger, you need to start there.
def on_start(self):
print(self.root.get_screen('main').ids.screen_manager.get_screen('nova_tela').ids.rv2)
self.root.get_screen('main').ids.screen_manager.get_screen('nova_tela').ids.rv2.md_bg_color = self.theme_cls.primary_dark
The code above will run, but I don’t expect it will do what you want because the Recycle view does not have a md_bg_color property. The RecycleBoxLayout does not have the property either. I’m assuming you want to create a background color for the RecycleBoxLayout. You will need create a canvas and fill in the background with the canvas using Rectangle.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CALgom8ob%3DM3DoCutDVSv9EVzV268_bnK-TKGWUQrnj-46_Oc2g%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/622fdd4b.1c69fb81.c6b0f.cc62SMTPIN_ADDED_MISSING%40gmr-mx.google.com.
Thank you!
This is why I suggest printing out each step along the way… when you print self.root, you can see it is the screenmanager.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CALgom8oRkj3kQ%3D71akCvU%3DRc91e9UMPPYQPC%3Dkfk13r0vjihgA%40mail.gmail.com.