Python Forum
Autocomplete list of tuples data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Autocomplete list of tuples data
#1
The code below works fine if the list (=test_list) is defined as test_list=['aaa','xxxxxx','a1a1a11',.....].
When the test_list gets values from an sql table (lines 41-46), there is TypeError: descriptor 'lower' requires a 'str' object but received a 'tuple'.
Instead of the code line #27 (-> data = sorted(data, key=str.lower)), i tried sth like: data = sorted(data, key=lambda x: x[0]), data = sorted(data, key=lambda x: (x[0].lower())) but i can't find any solution.
The fetchall data has the format: [('FCZ204613AR',), ('FCZ204613AS',), ('FCZ204613AT',), ('FCZ204613AV',), ('FCZ204613AY',),....]

import tkinter as tk
import cx_Oracle

def on_keyrelease(event):

    # get text from entry
    value = event.widget.get()
    value = value.strip().lower()

    # get data from test_list
    if value == '':
        data = test_list
    else:
        data = []
        for item in test_list:
            if value in item.lower():
                data.append(item)                

    # update data in listbox
    listbox_update(data)

def listbox_update(data):
    # delete previous data
    listbox.delete(0, 'end')

    # sorting data 
    data = sorted(data, key=str.lower)

    # put new data
    for item in data:
        listbox.insert('end', item)

def on_select(event):
    # display element selected on list
    print('(event) previous:', event.widget.get('active'))
    print('(event)  current:', event.widget.get(event.widget.curselection()))
    print('---')

# --- main ---

connstr='SOLVATIO/SOLVATIO@localhost'
conn = cx_Oracle.connect(connstr)
cursw1 = conn.cursor()
cursw1.execute("select SERIAL_NUMBER  from customer_MCM")
result1=cursw1.fetchall()
test_list=list(result1)

root = tk.Tk()

entry = tk.Entry(root)
entry.pack()
entry.bind('<KeyRelease>', on_keyrelease)

listbox = tk.Listbox(root)
listbox.pack()
#listbox.bind('<Double-Button-1>', on_select)
listbox.bind('<<ListboxSelect>>', on_select)
listbox_update(test_list)

root.mainloop()
Reply
#2
when you fetchall() you get list of single element tuples. just convert this to a single tupple or list, i.e. change line#46 to
test_list=[item[0] for item in result1]
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
It works. Thanks a lot!!!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Autocomplete tkinter/notebook dimvord 0 4,750 Jul-16-2018, 12:35 PM
Last Post: dimvord

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020