3

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user6842389
  • 49
  • 1
  • 4
  • 8

4 Answers4

2

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)

Tony
  • 781
  • 6
  • 22
1

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

TRC
  • 11
  • 1
0

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()
    ...
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thanks! So if i were to type print(x), it would then print what I inputted – user6842389 Sep 17 '16 at 14:46
  • @user6842389: no, you would have to print `x_value`. `x` is a widget, not the value _in_ the widget. – Bryan Oakley Sep 17 '16 at 14:47
  • Okay, however how would i access the x_value since its in a function, and i want to access it outside the function – user6842389 Sep 17 '16 at 14:51
  • @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. – Bryan Oakley Sep 17 '16 at 14:59
  • 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 – user6842389 Sep 17 '16 at 15:57
  • @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()`. – Bryan Oakley Sep 17 '16 at 16:04
0

How do I get an entry widget to save what I input? However I want to save those details as variables, so I can write them in a txt file.

The problem can be fixed.

  • Add import configparser.
  • open("child_inform.ini", "r+")
  • Create child_settings() functions.
  • Add Button widget and add keyword command=child_settings.

Snippet:

from tkinter import *
import configparser

parser_child_inform = configparser.ConfigParser()
parser_child_inform.read('child_inform.ini')

root = Tk()
root.title('Child information')

load_child_inform = open("child_inform.ini", "r+")
line = load_child_inform.readlines()
load_child_inform = line[1].split()[-1]
child_first_name = load_child_inform[2].split()[-1]

def child_settings():
    if not parser_child_inform.has_section("Child"):
        parser_child_inform.add_section('Child')
    parser_child_inform.set('Child', 'first_name', entry_child_first_name.get())
    parser_child_inform.set('Child', 'last_name', entry_child_last_name.get())
    parser_child_inform.set('Child', 'year', entry_child_year.get())
    parser_child_inform.set('Child', 'month', entry_child_month.get())
    parser_child_inform.set('Child', 'day', entry_child_day.get())

    with open("child_inform.ini", "w") as config:
        parser_child_inform.write(config)
        
    root.destroy()                         

if parser_child_inform.has_section("Child"):
    entry_child_first_name = Entry(root)
    entry_child_first_name.insert(0, parser_child_inform.get('Child', 'first_name'))
    entry_child_first_name.grid(row=0, column=1)

    entry_child_last_name = Entry(root)
    entry_child_last_name.insert(0, parser_child_inform.get('Child', 'last_name'))
    entry_child_last_name.grid(row=1, column=1)

    entry_child_year = Entry(root)
    entry_child_year.insert(0, parser_child_inform.get('Child', 'year'))
    entry_child_year.grid(row=2, column=1)

    entry_child_month = Entry(root)
    entry_child_month.insert(0, parser_child_inform.get('Child', 'month'))
    entry_child_month.grid(row=3, column=1)

    entry_child_day = Entry(root)
    entry_child_day.insert(0, parser_child_inform.get('Child', 'day'))
    entry_child_day.grid(row=4, column=1)


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)

Button(root, text='Save', command=child_settings).grid(row = 5,column=1,  sticky = W)
 
mainloop()

Screenshot:

enter image description here

Copied and Pasted in Notepad, Editor, etc:

[Child]
first_name = Adam
last_name = West
year = 1945
month = 8
day = 12
toyota Supra
  • 3,181
  • 4
  • 15
  • 19