def changedir(listbox):
choice = varus.get()
os.getcwd()
os.chdir("C:/Users/haris/" choice)
directory = os.listdir("C:/Users/haris/" choice)
listbox.delete()
directory = sorted(((os.stat(line).st_mtime, line) for line in directory if line.endswith('.zip')), reverse=1)
for unixtime, line in directory:
kws = datetime.fromtimestamp(unixtime)
listbox.insert(tk.END, line, 'Date modified:', kws.date(), "\n")
So this is the function/command i am using for my tkinter project but for some reason, whenever I click the dropdown menu:
dropdown = tk.OptionMenu(window,
varus,
*certifiedpoop,
command=lambda:changedir(list))
I am always left with this error:
"<lambda>() takes 0 positional arguments but 1 was given"
What is going on???
CodePudding user response:
The callback of command
option of OptionMenu
expects an argument, the selected value, so you need to have an argument for the lambda
.
Also you can pass this argument to changedir()
so that you don't need to call varus.get()
inside it:
def changedir(listbox, choice):
os.getcwd()
os.chdir("C:/Users/haris/" choice)
directory = os.listdir("C:/Users/haris/" choice)
#listbox.delete()
listbox.delete(0, 'end')
directory = sorted(((os.stat(line).st_mtime, line) for line in directory if line.endswith('.zip')), reverse=1)
for unixtime, line in directory:
kws = datetime.fromtimestamp(unixtime)
listbox.insert(tk.END, line, 'Date modified:', kws.date(), "\n")
...
dropdown = tk.OptionMenu(window,
varus,
*certifiedpoop,
command=lambda v:changedir(list, v))
CodePudding user response:
The error you're seeing is hinting that the lambda you pass as the command
argument when building an OptionMenu
widget takes an argument that is the selected item. You can choose to ignore it if you want, but it still needs to accept that argument.
For instance:
dropdown = tk.OptionMenu(window,
varus,
*certifiedpoop,
command=lambda selected: changedir(list))
CodePudding user response:
tk.OptionMenu
passes the new value as a parameter to the function specified by command
.
You can ignore it, but you must include it in the lambda definition: lambda x: changedir(list))