Skip to content
Advertisement

Takes 1 positional argument but 2 were given Python Tkinter

I am working on one feature of my software which takes keyboard which I have bind to the window like this win.bind('<Key>', self.triggerBackButton). And here is the callback funtion.

class Navigator(Validator):
    def __init__(self, win, renameFrameOBJ):
        self.win = win
        self.renameFrameOBJ = renameFrameOBJ
        self.child_window = None

        # Initializing List Frame to display file names
        listFrame = Frame(win, bg='#222222')
        listFrame.grid(row=1, column=0, sticky='nsew')
        Grid.columnconfigure(listFrame, 0, weight=1)

        listFrame.update()

        width = listFrame.winfo_width() // 16
        self.listBox = Listbox(listFrame, width=width, height= 20,justify='center', font=('Flux Regular', 15),
           fg='white', selectbackground='#375a7f', selectforeground='white', bg='#222222', bd = 0,
           activestyle='none', highlightthickness=0)
        self.listBox.grid(ipadx=10, ipady=10)
        
        win.bind('<Key>', self.triggerBackButton)



    def triggerBackButton(self, event):

        if event.char in string.ascii_lowercase:

            files = os.listdir(self.path_history[-1])
            for idx in range(len(files)):
                if files[idx][0].lower() == event.char:
                    self.listBox.curselection(0)
                    self.listBox.activate(idx)
                    break

Logic of above function goes something like this. It takes self and event as input, where event holds the information like from where it was triggered, which button triggered it etc.

If condition validates that does this event was trigger by buttons on keyboard containing value between a-z

self.path_history stores the path of files, so here I am taking the last path from the self.path_history variable and storing all the files present in that path into files variable.

I am iterating over the files list and checking if the first letter of that file is equal to event.char which holds the letter from a-z. If that’s the case then I am trying to access the self.listBox widget and use curselection() method of Listbox widget. BUT I am getting following error. I have used this self.listBox.curselection() inbuild method several times, but this time it’s giving me error

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.8/tkinter/__init__.py", line 1892, in __call__
    return self.func(*args)
  File "/media/hackytech/Local Disk1/data/python/Project/Bunch File Renamer LINUX/packages/navigator.py", line 104, in triggerBackButton
    self.listBox.curselection(0)
TypeError: curselection() takes 1 positional argument but 2 were given

Thank you so much for your help

Advertisement

Answer

At the moment, calling self.listBox.curselection(0) sends two parameters to the method: the reference to the instance of the class Listbox (self.listBox) and the integer value (0). Did you mean self.listBox.curselection() instead?

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement