Как добавить запись автозаполнения в мой уже существующий проект tkinter? - PullRequest
0 голосов
/ 21 апреля 2020

Я работаю над проектом Tkinter, я закончил, и все работает, но я хотел добавить список автозаполнения в одну из моих записей, поэтому я клонировал один из github, но я не могу заставить его работать. .. Каждый раз, когда я запускаю, он открывает два windows с виджетом поверх моего проекта вместо того, чтобы находиться в самом проекте как запись

. Я хочу, чтобы запись автозаполнения работала над записью "entername_e". Это GUI моего проекта:

 class Application:
        def __init__(self, master, *args, **kwargs):                
            self.master = master
            #frame
            self.left = Frame(master, width=800, height=768, bg='white')
            self.left.pack(side=LEFT)

            self.right = Frame(master, width=566, height=768, bg='lightblue')
            self.right.pack(side=RIGHT)

            #components
            self.heading = Label(master, text= "Quame Supermarket", font=('arial 40 bold'), bg='white')
            self.heading.place(x=0, y=0)

            self.date_l = Label(self.right, text="Today's Date: "+str(date), font=('arial 14 bold'), bg='lightblue', fg='white')
            self.date_l.place(x=0, y=0)

            #table invoice=========================
            self.tproduct = Label(self.right, text="Products", font=('arial 18 bold'), bg='lightblue', fg='white')
            self.tproduct.place(x=0, y=60)

            self.tquantity = Label(self.right, text="quantity", font=('arial 18 bold'), bg='lightblue', fg='white')
            self.tquantity.place(x=200, y=60)

            self.tamount = Label(self.right, text="amount", font=('arial 18 bold'), bg='lightblue', fg='white')
            self.tamount.place(x=400, y=60)

            #enter stuff
            self.enterid = Label(self.left, text="Enter Product's ID", font=('arial 18 bold'), bg='white')
            self.enterid.place(x=0, y=80)

            self.enteride = Entry(self.left, width=25, font=('arial 18 bold'), bg='lightblue')
            self.enteride.place(x=260, y=80)
            self.enteride.focus()

            self.entername = Label(self.left, text="Enter Product's Name", font=('arial 18 bold'), bg='white')
            self.entername.place(x=0, y=150)

            self.entername_e = Entry(self.left, width=25, font=('arial 18 bold'), bg='lightblue')
            self.entername_e.place(x=260, y=150)

            #button 
            self.search_btn = Button(self.left, text="Search by Id(Right)", width=20, height=2, bg='orange', command=self.ajax1)
            self.search_btn.place(x=600, y=80)

            self.search_btn = Button(self.left, text="Search by Name", width=20, height=2, bg='orange', command=self.ajax2)
            self.search_btn.place(x=600, y=150)

            self.clear_btn = Button(self.left, text="Clear All", width=20, height=2, bg='green', fg='white', command=self.clear)
            self.clear_btn.place(x=600, y=490)

            #fill it later by the fucntion ajax
            self.productname = Label(self.left, text="", font=('arial 27 bold'), bg='white', fg='green')
            self.productname.place(x=0, y=250)

            self.pprice = Label(self.left, text="", font=('arial 27 bold'), bg='white', fg='green')
            self.pprice.place(x=0, y=290)

            #total label
            self.total_l = Label(self.right, text="Total", font=('arial 40 bold'), bg='lightblue', fg='green')
            self.total_l.place(x=0, y=600)
root = Tk()
b = Application(root)

root.geometry("1366x768+0+0")
root.mainloop()

Это код, который я взял с github:

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

        # Listbox length
        if 'listboxLength' in kwargs:
            self.listboxLength = kwargs['listboxLength']
            del kwargs['listboxLength']
        else:
            self.listboxLength = 8

        # Custom matches function
        if 'matchesFunction' in kwargs:
            self.matchesFunction = kwargs['matchesFunction']
            del kwargs['matchesFunction']
        else:
            def matches(fieldValue, acListEntry):
                pattern = re.compile('.*' + re.escape(fieldValue) + '.*', re.IGNORECASE)
                return re.match(pattern, acListEntry)

            self.matchesFunction = matches


        Entry.__init__(self, *args, **kwargs)
        self.focus()

        self.autocompleteList = autocompleteList

        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.moveUp)
        self.bind("<Down>", self.moveDown)

        self.listboxUp = False

    def changed(self, name, index, mode):
        if self.var.get() == '':
            if self.listboxUp:
                self.listbox.destroy()
                self.listboxUp = False
        else:
            words = self.comparison()
            if words:
                if not self.listboxUp:
                    self.listbox = Listbox(width=self["width"], height=self.listboxLength)
                    self.listbox.bind("<Button-1>", self.selection)
                    self.listbox.bind("<Right>", self.selection)
                    self.listbox.place(x=self.winfo_x(), y=self.winfo_y() + self.winfo_height())
                    self.listboxUp = True

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

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

    def moveUp(self, event):
        if self.listboxUp:
            if self.listbox.curselection() == ():
                index = '0'
            else:
                index = self.listbox.curselection()[0]

            if index != '0':                
                self.listbox.selection_clear(first=index)
                index = str(int(index) - 1)

                self.listbox.see(index) # Scroll!
                self.listbox.selection_set(first=index)
                self.listbox.activate(index)

    def moveDown(self, event):
        if self.listboxUp:
            if self.listbox.curselection() == ():
                index = '0'
            else:
                index = self.listbox.curselection()[0]

            if index != END:                        
                self.listbox.selection_clear(first=index)
                index = str(int(index) + 1)

                self.listbox.see(index) # Scroll!
                self.listbox.selection_set(first=index)
                self.listbox.activate(index) 

    def comparison(self):
        return [ w for w in self.autocompleteList if self.matchesFunction(self.var.get(), w) ]

