Python Forum

Full Version: How to save record
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How to save variable value entered by user from GUI

I made this program How to modified program to store details

python 3 raspbain os

from tkinter import *    
top = Tk()

NameLabel= Label(top, text="Name", relief=RIDGE,width=10, height= 5)
NameLabel.grid(row=2, column=0)

NameEntry = Entry(top)
NameEntry.grid(row=2, column=1)

Button1 = Button(text="Submit", relief=RIDGE,width=10, height= 5)
Button1.grid(row=2, column=3)

AgeLabel = Label(top, text="Age", relief=RIDGE,width=10, height= 5)
AgeLabel.grid(row=3, column=0)

AgeEntry = Entry(top)
AgeEntry.grid(row=3, column=1)

Button2 = Button(text="Submit", relief=RIDGE,width=10, height= 5)
Button2.grid(row=3, column=3)

mainloop()
Add event handlers to your buttons, and then save state in the event handlers. Something like:
def name_submit_handler():
    # save state here
    print("Button was clicked!")

# and then later...
Button1 = Button(text="Submit", command=name_submit_handler, relief=RIDGE, width=10, height=5)
Button1.grid(row=2, column=3)
I think he or she means how do i save the input information. well you could save it a thousand different ways, write to text file.
save it in a data base with pickle or shelve, or email it to a friend, here's how to save it to a basic txt file

from tkinter import *    
top = Tk()
def getinfo():
    ne= NameEntry.get() #get name
    yr= AgeEntry.get()
    scm= open('secretclubmembers.txt', 'a') #create and append
    scm.write('Member %s Age: %s'% (ne,yr))
    scm.close()
    print('info was saved: {0},{1}'.format(ne,yr))
    #clear the entry field
    #clearinfo()
     
         
NameLabel= Label(top, text="Name", relief=RIDGE,width=10, height= 5)
NameLabel.grid(row=2, column=0)
 
NameEntry = Entry(top)
NameEntry.grid(row=2, column=1)
 
Button1 = Button(text="Submit",command=getinfo,
                 relief=RIDGE,width=10, height= 5)
Button1.grid(row=2, column=3)
 
AgeLabel = Label(top, text="Age", relief=RIDGE,width=10, height= 5)
AgeLabel.grid(row=3, column=0)
 
AgeEntry = Entry(top)
AgeEntry.grid(row=3, column=1)
 

 
mainloop()