0
from tkinter import*
from tkinter import messagebox
import os

def accountinfo1():
    a = nameentry.get()
    
    info1 = open("Account #1.txt", "w")
    info1.write(a)
    info1.close()

    messagebox.showinfo("Sign Up Process", "Registration Success") 
def newaccount():
    global newscreen
    newscreen= Tk()

    global nameentry
    nameentry = StringVar()
    
    labelname=Label(newscreen, text='Enter your Name:')
    labelname.pack()
    entername=Entry(newscreen, textvariable=nameentry)
    entername.pack()

    submitbutton = Button(newscreen, text="SUBMIT", command = accountinfo1)
    submitbutton.pack()
    
    newscreen.mainloop()
def main_screen():
    global mscreen
    mscreen=Tk()

    addaccountbutton = Button(mscreen, text="Add Account", width = '20', height = '2', command=newaccount)
    addaccountbutton.pack(padx=50,pady=50)

    mscreen.mainloop()
main_screen()
TheLizzard
  • 7,248
  • 2
  • 11
  • 31
  • 1
    Try changing ` nameentry = StringVar()` to ` nameentry = StringVar(master=newscreen)` or replace the second `Tk()` with `Toplevel()` – TheLizzard Jun 24 '21 at 12:41
  • 1
    Welcome to StackOIverflow! Don't hesitate to look at the [How to ask page](https://stackoverflow.com/help/how-to-ask) of the help center. For instance, that would be better if you could edit your title to make it shorter and move the extra information to the body of the question. – j_4321 Jun 24 '21 at 12:44
  • 1
    Also see [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/questions/48045401/why-are-multiple-instances-of-tk-discouraged) – martineau Jun 24 '21 at 12:46
  • Thank you so much! TheLizzard. It works!❣️GodBless you – Moonlit Jun 24 '21 at 12:53

1 Answers1

0

Your StringVar is unnecessary. What you need to do is to make your entry global, so when you call accountinfo1(), you can get the value of your entry straight away without having to use a StringVar. Also use Toplevel() instead of a second Tk().

from tkinter import messagebox
import os

def accountinfo1():
    a = entername.get()
    
    info1 = open("Account #1.txt", "w")
    print(a)
    info1.write(a)
    info1.close()

    messagebox.showinfo("Sign Up Process", "Registration Success") 
def newaccount():
    global entername
    global newscreen
    newscreen= Tk()

    global nameentry
    
    labelname=Label(newscreen, text='Enter your Name:')
    labelname.pack()
    entername=Entry(newscreen)
    entername.pack()

    submitbutton = Button(newscreen, text="SUBMIT", command = accountinfo1)
    submitbutton.pack()
    
    newscreen.mainloop()
def main_screen():
    global mscreen
    mscreen=Tk()

    addaccountbutton = Button(mscreen, text="Add Account", width = '20', height = '2', command=newaccount)
    addaccountbutton.pack(padx=50,pady=50)

    mscreen.mainloop()
main_screen()
  • The is no point to the `newscreen.mainloop()` line. If you remove it, the code will run the same way. Also what if OP was tracing how the entry was changing using `.trace`? – TheLizzard Jun 24 '21 at 16:28