Reputation: 73
main:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
class testclass:
def someth(txt):
print (txt)
#how get access to textinput from begin Screen here?
class BeginScreen(Screen):
def __init__(self,**kwargs):
super().__init__()
self.layout =BoxLayout(orientation='vertical',padding=20,spacing=5,)
self.btn=Label(text=str('Hello'))
self.layout.add_widget(self.btn)
self.btn=TextInput(id='test',text='')
self.layout.add_widget(self.btn)
self.btn=Button(text='Button!', on_press=testclass.someth('?'))
# what write in ? to send textinput text to testclass.someth?
self.layout.add_widget(self.btn)
self.add_widget(self.layout)
print(self.layout.ids) #why i have no ids? textinput have id
class TestApp(App):
from kivy.config import Config
Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '400')
def build(self):
sm = ScreenManager()
sm.add_widget(BeginScreen(name='test'))
return sm
TestApp().run()
So how can i access the textinput? I have id='test' but when i printing layouts id is saying i have noone. Why? Someone can explain me what im doing wrong and how can i make it good?
Upvotes: 2
Views: 17135
Reputation: 16041
To access the textinput in your external method, you could use partial functions or lambda function.
You are getting None or empty dictionary because you don't have a kv file.
When your kv file is parsed, kivy collects all the widgets tagged with id’s and places them in this self.ids dictionary type property.
Please refer to my example below for deatils.
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from functools import partial
class testclass:
def someth(*args, txt):
print(txt)
class BeginScreen(Screen):
def __init__(self, **kwargs):
super(BeginScreen, self).__init__(**kwargs)
layout = BoxLayout(orientation='vertical', padding=20, spacing=5)
layout.add_widget(Label(text=str('Hello')))
# layout.add_widget(TextInput(id='test', text='')) # id in python+kivy is deprecated
txtInput = TextInput(text='text input')
layout.add_widget(txtInput)
self.ids['test'] = txtInput
layout.add_widget(Button(text='Button!', on_press=partial(testclass.someth, txt=self.ids.test.text)))
self.add_widget(layout)
print("self.ids={}".format(self.ids))
print("self.ids['test']={}".format(self.ids['test']))
print("self.ids['test'].text={}".format(self.ids['test'].text))
print("self.ids.test.text={}".format(self.ids.test.text))
for key, val in self.ids.items():
print("key={0}, val={1}".format(key, val))
class TestApp(App):
from kivy.config import Config
Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '400')
def build(self):
sm = ScreenManager()
sm.add_widget(BeginScreen(name='test'))
return sm
TestApp().run()
Upvotes: 3