我想在kivy的TextInput中编辑一个文本,但不知道怎么做,我已经尝试过这个代码,因为我在网上搜索但仍然没有运气。
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
这不是我的整个代码,但我认为这些是问题中的重点
这是错误:
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__'
答案 0 :(得分:1)
要在kivy中更改Widgets属性,您需要在.py和.kv文件之间“链接”小部件,首先在.py文件中:
txt_Search = ObjectProperty()
然后在.kv文件中,在根小部件中:
txt_Search: txtSearch
然后将id分配给一个小部件(就像你已经做过的那样):
<Main>:
TextInput:
id: txtSearch
text: ' '
然后在.py文件中,您可以通过执行以下操作来更改窗口小部件的属性:
self.txt_Search.text = 'New Text'
或任何其他属性:
self.txt_Search.height = '30dp'
答案 1 :(得分:0)
当您尝试为其分配self.ids.txtSearch
时,您确定text
存在吗?您在上面一行调用了super(Main,self)
,所以我猜txtSearch
永远不会被实例化。
顺便说一句,在*.kv
个文件中初始化小部件更好:
<Main>:
TextInput:
id: txtSearch
text: "new text"