gpt4 book ai didi

Why is the passed argument "not defined" in the exec(), even though the argument is defined in the same function?(为什么传递的参数在exec()中“没有定义”,即使该参数是在同一个函数中定义的?)

转载 作者:bug小助手 更新时间:2023-10-25 22:50:40 27 4
gpt4 key购买 nike



I'm trying to dynamically create pages when each button is pressed. In the following code I pass the canvas of the current page into the next function so that it can be deleted ( preventing multiple pages open at the same time), but I'm getting an error: "File "", line 1, in NameError: name 'menu_canvas' is not defined".

我尝试在按下每个按钮时动态创建页面。在下面的代码中,我将当前页面的画布传递给下一个函数,以便可以将其删除(防止同时打开多个页面),但我收到了一个错误:“文件”,NameError中的第1行:名称‘Menu_Canvas’is Not Defined“。


def gtpath_menu(canvas):

menu_canvas = Canvas(gui, width=1480, height=920)
menu_canvas.pack(fill="both", expand=True)
path_create_button = Button(gui, text="Go To Path Creation!", font=("Helvetica"), bg="white", padx=10, pady=10, border=5, command=lambda:path_create_menu(menu_canvas))
with open("Paths.csv",newline='') as save_file:
reader = csv.DictReader(save_file)
read_list = []
for row in reader:
read_list.append(row)

path_name = ""
path_list = []
for dict in read_list:
if path_name != dict.get("Path Name"):
path_name = dict.get("Path Name")
path_list.append(path_name)
else:
continue
#button creation
x = 0
while x < len(path_list): #PROBLEM IS HERE
exec("button{}=Button(gui, text=path_list[x], font=(\"Helvetica\"), bg=\"white\", padx=10, pady=10, border=5,command=lambda:path_page(menu_canvas))" .format(x))
exec("menu_canvas.create_window(100, (300 + ({}*100)), anchor=\"nw\", window=button{})".format(x, x))
x += 1
menu_canvas.create_image(0,0, image=path_img, anchor='nw')
menu_canvas.create_text(100, 200, text="Choose Your Path Below", font=("Helvetica",30), fill="white", anchor="nw")

menu_canvas.create_window(600, 200, anchor="nw", window=path_create_button)
canvas.forget()

The button in the above while loop is supposed to call the following function while passing (menu_canvas) as an argument:

上面While循环中的按钮应该在将(Menu_Canvas)作为参数传递时调用以下函数:


def path_page(canvas):

canvas.forget()
path_canvas = Canvas(gui, width=1480, height=920)
path_canvas.pack(fill="both", expand=True)
path_canvas.create_image(0,0, image=create_img, anchor ='nw')

I wanted the button (on button press) to call path_page(), which would create a new page with the mentioned picture and size. The code is supposed to dynamically create a page for each new button made.(Buttons are made after reading data from a another save file)

我希望按钮(按下按钮时)调用PATH_PAGE(),它将创建一个具有所述图片和大小的新页面。代码应该为每个新创建的按钮动态创建一个页面。(按钮是在从另一个保存文件读取数据后创建的)


更多回答

This might be a case of How do I create variable variables?. Why use exec in the first place, rather than a list or dict of buttons?

这可能是我如何创建变量变量的问题?为什么一开始就使用exec,而不是按钮列表或字典?

exec is a newbie trap. It's got a minefield of weird limitations and undefined behavior that make it tricky to use even for experienced programmers, but it looks like it does exactly what newbies think they need. Pretend it doesn't exist - there is almost always a better tool for the job.

exec是新手陷阱。它有一个奇怪的限制和未定义的行为的雷区,即使是有经验的程序员也很难使用它,但它看起来确实像新手认为他们需要的那样。假装它不存在-几乎总是有一个更好的工具的工作。

@user2357112 Good point, I'll try using dictionaries instead. Like you said, at first I thought exec() was pretty straight forward but after looking into it, there are a lot of limitations to it.

@user2357112说得好,我会试着用字典代替。就像您说的,一开始我认为exec()非常简单,但在研究它之后,它有很多局限性。

@ggorlen I'll look into that, thanks! I wasn't sure how to use dictionaries for this because like I said I wanted to dynamically create pages for each button. The buttons are made based on user input, so the number of buttons isn't predefined

@ggorlen我会调查的,谢谢!我不确定如何使用词典来实现这一点,因为就像我说的,我想为每个按钮动态创建页面。这些按钮是基于用户输入制作的,因此按钮的数量不是预定义的

Use while true: instead of while x < len(path_list):

使用WHILE TRUE:代替WHILE x

优秀答案推荐
更多回答

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