Hello, I am practicing code in my Python book and I am struggling to define the submit and clear buttons for this Address Entry Form. It isn't working and states that they aren't defined. I also cannot get the 'sunken' effect look on the window frame. Can you help? My code is below. # Import tkinter import tkinter as tk # Define the submit button with a function def submit(): print("First Name: %s\nLast Name: %s\nAddress Line 1: %s\nAddress Line 2: %s\nCity: %s\nState/Province: %s\nCountry: %s" % (e1.get(), e2.get(), e3.get(), e4.get(), e5.get(), e6.get(), e7.get())) # Define the clear button with a function def clear(): e1.delete(0, 'end') e2.delete(0, 'end') e3.delete(0, 'end') e4.delete(0, 'end') e5.delete(0, 'end') e6.delete(0, 'end') e7.delete(0, 'end') # Create the master title master = tk.Tk() master.title("Address Entry Form") # Create the 'sunken' look for the window frame sunken_frame = tk.Frame(master, relief=tk.SUNKEN) # Create the labels labels=["First Name", "Last Name", "Address Line 2", "City", "State/Province", "Country"] entries = [] # Create the length for the labels for i in range(len(labels)): tk.Label(master, text=labels[i]).grid(row=i) entries.append(tk.Entry(master)) entries[i].grid(row=i, column=1) # Create the submit button tk.Button(master, text='Submit', command=submit).grid(row=len(labels), column=1, sticky=tk.W, pady=4) # Create the clear button tk.Button(master,text='Clear', command=clear).grid(row=len(labels), column=0, sticky=tk.W, pady=4) # Run the program tk.mainloop()
Hello, I am practicing code in my Python book and I am struggling to define the submit and clear buttons for this Address Entry Form. It isn't working and states that they aren't defined. I also cannot get the 'sunken' effect look on the window frame. Can you help? My code is below.
# Import tkinter
import tkinter as tk
# Define the submit button with a function
def submit():
print("First Name: %s\nLast Name: %s\nAddress Line 1: %s\nAddress Line 2: %s\nCity: %s\nState/Province: %s\nCountry: %s" % (e1.get(), e2.get(), e3.get(), e4.get(), e5.get(), e6.get(), e7.get()))
# Define the clear button with a function
def clear():
e1.delete(0, 'end')
e2.delete(0, 'end')
e3.delete(0, 'end')
e4.delete(0, 'end')
e5.delete(0, 'end')
e6.delete(0, 'end')
e7.delete(0, 'end')
# Create the master title
master = tk.Tk()
master.title("Address Entry Form")
# Create the 'sunken' look for the window frame
sunken_frame = tk.Frame(master, relief=tk.SUNKEN)
# Create the labels
labels=["First Name", "Last Name", "Address Line 2", "City", "State/Province", "Country"]
entries = []
# Create the length for the labels
for i in range(len(labels)):
tk.Label(master, text=labels[i]).grid(row=i)
entries.append(tk.Entry(master))
entries[i].grid(row=i, column=1)
# Create the submit button
tk.Button(master, text='Submit', command=submit).grid(row=len(labels), column=1, sticky=tk.W, pady=4)
# Create the clear button
tk.Button(master,text='Clear', command=clear).grid(row=len(labels), column=0, sticky=tk.W, pady=4)
# Run the program
tk.mainloop()
Step by step
Solved in 5 steps with 3 images