Python Forum

Full Version: Test program returns int (I want int) and real one returns tuple (not wanted)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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)

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.
What is itembox? I see it used in line 5 of your real program but I don't see where it is assigned anything. Is it a Listbox? If I inserted print(type(itembox)) right after line 4, what would it print? My guess is it would not say Listbox.
It's also weird (rather, unnecessary) to call int on the return value of a method called size - a size on some kind of container object should be an integer. What else could it possibly be?
I move it to GUI section because it's more Tkinter question - what is returned by ListBox.size() rather than anything else. Also, we don't see where itembox is defined, i.e. we don't know is it listbox indeed
deanhystad, thanks for the reply. This is not the full program. itembox is defined as a listbox further back. Strangely, in the test program, ListBox.size() returns integer (needed) and the real one returns tuple (bad).

Would you like to see the full code?
Why don't you try the test I suggest first. Before calling itembox.size() do print(itembox) or print(type(itembox)) and see if itembox is what you think it is. That is how I would go about debugging something like this. First I try to pin down what problem I am trying to solve and usually the solution is obvious once the problem is pinned down.
It's not itembox's type I'm worried about. It's what itembox.size() is returning. itembox.size()'s type is indepenent.
(Mar-25-2020, 05:21 PM)chesschaser Wrote: [ -> ]It's not itembox's type I'm worried about.

You should be worried. ListBox.size() returns int as you example code show. It's pretty clear itembox is NOT ListBox.

Also please, post full traceback produced by your real code, not something else, e.g. your traceback refer to line 1 and line that is just int(itembox.size()). Full code would be helpful too
You are suffering a common form of blindness that is the biggest hindrance to debugging. You assume your code is doing what you expect so you can only see the error reported in the traceback. For me at least the error reported in the traceback is not the real error, but some side effect associated with the real error. Once you fix the itembox not being a ListBox error the itembox.size() will work correctly.
Full code is too big to fit in post

I could give you the code in chunks.
Pages: 1 2