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