0

我正在尝试使用 kivy 创建 GUI,但无法计算出这个。创建的弹出屏幕并尝试从 dropbox 中选择值并将此值添加到我的 TextInput 中。

测试.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()

测试.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。

但是在从微调器中选择后无法更新 TextInput。如何在从 dropbox 中选择值后更新此文本输入。我想不通。我想我正确地描述了我的问题。感谢阅读和回答。

4

1 回答 1

0

kv = "微调器:

        on_text:

              my_textinput_id.text = my_popup_spinner.text"

在我的 Builder.load_string(kv) 的 kv 字符串中做了这个,它对我有用,你可以尝试在你的代码示例中实现它,看看它是否有效。

将 textinput = 的 id.text 设置为我的微调器 id.text 在 on_text: 微调器的事件

于 2020-06-25T05:24:30.127 回答