开发者

GTK / PyGTK make ComboBox searchable with keyboard

开发者 https://www.devze.com 2023-01-11 02:57 出处:网络
Is it possible to make a ComboBox searchable ? If yes, how ? I want to be able, when the ComboBox is active and a letter is typed with the keyboard, to select the first item beginning with this lette

Is it possible to make a ComboBox searchable ? If yes, how ?

I want to be able, when the ComboBox is active and a letter is typed with the keyboard, to select the first item beginning with this letter inside the ComboBox and so on with the next letters.

The is the same functionality of a ComboBox inside a webpage, for example.

I can't find any option the achieve this on the ComboBox or on the ListStore containing the data in the same way as the TreeView has the m开发者_开发百科ethods set_enable_search and set_search_column.


How to: an introduction to GTK+ treeviews and autocompletion.


I finally decided to write my own completion function :

def func(menu, user_data, (widget, window)):
        return (widget.get_allocation().x + window.get_position()[0],widget.get_allocation().y + window.get_position()[1],True)

def completion(self, widget, event):
        alphanum = re.compile(r'[a-zA-Z0-9-]')
        keyval = event.keyval
        key = event.string
        if keyval == 65288:
            #DEL
            self.text = self.text[:-1]
        elif alphanum.match(key):
            self.text = self.text+key
        else:
            self.yTree.get_widget("comp_menu").popdown()
            self.text = ''
            return
        self.yTree.get_widget("comp_menu").popup( None, None, self.func, 1, event.time, (widget, self.wTree.get_widget('main_window')))

        widget.grab_focus()
        m = widget.get_model()
        j = 0
        for i in m:
            if i[0].lower().startswith(self.text):
                widget.set_active(j)
                return
            j+=1
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号