Shivr
Shivr

Reputation: 35

Kivy - How to access kivy widget in python?

I want to edit a text in a TextInput in kivy but don't know how to, I've tried this code as Ive search in the net but still no luck.

class MainApp(Screen):
    def __init__(self,**kwargs):
        super(MainApp,self).__init__(**kwargs)
    pass

class Forms(BoxLayout): 
    def __init__(self, **kwargs):
        super(Main,self).__init__(**kwargs)
        self.ids.txtSearch.text = "new text"

class Main(App):
    def build(self):
        return root_widget

if __name__ == "__main__":
    Main().run()

------kivy-------

<Main>:
    TextInput:
        id: txtSearch

this is not my whole code but I think those are what matters in the issue

this is the error:

    File "C:\Users\Guest\Documents\Python\Kivy\Project\main.py", line 295, in <module>
 ''')
 File "D:\Kivy-1.9.0-py3.4-win32-x64\kivy34\kivy\lang.py", line 1828, in load_string
 self._apply_rule(widget, parser.root, parser.root)
 File "D:\Kivy-1.9.0-py3.4-win32-x64\kivy34\kivy\lang.py", line 1985, in _apply_rule
 self.apply(child)
 File "D:\Kivy-1.9.0-py3.4-win32-x64\kivy34\kivy\lang.py", line 1872, in apply
 self._apply_rule(widget, rule, rule)
 File "D:\Kivy-1.9.0-py3.4-win32-x64\kivy34\kivy\lang.py", line 1986, in _apply_rule
 self._apply_rule(child, crule, rootrule)
 File "D:\Kivy-1.9.0-py3.4-win32-x64\kivy34\kivy\lang.py", line 1986, in _apply_rule
 self._apply_rule(child, crule, rootrule)
 File "D:\Kivy-1.9.0-py3.4-win32-x64\kivy34\kivy\lang.py", line 1983, in _apply_rule
 child = cls(__no_builder=True)
 File "C:\Users\Guest\Documents\Python\Kivy\Project\main.py", line 40, in __init__
 self.ids.txtSearch.text = "new text"
  File "kivy\properties.pyx", line 720, in kivy.properties.ObservableDict.__getattr__ (kivy\properties.c:10911)
 AttributeError: 'super' object has no attribute '__getattr__'

Upvotes: 1

Views: 1117

Answers (2)

RingK
RingK

Reputation: 83

To change Widgets properties in kivy you need to 'link' the widget between .py and .kv file, first in .py file:

txt_Search = ObjectProperty()

then in .kv file, in your root widget:

txt_Search: txtSearch

then assign the id to a widget (as you already did):

<Main>:
    TextInput:
        id: txtSearch
        text: ' ' 

then in your .py file, you can change attributes of the widget by doing this:

self.txt_Search.text = 'New Text'

or any other attribute:

self.txt_Search.height = '30dp'

Upvotes: 2

martin
martin

Reputation: 96889

Are you sure self.ids.txtSearch exists when you try to assign a text to it? You're calling super(Main,self) one line above so I guess txtSearch is never instantiated.

Btw, it's better to init widgets in *.kv files:

<Main>:
    TextInput:
        id: txtSearch
        text: "new text"

Upvotes: 0

Related Questions