tkinter callback event to retrieve value from Entry and write to text file

Question:

*First of all, i am trying to create a register system saved into textfile(not real system, i know its not safe to write into textfiles) Created the GUI and then i defined multiple functions which is Menu, register and submit. Submit function is nested and inside register function. The problem is when i nested the functions it doesn’t write into textfiles, but when i delete the register function, it works.
When i press register and write something in the Entry box, it didnt record to the text files, i have been scratching my head as to find what the error in my code is.

Edit: I now have put a picture to get better understanding. the blue window is main menu and its the mainloop and the yellow window is appearing when i click register button.*

picture of what should be my app

from tkinter import *

def Register():

    def Submit():


        elev = open("bruker.txt","a", encoding="utf-8")

        elev.write(brukeridinc.get()+"n")

        elev.write(fornavn.get()+"n")

        elev.write(etternavn.get()+"n")

        



register = Tk()

register.geometry("500x500")

register.configure(bg="yellow")

register.title("Bibliotekapp Login")



label_brukerid = Label(register, text="Brukerid:", bg="white", font=("Arial", 25))

label_brukerid.grid(row=1, column=0, padx=5, pady=5, sticky=E)



label_fornavn = Label(register, text="Fornavn:", bg="white", font=("Arial", 25))

label_fornavn.grid(row=2, column=0, padx=5, pady=5, sticky=E)



label_etternavn = Label(register, text="Etternavn:", bg="white", font=("Arial", 25))

label_etternavn.grid(row=3, column=0, padx=5, pady=5, sticky=E)



brukeridinc= StringVar()

entry_brukerid= Entry(register, width=10, textvariable=brukeridinc, font=("Arial", 25))

entry_brukerid.grid(row=1, column=1, padx=5, pady=5, sticky=W) 



fornavn= StringVar()

entry_fornavn= Entry(register, width=10, textvariable=fornavn, font=("Arial", 25))

entry_fornavn.grid(row=2, column=1, padx=5, pady=5, sticky=W) 



etternavn= StringVar()

entry_etternavn= Entry(register, width=10, textvariable=etternavn, font=("Arial", 25))

entry_etternavn.grid(row=3, column=1, padx=5, pady=5, sticky=W) 



button_resultat= Button(register, text="Enter", command=Submit, height=2, width=15)

button_resultat.grid(row=4, column=1, padx=5, pady=5, sticky=W)



informasjon= StringVar()

entry_informasjon= Entry(register, width=24, textvariable=informasjon, state="readonly", font=("Arial", 25))

entry_informasjon.grid(row=5, column=1, padx=5, pady=5, sticky=W)


#GUI

start = Tk()

start.geometry("500x500")

start.configure(bg="lightblue")

start.title("Bibliotekapp")



button_register = Button(start, text="Register", bg="white", font=("Arial", 25), command=Register)

button_register.grid(row=5, column=5, padx=5, pady=5, sticky=E)










start.mainloop()
Asked By: Svimmel

||

Answers:

I do not understand how you write code. You are using too many duplicates. I will not going to explain to you. In line 51 and 54 , I changed command to None. You can replace it.

Here is code:

from tkinter import *

start = Tk()
start.geometry("800x500")
start.configure(bg="lightblue")
start.title("Bibliotekapp")
 
def menu():
    start.geometry("500x500")
    start.title("Bibliotekapp")
    start.configure(bg="red")

def Register():
    start.geometry("800x800")
    start.configure(bg="yellow")
    start.title("Bibliotekapp Login")
     
def Submit():
    elev = open("bruker.txt", "r")
    sjekk = elev.read()

    if brukeridinc.get() in sjekk:
        informasjon.set("User already exists")

    else:
        elev.close()
        elev = open("bruker.txt", "a")
        elev.write(brukeridinc.get() + "")
        elev.write(fornavn.get() + "")
        elev.write(etternavn.get() + "n")
        elev.close()
        informasjon.set("You are started")


#######    GUI  ############
label_brukerid = Label(start, text="Brukerid:", bg="white", font=("Arial", 25))
label_brukerid.grid(row=1, column=0, padx=5, pady=5, sticky=E)

label_fornavn = Label(start, text="Fornavn:", bg="white", font=("Arial", 25))
label_fornavn.grid(row=2, column=0, padx=5, pady=5, sticky=E)

label_etternavn = Label(start, text="Etternavn:", bg="white", font=("Arial", 25))
label_etternavn.grid(row=3, column=0, padx=5, pady=5, sticky=E)

brukeridinc= StringVar()
entry_brukerid= Entry(start, width=10, textvariable=brukeridinc, font=("Arial", 25))
entry_brukerid.grid(row=1, column=1, padx=5, pady=5, sticky=W) 

fornavn= StringVar()
entry_fornavn= Entry(start, width=10, textvariable=fornavn, font=("Arial", 25))
entry_fornavn.grid(row=2, column=1, padx=5, pady=5, sticky=W) 

etternavn= StringVar()
entry_etternavn= Entry(start, width=10, textvariable=etternavn, font=("Arial", 25))
entry_etternavn.grid(row=3, column=1, padx=5, pady=5, sticky=W) 

button_start = Button(start, text="start", bg="white", font=("Arial", 25), command=Submit)
button_start.grid(row=5, column=5, padx=5, pady=5, sticky=E)

button_login = Button(start, text="Login", bg="white", font=("Arial", 25), command=menu)
button_login.grid(row=6, column=5, padx=5, pady=5, sticky=E)

button_lån = Button(start, text="Lån", bg="white", font=("Arial", 25), command=Register)
button_lån.grid(row=7, column=5, padx=5, pady=5, sticky=E)
 
informasjon= StringVar()
entry_informasjon= Entry(start, width=24, textvariable=informasjon, state="readonly", font=("Arial", 25))
entry_informasjon.grid(row=5, column=1, padx=5, pady=5, sticky=W)
 
start.mainloop()

Result:

enter image description here

Result Red:

enter image description here

Result for yellow:

enter image description here

Answered By: toyota Supra