user6842389 user6842389 - 1 year ago 85
Python Question

How do I get an entry widget to save what I input? Python Tkinter

I want to make an entry widget that inputs personal details, however I want to save those details as variables, so I can write them in a txt file.

from tkinter import *
root = Tk()
Label(root, text = "Childs First name").grid(row = 0, sticky = W)
Label(root, text = "Childs Surname").grid(row = 1, sticky = W)
Label(root, text = "Childs Year of Birth").grid(row = 2, sticky = W)
Label(root, text = "Childs Month of Birth").grid(row = 3, sticky = W)
Label(root, text = "Childs Day of Birth").grid(row = 4, sticky = W)

Fname = Entry(root)
Sname = Entry(root)
x = Entry(root)
y = Entry(root)
z = Entry(root)

Fname.grid(row = 0, column = 1)
Sname.grid(row = 1, column = 1)
x.grid(row = 3, column = 1)
y.grid(row = 2, column = 1)
z.grid(row = 4, column = 1)

Fname = Fname.get
Sname = Sname.get
x = x.get
y = y.get
z = z.get

My code works absolutely fine, however it doesn't save what I inputted, let alone save it within a variable. I'm obviously missing chunks of code, but I don't know what code. Please help!? Thanks!!

P.S: Also, if its not too much, how would I make a button to continue on to the next lines of code? Thank you again.

Answer Source

Entry widgets have a get method which can be used to get the values when you need them. Your "save" function simply needs to call this function before writing to a file.

For example:

def save():
    x_value = x.get()
    y_value = y.get()
    z_value = z.get()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download