if __name__ == '__main__':
    autocompleteList = [ 'Dora Lyons (7714)', 'Hannah Golden (6010)', 'Walker Burns (9390)', 'Dieter Pearson (6347)', 'Allen Sullivan (9781)', 'Warren Sullivan (3094)', 'Genevieve Mayo (8427)', 'Igor Conner (4740)', 'Ulysses Shepherd (8116)', 'Imogene Bullock (6736)', 'Dominique Sanchez (949)', 'Sean Robinson (3784)', 'Diana Greer (2385)', 'Arsenio Conrad (2891)', 'Sophia Rowland (5713)', 'Garrett Lindsay (5760)', 'Lacy Henry (4350)', 'Tanek Conley (9054)', 'Octavia Michael (5040)', 'Kimberly Chan (1989)', 'Melodie Wooten (7753)', 'Winter Beard (3896)', 'Callum Schultz (7762)', 'Prescott Silva (3736)', 'Adena Crane (6684)', 'Ocean Schroeder (2354)', 'Aspen Blevins (8588)', 'Allegra Gould (7323)', 'Penelope Aguirre (7639)', 'Deanna Norman (1963)', 'Herman Mcintosh (1776)', 'August Hansen (547)', 'Oscar Sanford (2333)', 'Guy Vincent (1656)', 'Indigo Frye (3236)', 'Angelica Vargas (1697)', 'Bevis Blair (4354)', 'Trevor Wilkinson (7067)', 'Kameko Lloyd (2660)', 'Giselle Gaines (9103)', 'Phyllis Bowers (6661)', 'Patrick Rowe (2615)', 'Cheyenne Manning (1743)', 'Jolie Carney (6741)', 'Joel Faulkner (6224)', 'Anika Bennett (9298)', 'Clayton Cherry (3687)', 'Shellie Stevenson (6100)', 'Marah Odonnell (3115)', 'Quintessa Wallace (5241)', 'Jayme Ramsey (8337)', 'Kyle Collier (8284)', 'Jameson Doyle (9258)', 'Rigel Blake (2124)', 'Joan Smith (3633)', 'Autumn Osborne (5180)', 'Renee Randolph (3100)', 'Fallon England (6976)', 'Fallon Jefferson (6807)', 'Kevyn Koch (9429)', 'Paki Mckay (504)', 'Connor Pitts (1966)', 'Rebecca Coffey (4975)', 'Jordan Morrow (1772)', 'Teegan Snider (5808)', 'Tatyana Cunningham (7691)', 'Owen Holloway (6814)', 'Desiree Delaney (272)', 'Armand Snider (8511)', 'Wallace Molina (4302)', 'Amela Walker (1637)', 'Denton Tillman (201)', 'Bruno Acevedo (7684)', 'Slade Hebert (5945)', 'Elmo Watkins (9282)', 'Oleg Copeland (8013)', 'Vladimir Taylor (3846)', 'Sierra Coffey (7052)', 'Holmes Scott (8907)', 'Evelyn Charles (8528)', 'Steel Cooke (5173)', 'Roth Barrett (7977)', 'Justina Slater (3865)', 'Mara Andrews (3113)', 'Ulla Skinner (9342)', 'Reece Lawrence (6074)', 'Violet Clay (6516)', 'Ainsley Mcintyre (6610)', 'Chanda Pugh (9853)', 'Brody Rosales (2662)', 'Serena Rivas (7156)', 'Henry Lang (4439)', 'Clark Olson (636)', 'Tashya Cotton (5795)', 'Kim Matthews (2774)', 'Leilani Good (5360)', 'Deirdre Lindsey (5829)', 'Macy Fields (268)', 'Daniel Parrish (1166)', 'Talon Winters (8469)' ]
    def matches(fieldValue, acListEntry):
        pattern = re.compile(re.escape(fieldValue) + '.*', re.IGNORECASE)
        return re.match(pattern, acListEntry)

    root = Tk()
    entry = AutocompleteEntry(autocompleteList, root, listboxLength=6, width=32, matchesFunction=matches)
    entry.grid(row=0, column=0)    
    Button(text='Python').grid(column=0)

    root.mainloop()
...