-1

I am making a simple program that saves someone's name and their email address in a file. I am coding in python idle. My code is:

import random
from tkinter import *

num = (random.randint(1,3))
NOSa = open("CN.txt", "r")
NOS = (NOSa.read())
NOSa.close()
NOSa = open("CN.txt", "w")
if num == ("1"):
    NOS = NOS + "a"
elif num == ("2"):
    NOS = NOS + "v"
else:
    NOS = NOS + "x"
NOSa.write(NOS)
NOSa.close()

def efg():
        window2.destroy()
        window2.mainloop()
        exit()

def abc():
    name = entry.get()
    email = entry2.get()
    window.destroy()
    window2 = Tk()
    window2.title("OP")
    OT = Text(window2, width=30, height=10, wrap=WORD, background="yellow")
    OT.grid(row=0, column=0, sticky=W)
    OT.delete(0.0, END)
    MT = "We have logged your details " + name
    MT2 = ". We have a file saved called " + NOS
    MT3 = ". Go check it out!"
    OT.insert(END, MT)
    OT.insert(END, MT2)
    OT.insert(END, MT3)
    new_file = open(NOS, "a")
    new_file.write("This is ")
    new_file.write(name)
    new_file.write(" email address.")
    new_file.write(" ")
    new_file.write(email)
    button2 = Button(window2, text="OK", width=5, command=efg)
    button2.grid(row=1, column=0, sticky=W)
    window.mainloop()
    window2.mainloop()

window = Tk()
window.title("EN")
label = Label(window, text="Enter your name: ")
label.grid(row=0, column=0, sticky=W)
entry = Entry(window, width=20, bg="light green")
entry.grid(row=1, column=0, sticky=W)
label2 = Label(window, text="Enter your email address: ")
label2.grid(row=2, column=0, sticky=W)
entry2 = Entry(window, width=25, bg="light green")
entry2.grid(row=3, column=0, sticky=W)
button = Button(window, text="SUBMIT", width=5, command=abc)
button.grid(row=4, column=0, sticky=W)
window.mainloop()

When I run the code my first 2 boxes appear and run their code perfectly. However, when I click the button 'OK' I get this error: NameError: name 'window2' is not defined

I use python-idle.

Thomas
  • 1,214
  • 4
  • 18
  • 45
  • 3
    `window2` is a local variable, only visible in the function where it's defined. – Bryan Oakley Mar 06 '20 at 20:25
  • What do you want to achieve with `efg` anyway? – Odysseus Mar 06 '20 at 20:39
  • Well you have multiple mainloops in your code. There should only be one instance of `Tk()` and one instance of `mainloop`. If you want to open a 2nd window then use `Toplevel`. – Mike - SMT Mar 06 '20 at 21:37
  • NameErrors have nothing to do with the editor or IDE you use to edit and run your code, not with the GUI framework. They arise either from typos or scope issues. (And others have pointed to the latter for your question.) – Terry Jan Reedy Mar 07 '20 at 00:50
  • Read Read up on [scopes-and-namespaces](https://docs.python.org/3/tutorial/classes.html#scopes-and-namespaces-example), [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged) and [Tkinter understanding mainloop](https://stackoverflow.com/questions/29158220/tkinter-understanding-mainloop) – stovfl Mar 07 '20 at 12:39

1 Answers1

1

You may make windows2 global

window2 = None

def efg():
        global window2

and later

def abc():
    global window2
Askold Ilvento
  • 1,405
  • 1
  • 17
  • 20