# -*- 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)
留下了几个,如果这里出去brevitys缘故,而是与几个不同的战役同样的事情,命名按钮A,B,C,d,E,F等。nameerror:全局名称不Tkinter的定义
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
然后任何按钮忘记()一前一后未定义的内容不会被清除。
环顾了类似的问题后,我发现将变量定义为某些东西,即使所有的if语句都不是真且所有的按钮都没有创建,变量至少也有一个值。没有工作,我只想得到
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)
有了这个,当我有多个按钮/战斗在地图上,如果我只要点击它们中的任何一个就可以清除地图。同样的错误被抛出,但至少地图被清除。 我试着做这个工作,但我拉我的头发,感谢任何帮助!
为什么你试图调用一个方法上可能会或可能不会被绑定到名称的对象? – 2014-11-20 22:19:29
你能在代码块中修复'global a'的缩进吗请 – W1ll1amvl 2014-11-20 22:31:57