Python Forum
Test program returns int (I want int) and real one returns tuple (not wanted)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Test program returns int (I want int) and real one returns tuple (not wanted)
#1
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.
Reply
#2
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.
Reply
#3
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?
Reply
#4
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
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
#5
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?
Reply
#6
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.
Reply
#7
It's not itembox's type I'm worried about. It's what itembox.size() is returning. itembox.size()'s type is indepenent.
Reply
#8
(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
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
#9
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.
Reply
#10
Full code is too big to fit in post

I could give you the code in chunks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Entry returns NONE mollerpunker 2 2,483 Dec-06-2020, 06:50 PM
Last Post: DT2000

Forum Jump:

User Panel Messages

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