Mar-08-2019, 03:21 AM
I had an issue with the results of my database query displaying curly brackets at the beginning and end of the results when it populated a tkinter widget. I found that if I change the insert string from output.insert(0,each_result) to output.insert(0,each_result[0]) it resolved the issue but it has raised a new issue.
The widget populated with the data now displays properly without the {} brackets but I can no longer click an item in that list displayed and run the query for that item.
The call to query the database for a selection made from the OptionMenu:
The binding for the displayed list to be clicked:
This is the getRecipe procedure run when an item is clicked:
Any advice and/or a point to documents that can explain why this is happening and how I can make the items in the displayed list clickable again without having the show the curly brackets would be appreciated.
The widget populated with the data now displays properly without the {} brackets but I can no longer click an item in that list displayed and run the query for that item.
The call to query the database for a selection made from the OptionMenu:
button = tkinter.Button(window_2, text="Load Category", font=('Times 9 bold'), bg = "#F9F8D6", fg = "#9A0615", command=select) button.packThe query for the selection made from the OptionMenu:
def select(): try: output.delete(0, END) selection = var.get() c.execute('SELECT Recipe FROM Recipes WHERE Dish_Type = :selection ORDER BY Recipe DESC', {"selection": selection}) results = c.fetchall() for each_result in results: output.insert(0,each_result[0]) except: messagebox.showerror("DataBase Error", "Failed to load category")The results of this query works properly and I no longer have the curly brackets showing the beginning and end of each result in the list that is displayed.(adding the [0] in the insert line resolved that)
The binding for the displayed list to be clicked:
output = tkinter.Listbox(window_2, font=('Times 9'), height = 20, width=40, bd=0, bg = "#FFD599", fg = '#9A0615', selectmode=SINGLE) output.pack() output.place(x=210, y=200) yscroll = tkinter.Scrollbar(command=output.yview, orient=tkinter.VERTICAL) yscroll.place(x=450, y=200) output.configure(yscrollcommand=yscroll.set) output.bind('<ButtonRelease-1>', getRecipe)The procedure getRecipe to query the item in the displayed list when clicked:
This is the getRecipe procedure run when an item is clicked:
try: dish_type_entry.delete(0,END) index = output.curselection()[0] selected = output.get(index) c.execute('SELECT Dish_Type FROM Recipes WHERE Recipe = :selected', {"selected": selected[0]}) dish_type_results = c.fetchone() dish_type_entry.insert(0,dish_type_results) recipe_entry.delete(0,END) index = output.curselection()[0] selected = output.get(index) c.execute('SELECT Recipe FROM Recipes WHERE Recipe = :selected', {"selected": selected[0]}) recipe_results = c.fetchone() recipe_entry.insert(0,recipe_results[0]) serves_entry.delete(0,END) index = output.curselection()[0] selected = output.get(index) c.execute('SELECT Serves FROM Recipes WHERE Recipe = :selected', {"selected": selected[0]}) serves_results = c.fetchone()\serves_entry.insert(0,serves_results[0]) cook_time_entry.delete(0,END) index = output.curselection()[0] selected = output.get(index) c.execute('SELECT Cook_Time FROM Recipes WHERE Recipe = :selected', {"selected": selected[0]}) cook_time_results = c.fetchone() cook_time_entry.insert(0,cook_time_results[0]) index = output.curselection()[0] selected = output.get(index) c.execute('SELECT Ingredients FROM Recipes WHERE Recipe = :selected', {"selected": selected[0]}) ingredients_results = c.fetchone() ingredients.insert(1.0,ingredients_results[0]) ingredients.config(wrap=WORD) index = output.curselection()[0] selected = output.get(index) c.execute('SELECT Instructions FROM Recipes WHERE Recipe = :selected', {"selected": selected[0]}) instructions_results = c.fetchone() instructions.insert(1.0, instructions_results[0]) instructions.config(wrap=WORD) except: messagebox.showerror("DataBase Error", "Failed to load category")This no longer populates the widgets and draws the error messagebox. This started when I added the [0] to the insert line of the for the selection made from the OptionMenu. (the second snip of code above)
Any advice and/or a point to documents that can explain why this is happening and how I can make the items in the displayed list clickable again without having the show the curly brackets would be appreciated.