gpt4 book ai didi

python - 名称错误 : global name is not defined in tkinter

转载 作者:行者123 更新时间:2023-11-30 23:17:57 25 4
gpt4 key购买 nike

# -*- coding: utf-8 -*-
from Tkinter import *
import Image, ImageTk
import test2

root = Tk()
root.wm_title("InterActMap")

def get():


global CountryName
CountryName = e1.get()
global Day
Day = e2.get()
global Month
Month = e3.get()
global Year
Year = e4.get()
Monthint=int(Month)
Dayint=int(Day)
Yearint=int(Year)

if CountryName == "germany" and Monthint == 8 and Dayint == 26 and Yearint == 1914:
global a
a = Button(root, bg = "red", text ="Battle of Tannenberg" , command=None)
a.place(x=691, y=229)
test2.register(a, Tannenberg)

为了简洁起见,在这里留下了一些“如果”,但在一些不同的战斗中,这是同样的事情,将按钮命名为 a、b、c、d、e、f 等。

def forget():
a.place_forget()
b.place_forget()
c.place_forget()
d.place_forget()
f.place_forget()


canvas = Canvas(root, width = 1280, height=720)

country = Label(root, text = "Country")
country.place(x=5, y=5)
e1 = Entry(root)
e1.place(x=60,y=5)

day = Label(root, text = "Day")
day.place(x=230, y=5)
e2 = Entry(root)
e2.place(x=260, y=5)

month = Label(root, text = "Month")
month.place(x=430, y=5)
e3 = Entry(root)
e3.place(x=475, y=5)

year = Label(root, text = "Year")
year.place(x=645, y=5)
e4 = Entry(root)
e4.place(x=680, y=5)



File = "map1.jpg"
img = ImageTk.PhotoImage(Image.open(File))
canvas.create_image(0,0,image=img,anchor="nw")

Button1 = Button(root, text = "Submit", command=get)
Button1.place(x=850, y=5)
Button2 = Button(root, text = "Clear", command=forget)
Button2.place(x=925, y=5)
Button3 = Button(root, text = "Exit", command=root.quit)
Button3.place(x=960, y=5)




canvas.pack()
root.mainloop()

我知道问题是,当我单击“清除”时,所有按钮不一定存在,因此会引发错误...

NameError: global name 'c' is not defined

然后,forget() 中未定义的按钮之后的任何按钮都不会被清除。

在查看了类似的问题后,我发现将变量定义为某种东西,这样即使所有 if 语句都不是 true 并且所有按钮都没有创建,变量至少有一个值。那不起作用,我就会得到

AttributeError: 'NoneType' object has no attribute 'forget'

我找到了一个解决方法,但这并不是我想要的

if CountryName == "germany" and Monthint == 8 and Dayint == 26 and Yearint == 1914:
global a
a = Button(root, bg = "red", text ="Battle of Tannenberg" , command=forget)
a.place(x=691, y=229)
test2.register(a, Tannenberg)

有了这个,当我在 map 上有多个按钮/战斗时,如果我只需单击其中任何一个,就会清除 map 。抛出同样的错误,但至少 map 被清除了。我已经尝试过这项工作,但我正在抓狂,感谢您的帮助!

最佳答案

最好的办法是创建一个包含所有按钮的实例级列表。例如:

button_list = []

if CountryName == "germany" and Monthint == 8 and Dayint == 26 and Yearint == 1914:
a = Button(root, bg = "red", text ="Battle of Tannenberg" , command=None)
a.place(x=691, y=229)
button_list.append(a)

那么你的忘记命令应该是这样的:

def forget(lst_to_clear):
for button in reversed(lst_to_clear):
button.place_forget()
lst_to_clear.pop()
# the reversal is to make sure popping the item out of the
# original list doesn't break the for loop. You could just
# as easily do:
# # while lst_to_clear:
# # lst_to_clear.pop().place_forget()
# but I find it a little less readable.

您的清晰按钮应如下所示:

Button2 = Button(root, text = "Clear", command=lambda: forget(button_list))
Button2.place(x=925, y=5)

关于python - 名称错误 : global name is not defined in tkinter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27050617/

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