[英]how can i change the hint_text of TextInput box which is in .kv file , from .py file
[英]Get text from TextInput defined in .kv file from class
我在python中有以下代碼:
from kivy.app import App
from kivy.uix.widget import Widget
class UI(Widget):
pass
class UIApp(App):
def build(self):
return UI()
def process(self):
text = self.ids.input.text
print(text)
並在UI.kv中:
#:kivy 1.0.9
<UI>:
title: 'InputDialog'
auto_dismiss: False
id: 'test1'
RelativeLayout:
orientation: 'vertical'
pos: self.pos
size: root.size
id: 'test2'
TextInput:
id: 'input'
hint_text:'Enter compounds'
pos_hint: {'center_x': 0.5, 'center_y': 0.705}
size_hint: 0.95, 0.5
現在,如果我調用process(),我將得到錯誤'UIApp' object has no attribute 'ids'
。 好的。 現在,我嘗試用UI
替換self
: AttributeError: 'kivy.properties.DictProperty' object has no attribute 'input'
。 嘗試獲取'test1'也無濟於事,也沒有刪除.kv文件中ID周圍的'。
有沒有一種方法可以從字段獲取輸入而無需以編程方式創建此字段?
在您的kv文件中,您已將id
定義為字符串。
警告
為id分配值時,請記住該值不是字符串。 沒有引號:好-> id:值,差-> id:“值”
id
刪除單引號 self.ids.input.text
與self.root.ids.input.text
因為ids
中的根目錄下定義, class UI()
#:kivy 1.0.9
<UI>:
title: 'InputDialog'
auto_dismiss: False
id: test1
RelativeLayout:
orientation: 'vertical'
pos: self.pos
size: root.size
id: test2
TextInput:
id: input
hint_text:'Enter compounds'
pos_hint: {'center_x': 0.5, 'center_y': 0.705}
size_hint: 0.95, 0.5
on_text: app.process()
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
Builder.load_file('main.kv')
class UI(Widget):
pass
class UIApp(App):
def build(self):
return UI()
def process(self):
text = self.root.ids.input.text
print(text)
if __name__ == "__main__":
UIApp().run()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.