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

17,647

Solution 1

This has not been answered yet so here's a complete chunk of code that does what you are requesting.

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)

def getInput():

    a = Fname.get()
    b = Sname.get()
    c = x.get()
    d = y.get()
    e = z.get()
    root.destroy()

    global params
    params = [a,b,c,d,e]


Button(root, text = "submit",
           command = getInput).grid(row = 5, sticky = W)
mainloop()

It is not very elegant but it does exactly what you are asking with the minimum amount of changes to your version.

If you run it, and input 1,2,3,4, and 5 to your entry fields, then click on the submit button I added, and print the params list, you get:

>>> params
['1', '2', '4', '3', '5']

If for some reason you don't want the window to close after submission, omit root.destroy() and take it from there.

Notice that getInput as a Button parameter does not have parentheses so that it is only called when the button is clicked, not when this line is executed.

Finally, I am not sure what you mean by your last question, 'how would I make a button to continue on to the next lines of code'. The mainloop() thingy you have added in the end makes sure (among other things) that the rest of your code is not executed until the box is closed (it also starts a loop collecting events and making sure the events get processed). So once you click submit and the window closes, the rest of the code gets executed. You will further understand this if you add a print('hi') statement before or after the mainloop() line. If you add it before, the string will be printed 'simultaneously' with the opening of the window; if you put it after, it will be printed once the window is closed. (for additional information on mainloop(), have a look at extensive discussions in stack here and here)

Solution 2

For accepting an input from user in tkinter, I always use the following code, with foolproof results -

from tkinter import *
root=Tk()
Label(root,text='Your input prompt').pack()
t1=Text(root, height=2, width=8)
t1.pack()

Now here's the main part:

def value(t):
    x=t.get('1.0','end-1c')
    return x

Obviously you need to trigger this after you have entered a value in the text box, and presumably by using a button that says 'Submit' or 'confirm' or whatever.

def submit():
    a=value(t1)
    print(a)
Button(root, text='Submit', command=submit).pack()

A couple of notes:

  1. Height and width in Text widget are optional.
  2. Note the widget is TEXT, not ENTRY. I haven't seen the code work with entry.
  3. The value inside get "end-1c" is to remove the extra newline character tkinter adds on its own.
  4. You can obviously use grid instead of pack().

Hope you find this useful

Share:
17,647
user6842389
Author by

user6842389

Updated on June 04, 2022

Comments

  • user6842389
    user6842389 almost 2 years

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

    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.

    P.S: Also, if it's not too much, how would I make a button to continue on to the next lines of code?

  • user6842389
    user6842389 over 7 years
    Thanks! So if i were to type print(x), it would then print what I inputted
  • Russell Smith
    Russell Smith over 7 years
    @user6842389: no, you would have to print x_value. x is a widget, not the value in the widget.
  • user6842389
    user6842389 over 7 years
    Okay, however how would i access the x_value since its in a function, and i want to access it outside the function
  • Russell Smith
    Russell Smith over 7 years
    @user6842389: you're missing the point. You can get the value anytime you want, in any function you want. You get the value when you need it.
  • user6842389
    user6842389 over 7 years
    Wait i dont understand how this is meant to work, since I cant confirm that its saved, and I have to press the x button, which means nothing had been saved
  • Russell Smith
    Russell Smith over 7 years
    @user6842389: anywhere in your code where you need the current value in the entry widget x, use x.get(). I honestly don't know how to make that more clear. Don't use x, use x.get().