Jim Jim - 1 year ago 49
Python Question

Is there a "contains" method for the Tkinter Listbox widget

Is there a

listbox.contains()
for python Tkinter similiar to the C# method?

I know the answer is probably NO but I need to check if the first 6 chars in a listbox item exist,

Answer Source

No. However, all the basic functionality is there for you to write your own. It would look something like this:

class CustomListbox(tk.Listbox):
    def __contains__(self, str):
        return str in self.get(0, "end")

lb = CustomListbox(...)
...
lb.insert("end", "foo")
...
if "foo" in lb:
    print "success"

Or, using a lambda to attach the function to a standard listbox:

lb = tk.Listbox(...)
lb.__contains__ = lambda str: str in lb.get(0, "end")
...
lb.insert("end", "foo")
...
if "foo" in lb:
    print "success"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download