1

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()

Results of Print button

3 Answers 3

1

With textvariable objects it's important to use the class set and get methods. A good example can be found here:

http://effbot.org/tkinterbook/entry.htm

I believe if you call

target.write(firstname.get())

and similar for your other variables your code should work as expected.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a bunch!! Works like a charm :-)
1

I recommend against using the textvariable option. In most cases it requires you to manage additional object that you simply don't need.

Solution without textvariable

Entry widgets have a get() method which can fetch the value. This works exactly the same as the get() method on the StringVar() variables, but works with an object you already have.

However, this requires that you save a reference to the entry widgets, so the first step is to change how you create the widgets:

firstNameLabel = ttk.Label(root, text = 'First Name: ')
firstNameEntry = ttk.Entry(root, width = 30, textvariable = firstName)
lastNameLabel = ttk.Label(root, text = 'Last Name: ')
lastNameEntry = ttk.Entry(root, width = 30, textvariable=lastName)
userAgeLabel = ttk.Label(root, text = 'Age: ')
userAgeEntry = ttk.Entry(root, width = 3, textvariable=userAge)

firstNameLabel.place(x=20, y=20)
firstNameEntry.place(x=115, y=20)
lastNameLabel.place(x=20, y=50)
lastNameEntry.place(x=115, y=50)
userAgeLabel.place(x=20, y=80)
userAgeEntry.place(x=115, y=80)

Separating widget creation from widget layout is a best practice, and makes it much easier to visualize and modify your layout.

(as an aside, I strongly encourage you to learn pack and grid. They require much less work to use, and generally result in UIs that respond better to resizing and different fonts on different systems.)

Next, you can call the get method of your Entry widgets:

def testPrint():
    print(firstNameEntry.get())
    print(lastNameEntry.get())
    print(userAgeEntry.get())

Solution using textvariables

If you want to use textvariables, the solution is similar. Instead of accessing the variable values directly (which is why you are getting PY_VAR0, etc), you must call the get() method on the variables:

def testPrint():
    print(firstName.get())
    print(lastName.get())
    print(userAge.get())

Comments

0

In def use it like this

def something():
    global var1
    global var2 
    global var3

then you can begin because the dwf are looking for local variables so you will have to make it global for the def and you will have to do this with every def you have

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.