Home > OS >  Code gives missing 1 required positional argument error
Code gives missing 1 required positional argument error

Time:02-23

This code gives the error:

TypeError: otsi() missing 1 required positional argument: 'x'

The code is supposed to open a .pdf file by a part of its name, which I input to the Tellimus_entry.

Tellimus_entry = Entry(ws)
Tellimus_entry.grid(row=4,column=3,padx=(10, 10), sticky=(N, S, E, W), columnspan=3)


def otsi(x):
    return glob.glob(f'C:/Users/ASUS/Desktop/Proov/*{x}*.pdf')

    otsitav = Tellimus_entry.get()
    files = otsi(otsitav)
    print(files)
    if files:
        os.startfile(files[0])
nupp = ttk.Button(ws, text="Ava tellimuse PDF", command=otsi)
nupp.grid(row=3,column=3, sticky=(N, S, E, W), pady=5, padx=5)

CodePudding user response:

You can pass the command argument as a lambda, this way the x parameter you need wont be missing

Tellimus_entry = Entry(ws)
Tellimus_entry.grid(row=4,column=3,padx=(10, 10), sticky=(N, S, E, W), columnspan=3)


def otsi(x):
    return glob.glob(f'C:/Users/ASUS/Desktop/Proov/*{x}*.pdf')

    otsitav = Tellimus_entry.get()
    files = otsi(otsitav)
    print(files)
    if files:
        os.startfile(files[0])
nupp = ttk.Button(ws, text="Ava tellimuse PDF", command=lambda: otsi(whatever_param_you_need))
nupp.grid(row=3,column=3, sticky=(N, S, E, W), pady=5, padx=5)

CodePudding user response:

Based on your description in your question, actually the argument x of otsi() is not necessary:

def otsi():
    otsitav = Tellimus_entry.get()
    files = glob.glob(f'C:/Users/ASUS/Desktop/Proov/*{otsitav}*.pdf')
    print(files)
    if files:
        os.startfile(files[0])
  • Related