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


Messages In This Thread
Test program returns int (I want int) and real one returns tuple (not wanted) - by chesschaser - Mar-24-2020, 09:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Entry returns NONE mollerpunker 2 2,482 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