0

I am interested in how to customize the given entry widget by additionally opening up another entry widget (here phone number) and save the input depending on if another entry is added or not. This is what I have so far:

import tkinter as tk
  
root=tk.Tk()
root.geometry("600x400")
  
name_var=tk.StringVar()
surname_var=tk.StringVar()
 

def getInput():
    name=name_var.get()
    surname=surname_var.get()
     
    print("The name is : " + name)
    print("The Surname is : " + surname)
     
    name_var.set("")
    surname_var.set("")
     
def addEntry():
    phone_label = tk.Label(root, text = 'Phone', font=('calibre',10, 'bold'))
    phone_entry = tk.Entry(root, font=('calibre',10,'normal'))
    phone_label.grid(row=3,column=0)
    phone_entry.grid(row=3,column=1)
    

name_label = tk.Label(root, text = 'Name', font=('calibre',10, 'bold'))
name_entry = tk.Entry(root,textvariable = name_var, font=('calibre',10,'normal'))

surname_label = tk.Label(root, text = 'Surname', font = ('calibre',10,'bold'))
surname_entry=tk.Entry(root, textvariable = surname_var, font = ('calibre',10,'normal'), show = '*')
  
name_label.grid(row=0,column=0)
name_entry.grid(row=0,column=1)
surname_label.grid(row=1,column=0)
surname_entry.grid(row=1,column=1)

save_btn=tk.Button(root,text = 'Save', command = getInput)
save_btn.grid(row=5,column=0)  

add_btn=tk.Button(root,text = 'Add Info', command = addEntry)
add_btn.grid(row=5,column=1)

root.mainloop()

How can I define another function for saving the additional phone input and save it using one 'save' button? Thanks a lot

1 Answer 1

1

You don't need an additional function to save the extra input, just to modify getInput().

What you can do is define an extra StringVar, phone_var, for the phone number (it will be '' by default) and connect it to the extra entry you create in addEntry(). Then, in getInput() you can get the content of phone_var (which always exists even if the entry doesn't). If it is '' then you don't save it.

import tkinter as tk

root = tk.Tk()
root.geometry("600x400")

name_var = tk.StringVar()
surname_var = tk.StringVar()
phone_var = tk.StringVar()  # extra StringVar for the optional entry


def getInput():
    name = name_var.get()
    surname = surname_var.get()
    phone = phone_var.get()

    print("The name is : " + name)
    print("The Surname is : " + surname)
    if phone:  # save it only if not empty
        print("The Phone is : " + phone)

    name_var.set("")
    surname_var.set("")

def addEntry():
    phone_label = tk.Label(root, text='Phone', font=('calibre', 10, 'bold'))
    phone_entry = tk.Entry(root, textvariable=phone_var, font=('calibre', 10, 'normal'))
    phone_label.grid(row=3, column=0)
    phone_entry.grid(row=3, column=1)


name_label = tk.Label(root, text='Name', font=('calibre', 10, 'bold'))
name_entry = tk.Entry(root, textvariable=name_var, font=('calibre', 10, 'normal'))

surname_label = tk.Label(root, text='Surname', font=('calibre', 10, 'bold'))
surname_entry = tk.Entry(root, textvariable=surname_var, font=('calibre', 10, 'normal'), show='*')

name_label.grid(row=0, column=0)
name_entry.grid(row=0, column=1)
surname_label.grid(row=1, column=0)
surname_entry.grid(row=1, column=1)

save_btn = tk.Button(root, text='Save', command=getInput)
save_btn.grid(row=5, column=0)

add_btn = tk.Button(root, text='Add Info', command=addEntry)
add_btn.grid(row=5, column=1)

root.mainloop()
Sign up to request clarification or add additional context in comments.

1 Comment

I was just able to try it out ! you really made my day ! huge thanks :)

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.