I am trying to learn how to save the data typed into a few entry fields into a text file but I'm obviously missing something. I created a Print button to print these variables so I could test but only get PY_VAR0, PY_VAR1, and PY_VAR2. What am I missing? Why are the firstName, lastName and userAge variables not passing to the functions?
Thanks in advance :-)
This is my code:
from tkinter import *
from tkinter import ttk
# Function to exit app **********************************************************
def callbackExit():
root.destroy()
# End of Function to exit app ***************************************************
# Function to save entries to file **********************************************
def callbackSaveToFile():
fileName = 'data.txt'
target = open (fileName, 'w')
target.write(firstName)
target.write(lastName)
target.write(userAge)
target.close()
# End of Function to save entries ***********************************************
def testPrint():
print(firstName)
print(lastName)
print(userAge)
root = Tk()
root.config(height = 480, width = 640)
root.title('Name and Age App v.1.0')
root.resizable(False, False)
firstName = StringVar()
lastName = StringVar()
userAge = IntVar()
ttk.Label(root, text = 'First Name: ').place(x=20, y=20)
ttk.Entry(root, width = 30, textvariable = firstName).place(x=115, y=20)
ttk.Label(root, text = 'Last Name: ').place(x=20, y=50)
ttk.Entry(root, width = 30, textvariable=lastName).place(x=115, y=50)
ttk.Label(root, text = 'Age: ').place(x=20, y=80)
ttk.Entry(root, width = 3, textvariable=userAge).place(x=115, y=80)
buttonExitApp = ttk.Button(root, text = 'Exit', command=callbackExit).place(x=500, y=350)
buttonSaveData = ttk.Button(root,text = 'Save', command=callbackSaveToFile).place(x=400, y=350)
buttonTestPrint = ttk.Button(root, text = 'Print', command=testPrint).place(x=20, y=350)
root.mainloop()
