Search code examples
python-3.xerror-handlingtkinter-entry

TypeError: index() takes 2 positional arguments but 3 were given


I'm unable to understand what triggers the below Error.

This is the error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File line 26, in print_sel
    dob.index(INSERT, cal.selection_get())
TypeError: index() takes 2 positional arguments but 3 were given

This is my code:

def calendar():
    def print_sel():
        dob.index(INSERT, cal.selection_get())

    top = Toplevel(root)
    top.title("Select Registration Date")

    cal = Calendar(top, font="Arial 14", selectmode='day', locale='en_US',
                   cursor="hand1", year=2019, month=4, day=4)

    cal.pack(fill="both", expand=True)

    Button(top, text="ok", command=print_sel).pack()


dob=Entry(Registration_Frame,style='TEntry')
dob.grid(row=3,column=1,columnspan=2,sticky=NSEW)

Button(Registration_Frame, text='Select',command=calendar,width=5,style='TButton').grid(row=3,column=3)


Solution

  • You confused the .insert() method with index(); the latter only takes a single argument, and would move longer text within the entry box to show the character at the given index to be the left-most visible character.

    Just replace .index() with .insert():

    dob.insert(INSERT, cal.selection_get())