_tkinter.TclError: bad listbox index "": must be active, anchor, end, @x,y, or a number


from tkinter import *
window_main = Tk()

def window_open():
    def chgColor(event):
    lst["bg"] = lst.get(lst.curselection())

    window_public = Tk()
    window_public.title("New Page")
    lbl = Label(window_public, text = "Today is a good day and I'm a gooie!")
    lbl.grid(padx = 20, pady = 10)
    var = StringVar()
    lst = Listbox(window_public, height = 5, width = 10, listvariable = var)
    lst.grid(padx = 50, pady = 20)
    list = ["red", "blue", "yellow", "pink", "green", "white", "black"]
    lst.bind("<<ListboxSelect>>", chgColor)

window_main_btn = Button(window_main, text = "To New Page", command = window_open)
window_main_btn.grid(padx = 50, pady = 20)


Traceback (most recent call last):
  File "C:ComputerSoftwarepython3.6libtkinter__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:/Users/Shawn/PycharmProjects/gammaC@meL/gamma1.py", line 7, in chgColor
    lst["bg"] = lst.get(lst.curselection())
  File "C:ComputerSoftwarepython3.6libtkinter__init__.py", line 2792, in get
    return self.tk.call(self._w, 'get', first)
_tkinter.TclError: bad listbox index "": must be active, anchor, end, @x,y, or a number

I’m a beginner in python and got stuck with a problem. When I click on the button I go to a new page. Then when I click on listbox I get a traceback. I can’t understand the traceback’s meaning and can’t fix it.
But I found out that when I delete codes about window_main, it runs.

Asked By: Shawn Yao



I’m not an expert on listboxes, but I think the list isn’t populated. You need to insert your data into the list. Instead of var.set(tuple(list)), use

for c in list:

If I change this then it works for me.

Answered By: ViG

I am no Tkinter expert, and I know this isn’t a multiple windows question, but you claim that you click a button and get on a new page so your issue might be related to what I have to say.

I seem to remember something about having a single application window and then populating that window with a new frame on user actions and events. Each frame would be a class, and then you would have some kind of loop that allows you to “refresh” the windows content with a new frame.

When I found this guy and his videos on youtube it really helped me to learn Tkinter and overcome the newbie GUI programming issues (like multiple windows). Follow his playlist and you will be sorted in no time. It also wouldn’t hurt to show your support for the guy as well… so like his video or subscribe to his channel.

Please include the Traceback error you get in your post. This will be very helpful to anybody trying to debug your code. The Tkinter Gods might also be able to tell you exactly where you went wrong. Good luck!

EDIT: it seems like ViG is onto it.

Answered By: student23
  • insert the data first replace :

for index, element in enumerate(list):

  • you had it before like this:

      def chgColor(event):
          lst["bg"] = lst.get(lst.curselection())
  • now you should have it like this:

    def chgColor(event):
        if lst.curselection() != ():
           lst["bg"] = lst.get(lst.curselection())


that error occurs when the lst.get recieve an empty tuple.

Answered By: carlosdanielh

you can try this :

import _tkinter


    lst["bg"] = lst.get(lst.curselection())

except _tkinter.TclError:

Answered By: poorya mohammadi
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.