Home > Net >  Why am I getting a type error from this function?
Why am I getting a type error from this function?

Time:08-08

I am trying to create a drop down menu in tkinter that allows the user to select a machine, or row # from the excel sheet, and then all the data from that entire row is displayed in tkinter through the display_selected function. I keep getting this error. This is my error:

 TypeError: line_7_window.<locals>.display_selected() takes 0 positional arguments but 1 was given

This is my code:

def get_machine():
    for row in sheet.iter_rows(min_row=2, max_col=1, max_row=60):
        for cell in row:
            if cell.value == inputmachine:
                machineindex = cell
                return machineindex.row

def get_machineattributes():
    for col in sheet.iter_cols(min_row = (get_machine()), max_col = 15, max_row = (get_machine())):
        for cell in col:
            return (cell.value)

def display_selected():   
    data = Message(line_7, text=get_machineattributes())
    data.pack()
    data.place(x=650, y=30)

    copy = Button(line_7, text="Copy to Clipboard", command=pyperclip.copy(line7_choice))
    copy.pack()
    copy.place(x=550, y=45)
    return


inputmachine = StringVar(line_7)
inputmachine.set("Click to select a machine")
dropdown = OptionMenu(line_7, inputmachine, *lst, command=display_selected)
dropdown.pack()
dropdown.place(x=670, y=25)

I have tried everything and I cant figure out why this would not work.

CodePudding user response:

Your callback function display_selected, specified when creating the option menu, will receive the actual option chosen from Tk, so you need that parameter when defining it, even if you do nothing with it.

In other words, use something like:

   def display_selected(choice):
       del choice  # not used
       # rest of function

As an aside, I suspect you may be better off with an option menu to select the item, and a separate button to act on that selection. That would allow you to confirm the selected item before performing any action, in case you inadvertently choose the wrong one.

  • Related