在Pythonista 3中创建自动完成的TextField

时间:2018-03-18 14:31:13

标签: python pythonista

我想创建一个自动完成的TextField。

我的意思是 - 当您在字段中键入内容并在下面看到提示列表时。提示列表是一个包含可能值的数组。解释它的最佳方式显示了类似的图片。

Auto-complete example

我已经拥有Pythonista 3的一些经验,但它不是UI编程经验。

我理解这很复杂,也许我应该使用额外的View and Delegate机制,但我不知道如何开始。我已经花了几天时间在谷歌寻找解决方案,但我不能,在Pythonista的背景下。

有人这样做过吗?或者有人可以提供有用的阅读链接?

1 个答案:

答案 0 :(得分:1)

可以使用TableView在Pythonista中创建下拉列表。 TableViews实际上只是单列列表,它们不仅仅适用于表格。

所以步骤是:

  1. 创建一个tableview。
  2. 将其与文字字段对齐。
  3. 隐藏tableview,直到键入开始。
  4. 每当输入更改时,使用自动完成选项更新tableview的项目列表。
  5. 键入结束时可能会再次隐藏tableview。
  6. 您可以通过将.hidden属性设置为True来隐藏任何视图。

    通过创建实现textfield_did_change的委托对象,您可以在TextField中键入start时执行操作。

    通过为TableView提供data_source(可能是ui.ListDataSource的实现),将TableView设置为包含项目列表。只要数据源上的items属性发生更改,选项列表也会自动更改。

    您可以通过在TableView的action上设置delegate来对用户选择TableView中的选项作出反应。

    可以在Pythonista的Native GUI for iOS文档中找到TableView,TextField,ListDataSource,委托和操作的文档。

    这是一个基本的例子:

    # documentation at http://omz-software.com/pythonista/docs/ios/ui.html
    import ui
    
    # the phoneField delegate will respond whenever there is typing
    # the delegate object will combine phoneField delegate, tableview delegate, and data source, so that it can share data
    class AutoCompleter(ui.ListDataSource):
        def textfield_did_change(self, textfield):
            dropDown.hidden = False
            # an arbitrary list of autocomplete options
            # you will have a function that creates this list
            options = [textfield.text + x for x in textfield.text]
    
            # setting the items property automatically updates the list
            self.items = options
            # size the dropdown for up to five options
            dropDown.height = min(dropDown.row_height * len(options), 5*dropDown.row_height)
    
        def textfield_did_end_editing(self, textfield):
            #done editing, so hide and clear the dropdown
            dropDown.hidden = True
            self.items = []
    
            # this is also where you might act on the entered data
            pass
    
        def optionWasSelected(self, sender):
            phoneField.text = self.items[self.selected_row]
            phoneField.end_editing()
    
    autocompleter = AutoCompleter(items=[])
    autocompleter.action = autocompleter.optionWasSelected
    
    # a TextField for phone number input
    phoneField = ui.TextField()
    phoneField.delegate = autocompleter
    phoneField.keyboard_type = ui.KEYBOARD_PHONE_PAD
    phoneField.clear_button_mode = 'while_editing'
    
    # the drop-down menu is basically a list of items, which in Pythonista is a TableView
    dropDown = ui.TableView()
    dropDown.delegate = autocompleter
    dropDown.data_source = autocompleter
    # hide the dropdown until typing starts
    dropDown.hidden = True
    
    # create interface
    mainView = ui.View()
    mainView.add_subview(phoneField)
    mainView.add_subview(dropDown)
    
    # present the interface before aligning fields, so as to have the window size available
    mainView.present()
    
    # center text field
    phoneField.width = mainView.width*.67
    phoneField.height = 40
    
    phoneField.x = mainView.width/2 - phoneField.width/2
    phoneField.y = mainView.height/3 - phoneField.height/2
    
    # align the dropdown with the phoneField
    dropDown.x = phoneField.x
    dropDown.y = phoneField.y + phoneField.height
    dropDown.width = phoneField.width
    dropDown.row_height = phoneField.height
    

    在我的iPhone上,此代码会创建一个如下所示的界面:

    Sample code output on iPhone 8