Mar-24-2020, 09:55 PM
Hi everyone!
I'm having a problem coding a search function. I want to search a tkinter Listbox in Python 3 on Windows. I use itembox.size() to get the amount of items in the Listbox. I need it to return an Integer so in a test program this is what happened. But when I used the same method in the real program it returns a tuple therefore doesn't work.
Test program (returns int - good)
I'm having a problem coding a search function. I want to search a tkinter Listbox in Python 3 on Windows. I use itembox.size() to get the amount of items in the Listbox. I need it to return an Integer so in a test program this is what happened. But when I used the same method in the real program it returns a tuple therefore doesn't work.
Test program (returns int - good)
from tkinter import * root = Tk() lb = Listbox() lb.insert(1, "foo") lb.insert(2, "bar") lb.pack() elements = int(lb.size()) for i in range(0, elements): print("Worked!")The search part of the real program (returns tuple which prevents it from working)
class SearchWin: def searchList(self): content = self.searchTerms.get() next_index = 0 elements = int(itembox.size()) for i in range(0, elements): if content in itembox.get(next_index): itembox.activate(next_index) break else: if next_index <= elements: next_index += 1 continue else: messagebox.showerror("Item not found", "No item could be found containing " + "'" + content + "'") def __init__(self): searchPrompt = Tk() searchPrompt.title("Search list") searchPrompt.iconbitmap("feather.ico") searchPrompt.resizable(0, 0) searchText = Label(searchPrompt, text = "Search terms") searchText.pack() Label(searchPrompt).pack() self.searchTerms = Entry(searchPrompt, width = 30) self.searchTerms.pack(padx = 10) Label(searchPrompt).pack() searchbtn = Button(searchPrompt, text = "Search", command = self.searchList) searchbtn.pack()The error in the real program (I know what this means)
Error:Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
int(itembox.size())
TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'
I would be grateful if anyone can help.