GTK / PyGTK 使 ComboBox 可通过键盘搜索

发布于 2024-09-14 18:53:51 字数 272 浏览 2 评论 0原文

是否可以使 ComboBox 可搜索?如果是,怎么办?

我希望能够,当组合框处于活动状态并且使用键盘键入字母时,选择组合框中以该字母开头的第一个项目,依此类推下一个字母。

例如,这与网页内的组合框具有相同的功能。

我找不到任何选项可以在 ComboBox 或包含数据的 ListStore 上实现此目的,其方式与 TreeView 具有方法 set_enable_searchset_search_column 的方式相同。

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 methods set_enable_search and set_search_column.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灵芸 2024-09-21 18:53:51

我最终决定编写自己的完成函数:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文