繁体   English   中英

Python TKinter组合框自动完成

[英]python TKinter combobox autocomplete

我正在尝试创建一个组合框和一个依赖于组合框的条目小部件。 更准确地说:用户应在组合框中选择一个值,并且条目小部件使用自动完成功能根据组合框提供数据。 我在这里找到了自动完成源代码:

http://code.activestate.com/recipes/578253-an-entry-with-autocompletion-for-the-tkinter-gui/

到目前为止,这是我所做的:

from Tkinter import *
import ttk, os, re
root = Tk()
root.minsize(500,300)
root.maxsize(550,310)



class AutocompleteEntry(Entry):
    def __init__(self, lista, *args, **kwargs):
        Entry.__init__(self, *args, **kwargs)

        self.lista = lista
        print "lista in autocom " + str(lista)
        self.var = self["textvariable"]
        if self.var == '':
            self.var = self["textvariable"] = StringVar()
        self.var.trace('w', self.changed)
        self.bind("<Right>", self.selection)
        self.bind("<Up>", self.up)
        self.bind("<Down>", self.down)

        self.lb_up = False


    def changed(self, name, index, mode):
        if self.var.get() == '':
            self.lb.destroy()
            self.lb_up = False
        else:
            words = self.comparison()
            print words
            if words:
                if not self.lb_up:
                    self.lb = Listbox()
                    self.lb.bind("<Double-Button-1>", self.selection)
                    self.lb.bind("<Right>", self.selection)
                    self.lb.place(x=self.winfo_x(), y=self.winfo_y()+self.winfo_height())
                    self.lb_up = True

                self.lb.delete(0, END)
                for w in words:
                    self.lb.insert(END,w)
            else:
                if self.lb_up:
                    self.lb.destroy()
                    self.lb_up = False

    def selection(self, event):
        if self.lb_up:
            self.var.set(self.lb.get(ACTIVE))
            self.lb.destroy()
            self.lb_up = False
            self.icursor(END)

    def up(self, event):
        if self.lb_up:
            if self.lb.curselection() == ():
                index = '0'
            else:
                index = self.lb.curselection()[0]
            if index != '0':
                self.lb.selection_clear(first=index)
                index = str(int(index)-1)
                self.lb.selection_set(first=index)
                self.lb.activate(index)

    def down(self, event):
        if self.lb_up:
            if self.lb.curselection() == ():
                index = '0'
            else:
                index = self.lb.curselection()[0]
            if index != END:
                self.lb.selection_clear(first=index)
                index = str(int(index)+1)
                self.lb.selection_set(first=index)
                self.lb.activate(index)

    def comparison(self):
        pattern = re.compile(self.var.get() + '.*')
        return [w for w in self.lista if re.match(pattern, w)]




class MyListbox:

    lista = ['a', 'actions', 'additional', 'also', 'an', 'and', 'angle', 'are', 'as', 'be', 'bind', 'bracket', 'brackets', 'button', 'can', 'cases', 'configure', 'course', 'detail', 'enter', 'event', 'events', 'example', 'field', 'fields', 'for', 'give', 'important', 'in', 'information', 'is', 'it', 'just', 'key', 'keyboard', 'kind', 'leave', 'left', 'like', 'manager', 'many', 'match', 'modifier', 'most', 'of', 'or', 'others', 'out', 'part', 'simplify', 'space', 'specifier', 'specifies', 'string;', 'that', 'the', 'there', 'to', 'type', 'unless', 'use', 'used', 'user', 'various', 'ways', 'we', 'window', 'wish', 'you']


    def __init__(self, parent, title):
        self.parent = parent
        self.parent.title(title)
        self.parent.protocol("WM_DELETE_WINDOW", self.closes)
        self.cities = ['New York', 'Vienna', 'Miami', 'Oslo']

        self.establishment()

    def combobox_handler(self, event):
        current = self.combobox.current()
        print self.combobox.get()
        self.entNumber.delete(0, END)

        print self.lista
        self.entNumber = AutocompleteEntry(self.lista, self.parent)



    def establishment(self):
        mainFrame = Frame(self.parent)
        mainFrame.pack(fill=BOTH, expand=YES)

        fr_left = Frame(mainFrame, bd=10)
        fr_left.pack(fill=BOTH, expand=YES, side=LEFT)

        self.combobox = ttk.Combobox(fr_left, values=self.cities)
        self.combobox.bind('<<ComboboxSelected>>', self.combobox_handler)
        self.combobox.pack()

        fr_right = Frame(mainFrame, bd=10)
        fr_right.pack(fill=BOTH, expand=YES, side=RIGHT)

        fr_up = Frame(fr_right)
        fr_up.pack(side=TOP, expand=YES)


        self.entNumber = Entry()
        self.entNumber.place(x=50, y=100)

    def closes(self, event=None):
        self.parent.destroy()

if __name__ == '__main__':
    app = MyListbox(root, "Main Window")
    root.mainloop()

该代码仅用于测试。

问题是该程序在autocompleteEntry()的初始化中停止,没有任何错误。 有人可以提示我,我做错了吗?

谢谢您的期待! 斯特凡

您的问题是,即使您要创建AutocompleteEntry ,在组合框中选择一个条目后,也不会将其放置在任何位置,因此它不会显示。 您需要将其放置在某个地方才能显示出来。

范例-

self.entNumber1 = AutocompleteEntry(self.lista, self.parent)
self.entNumber1.place(x=50, y=120)

将其放置在名为entNumber的条目的正下方。


或者,您可以简单地从self.entNumber开始创建AutoCompleteEntry ,但这全取决于您要实现的目标。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM