Kivy:从 Spinner 或 Dropdown 中选取值后更新 TextInput
Kivy: Update TextInput after picking value from Spinner or Dropdown
我正在尝试使用 kivy 创建 GUI 但无法识别 this.Created 弹出屏幕并尝试从 dropbox 中选择值并将该值添加到我的 TextInput。
test.py:
from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.dropdown import DropDown
from kivy.uix.spinner import Spinner
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.uix.popup import Popup
from kivy.properties import ObjectProperty
class MyDropDown(DropDown):
def on_select(self, data):
print('Selected value:',data)
#Lets add this data to TextInput ?
MyTextInput.text = data
#Lets check is that actual text ?
print('MyTextInput.text is:',MyTextInput.text)
#HOW CAN I ADD THIS TEXT TEXTINPUT AFTER THAT?
class MySpinner(Spinner):
dropdown_cls = ObjectProperty(MyDropDown)
class MyTextInput(TextInput):
pass
class MyTestPopup(Popup):
my_popup_spinner = ObjectProperty()
my_textinput_id = ObjectProperty()
class TestPage(Screen):
MypagePopup = ObjectProperty()
def open_my_popup(self, *args):
#Lets create my popup
self.MypagePopup = MyTestPopup()
self.MypagePopup.my_popup_spinner.values = ['Test1','Test2','Test3','Test4','Test']
self.MypagePopup.open()
class TestPageManager(ScreenManager):
pass
class test(App):
def build(self):
return TestPageManager()
if __name__=='__main__':
test().run()
test.kv:
<TestPageManager>:
TestPage:
name: 'mainpagename'
<MySpinner>:
text: 'Pick Value'
<MyDropDown>:
values: ['Test1','Test2','Test3','Test4','Test5']
<MyTextInput>:
<MyTestPopup>:
my_popup_spinner : my_popup_spinner
my_textinput_id : my_textinput_id
BoxLayout:
orientation: 'vertical'
Label:
text: 'Testing Label Area..'
pos: self.pos
size: self.size
MyTextInput:
id: my_textinput_id
MySpinner:
id: my_popup_spinner
Button:
text: 'Done'
on_release:
print('MyTextInput id is :',my_textinput_id.text)
<TestPage>:
Button:
text: 'Open Popup'
on_release: root.open_my_popup()
如您所见,我创建了那个 GUI。
但是从 spinner.How 中选取后无法更新 TextInput 我可以在从 dropbox.I 中选取值后更新此 textinput 无法计算 out.I 我认为我正确地描述了我的问题。感谢阅读和回答。
kv = "微调器:
on_text:
my_textinput_id.text = my_popup_spinner.text"
在我的 Builder.load_string(kv) 的 kv 字符串中做了这个,它对我有用
您可以尝试在您的代码示例中实现它,看看它是否有效。
在 on_text: 事件
中将 textinput = 的 id.text 设置为我的微调器 id.text
我正在尝试使用 kivy 创建 GUI 但无法识别 this.Created 弹出屏幕并尝试从 dropbox 中选择值并将该值添加到我的 TextInput。
test.py:
from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.dropdown import DropDown
from kivy.uix.spinner import Spinner
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.uix.popup import Popup
from kivy.properties import ObjectProperty
class MyDropDown(DropDown):
def on_select(self, data):
print('Selected value:',data)
#Lets add this data to TextInput ?
MyTextInput.text = data
#Lets check is that actual text ?
print('MyTextInput.text is:',MyTextInput.text)
#HOW CAN I ADD THIS TEXT TEXTINPUT AFTER THAT?
class MySpinner(Spinner):
dropdown_cls = ObjectProperty(MyDropDown)
class MyTextInput(TextInput):
pass
class MyTestPopup(Popup):
my_popup_spinner = ObjectProperty()
my_textinput_id = ObjectProperty()
class TestPage(Screen):
MypagePopup = ObjectProperty()
def open_my_popup(self, *args):
#Lets create my popup
self.MypagePopup = MyTestPopup()
self.MypagePopup.my_popup_spinner.values = ['Test1','Test2','Test3','Test4','Test']
self.MypagePopup.open()
class TestPageManager(ScreenManager):
pass
class test(App):
def build(self):
return TestPageManager()
if __name__=='__main__':
test().run()
test.kv:
<TestPageManager>:
TestPage:
name: 'mainpagename'
<MySpinner>:
text: 'Pick Value'
<MyDropDown>:
values: ['Test1','Test2','Test3','Test4','Test5']
<MyTextInput>:
<MyTestPopup>:
my_popup_spinner : my_popup_spinner
my_textinput_id : my_textinput_id
BoxLayout:
orientation: 'vertical'
Label:
text: 'Testing Label Area..'
pos: self.pos
size: self.size
MyTextInput:
id: my_textinput_id
MySpinner:
id: my_popup_spinner
Button:
text: 'Done'
on_release:
print('MyTextInput id is :',my_textinput_id.text)
<TestPage>:
Button:
text: 'Open Popup'
on_release: root.open_my_popup()
如您所见,我创建了那个 GUI。
但是从 spinner.How 中选取后无法更新 TextInput 我可以在从 dropbox.I 中选取值后更新此 textinput 无法计算 out.I 我认为我正确地描述了我的问题。感谢阅读和回答。
kv = "微调器:
on_text:
my_textinput_id.text = my_popup_spinner.text"
在我的 Builder.load_string(kv) 的 kv 字符串中做了这个,它对我有用 您可以尝试在您的代码示例中实现它,看看它是否有效。
在 on_text: 事件
中将 textinput = 的 id.text 设置为我的微调器 id.text