() takes 0 positional arguments but 1 was given"?-6ren"> () takes 0 positional arguments but 1 was given"?-def changedir(listbox): choice = varus.get() os.getcwd() os.chdir("C:/Users/haris/" + ch-6ren">
gpt4 book ai didi

python - 为什么在我给出位置参数时 lambda 返回 "() takes 0 positional arguments but 1 was given"?

转载 作者:行者123 更新时间:2023-12-05 09:29:40 25 4
gpt4 key购买 nike

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")

所以这是我在我的 tkinter 项目中使用的功能/命令,但出于某种原因,每当我单击下拉菜单时:

dropdown = tk.OptionMenu(window,
varus,
*certifiedpoop,
command=lambda:changedir(list))

我总是遇到这个错误:

"<lambda>() takes 0 positional arguments but 1 was given" 

这是怎么回事???

最佳答案

OptionMenucommand 选项的回调需要一个参数,即所选值,因此您需要为lambda 提供一个参数。

您还可以将此参数传递给 changedir(),这样您就不需要在其中调用 varus.get():

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))

关于python - 为什么在我给出位置参数时 lambda 返回 "<lambda>() takes 0 positional arguments but 1 was given"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70527305/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com