I'm trying to get the value of something in my .kv file and change it using self.ids, but I'm not able to access it for some reason. When I try to, I get an attribute error, "AttributeError: 'CandLApp' object has no attribute 'ids'". I don't know if it's getting the right class or not, or if the code will even work at all since i'm using the builder.load_file thing.
I'm also trying to be pass the id of the button the function is called on, but I'm not sure how to do that either. I added some code for it in the py file, but in order for that code to even work the self.ids needs to work first. Would really appreciate any help
.py code:
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import time
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.config import Config
from kivy.core.window import Window
Builder.load_file("test.kv")
valuev = ""
class FirstScreen(Screen):
pass
class SecondScreen(Screen):
pass
class CandLApp(App):
global test123
def build(self):
sm = ScreenManager()
sm.add_widget(FirstScreen(name="first"))
sm.add_widget(SecondScreen(name="second"))
return sm
def text_wait(self, text, idforbutton):
global valuev
print("called") #does print out in console
print(self.ids.buttonfortext.text) #should print "sending this"
print(self.ids.idforbutton.text) #Also trying to be able to pass the value of id
#in the function, but the first self.ids
#doesn't even work
for i in text:
self.ids.buttonfortext.text = valuev i
time.sleep(1)
print(valuev)
if __name__ == "__main__":
CandLApp().run()
.kv code:
<FirstScreen>:
Button:
id: obama_text
size_hint: (.274, .2)
pos_hint: {"x":.067, "y":.049}
background_normal: ''
background_color: 1, .5, .6, .85
font_size: 45
bold: True
color: 200/255, 0/255, 70/255, 1
text: "Test"
on_release: root.manager.current = "second"
font_name: 'Micross'
<SecondScreen>:
FloatLayout:
Button:
id: buttonfortext
text: "button"
pos_hint: {"center_x": .5, "center_y": .5}
size_hint: .2, .2
on_release: app.text_wait("sending this", "buttonfortext") #sends "sending this" and the value of the id to text_wait
CodePudding user response:
The ids
are defined within the class that is the root of the kv
rule where they are defined. So buttonfortext
will be in the ids
of the instance of SecondScreen
. And there are no ids
defined for CandLApp
. So try replacing:
self.ids.buttonfortext.text
with:
self.root.get_screen("second").ids.buttonfortext.text