【问题标题】:Kivy: Update TextInput after picking value from Spinner or DropdownKivy:从 Spinner 或 Dropdown 选择值后更新 TextInput
【发布时间】:2020-10-12 23:04:36
【问题描述】:

我正在尝试使用 kivy 创建 GUI,但无法确定这一点。已创建弹出屏幕并尝试从保管箱中选择值并将此值添加到我的 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 类似。

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

【问题讨论】:

    标签: python kivy kivy-language python-class


    【解决方案1】:

    kv = "微调器:

            on_text:
    
                  my_textinput_id.text = my_popup_spinner.text"
    

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

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

    【讨论】:

    • 感谢您的回答。实际上,我在从下拉列表中选择后使用 on_text 函数重置我的 Spinner 的文本。所以现在,如果我像这样编辑我的 kivy 文件: on_text: MyTextInputid.text = MySpinnerid.text MySpinnerid.text = 'Pick from DropDown' 它失败并将我的微调器和文本输入的文本返回到“从 DropDown 选择”。有没有办法解决这个问题?我不想在更改微调器的文本后更改 mytextinput 的文本..
    • 嗯,我用这种方式更改了 spinnertext:在 MySpinner 类中,我创建了具有 Clock.schedule_once(self.reset_spinners_text) 的函数。
    猜你喜欢
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 2017-01-28
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多