Reputation: 131
I have a part of a code some thing like this:
def topLevel():
bugsendwindows = Toplevel(background="grey")
bugsendwindows.title("report bug")
buglabel1 = Label(bugsendwindows, text='title', background="white")
buglabel1.place(x=10, y=20)
bugtitleentry = Entry(bugsendwindows)
bugtitleentry.place(x=50, y=20)
bugtitleentry.focus_set()
buglabel2 = Label(bugsendwindows, text="email", background="white")
buglabel2.place(x=10, y=60)
bugemailentry = Entry(bugsendwindows)
bugemailentry.place(x=60, y=60)
bugemailentry.focus_set()
buglabel3 = Label(bugsendwindows, text="data", background="white")
buglabel3.place(x=10, y=100)
bugdataentry = Entry(bugsendwindows)
bugdataentry.place(x=60, y=100, height=60)
bugdataentry.focus_set()
def Enter():
global bugtitleentry
global bugemail
global bugdata
bugtitle = bugtitleentry
bugemail = bugemailentry
bugdata = bugdataentry
localtime = time.asctime(time.localtime(time.time()))
bugwrite = open("bugreport", "w")
bugwrite.write("title:")
bugwrite.write(bugtitle, "\n")
bugwrite.write("writer email:")
bugwrite.write(bugemail, "\n")
bugwrite.write("data:")
bugwrite.write(bugdata, "\n")
bugwrite.close()
bugsend = Button(bugsendwindows, text="send",
command=Enter)
bugsend.place(x=10, y=150)
However, everytime I click send, It says something like this:
bugtitle = bugtitleentry
NameError: global name 'bugtitleentry' is not defined
how do i fix it so Enter can write the data to the file from topLevel?
Upvotes: 0
Views: 53
Reputation: 309821
since Enter
is nested inside topLevel
, it will pick up those names from the closure and no global
statement is needed.
# ... <snip>
def Enter():
bugtitle = bugtitleentry
bugemail = bugemailentry
# </snip> ...
As a side note though, I'm not sure that the data writing will work as you expect. You probably need to use the .get()
method on the Entry
objects. e.g.:
bugwrite.write(bugtitle.get() + "\n") # and file.write only takes 1 argument!
Upvotes: 1