Tkinter Python Program to get details from Form and feeding to excel sheet

We are getting data via the form and feeding that to excel sheets. Let’s look into it with the below example:

import tkinter as tk
import xlwt

root = tk.Tk()

# setting the windows size
root.geometry("600x400")

# declaring string variable
# for storing details
cid_var = tk.StringVar()
cname_var = tk.StringVar()
caddr_var = tk.StringVar()
cstate_var = tk.StringVar()
ccity_var = tk.StringVar()
cmobile_var = tk.StringVar()
cgender_var = tk.StringVar()
cage_var = tk.StringVar()

# defining a function that will
# get the details and
# print them on the screen
def submit():
cid = cid_var.get()
cname = cname_var.get()
caddr = caddr_var.get()
cstate = cstate_var.get()
ccity = ccity_var.get()
cmobile = cmobile_var.get()
cgender = cgender_var.get()
cage = cage_var.get()

workbook = xlwt.Workbook()

#Naming the sheet in the excel
sheet = workbook.add_sheet("Contact Details")

sheet.write(0, 0, 'Contact Details')
sheet.write(1, 0, 'Contact ID')
sheet.write(2, 0, 'Name')
sheet.write(3, 0, 'Address')
sheet.write(4, 0, 'State')
sheet.write(5, 0, 'City')
sheet.write(6, 0, 'Mobile No')
sheet.write(7, 0, 'Gender')
sheet.write(8, 0, 'Age')

sheet.write(1, 1, cid)
sheet.write(2, 1, cname)
sheet.write(3, 1, caddr)
sheet.write(4, 1, cstate)
sheet.write(5, 1, ccity)
sheet.write(6, 1, cmobile)
sheet.write(7, 1, cgender)
sheet.write(8, 1, cage)

#saving the data to excel
workbook.save("ContactDetails.xls")

cid_var.set("")
cname_var.set("")
caddr_var.set("")
cstate_var.set("")
ccity_var.set("")
cmobile_var.set("")
cgender_var.set("")
cage_var.set("")

cid_label = tk.Label(root, text='Contact ID', font=('calibre', 10, 'bold'))
cname_label = tk.Label(root, text='Contact Name', font=('calibre', 10, 'bold'))
caddr_label = tk.Label(root, text='Contact Address', font=('calibre', 10, 'bold'))
cstate_label = tk.Label(root, text='State', font=('calibre', 10, 'bold'))
ccity_label = tk.Label(root, text='City', font=('calibre', 10, 'bold'))
cmobile_label = tk.Label(root, text='Mobile Number', font=('calibre', 10, 'bold'))
cgender_label = tk.Label(root, text='Gender', font=('calibre', 10, 'bold'))
cage_label = tk.Label(root, text='Age', font=('calibre', 10, 'bold'))

cid_entry = tk.Entry(root, textvariable=cid_var, font=('calibre', 10, 'normal'))
cname_entry = tk.Entry(root, textvariable=cname_var, font=('calibre', 10, 'normal'))
caddr_entry = tk.Entry(root, textvariable=caddr_var, font=('calibre', 10, 'normal'))
cstate_entry = tk.Entry(root, textvariable=cstate_var, font=('calibre', 10, 'normal'))
ccity_entry = tk.Entry(root, textvariable=ccity_var, font=('calibre', 10, 'normal'))
cmobile_entry = tk.Entry(root, textvariable=cmobile_var, font=('calibre', 10, 'normal'))
cgender_entry = tk.Entry(root, textvariable=cgender_var, font=('calibre', 10, 'normal'))
cage_entry = tk.Entry(root, textvariable=cage_var, font=('calibre', 10, 'normal'))

#submit button
sub_btn = tk.Button(root, text='Submit', command=submit)

# placing the label and entry in
# the required position using grid
# method
cid_label.grid(row=0, column=0)
cname_label.grid(row=1, column=0)
caddr_label.grid(row=2, column=0)
cstate_label.grid(row=3, column=0)
ccity_label.grid(row=4, column=0)
cmobile_label.grid(row=5, column=0)
cgender_label.grid(row=6, column=0)
cage_label.grid(row=7, column=0)

cid_entry.grid(row=0, column=1)
cname_entry.grid(row=1, column=1)
caddr_entry.grid(row=2, column=1)
cstate_entry.grid(row=3, column=1)
ccity_entry.grid(row=4, column=1)
cmobile_entry.grid(row=5, column=1)
cgender_entry.grid(row=6, column=1)
cage_entry.grid(row=7, column=1)

sub_btn.grid(row=8, column=1)

# performing an infinite loop
# for the window to display
root.mainloop()
Upon running the program, the form will be displayed
Feed the required form details
After clicking submit, a excel will be generated where in all the given form data is stored

How to use TKINTER UI CONTROLS in Python

Tkinter is a python package that provides various controls, such as buttons, labels, radio buttons, text area and text boxes used in a GUI application. These controls are commonly called widgets.  The Button widget is used to display buttons in your application.

Lets learn these UI controls via a simple form creation program:

from tkinter import *
from tkinter import ttk
from tkinter import messagebox

window = Tk()
window.title("Contact Details")
window.geometry('1000x1000')
window.configure(background = "pink");
a = Label(window ,text = "Contact ID").grid(row = 0,column = 0)
b = Label(window ,text = "Contact Name").grid(row = 2,column = 0)
c = Label(window ,text = "Contact Address").grid(row = 4,column = 0)
d = Label(window ,text = "State").grid(row = 6,column = 0)
e = Label(window ,text = "City").grid(row = 8,column = 0)
f = Label(window ,text = "Mobile Number").grid(row = 10,column = 0)
g = Label(window ,text = "Gender").grid(row = 12,column = 0)
h = Label(window ,text = "Age").grid(row = 14,column = 0)
v=IntVar()
v.set(1)
r1=Radiobutton(window, text="male", variable=v,value=1).grid(row = 12,column = 1)
r2=Radiobutton(window, text="female", variable=v,value=2).grid(row = 12,column = 2)
list_of_state=[ 'Texas' ,'Arizona' , 'Illinois' ,'New Jersey' ,'Utah']
list_of_city=[ 'Houston' ,'Phoenix' ,'Chicago' , 'Atlantic City', 'Blanding']

#the variable 'c' mentioned here holds String Value, by default ""
c=StringVar()
droplist=OptionMenu(window,c, *list_of_state)
droplist.config(width=25)
c.set('Select your State')
droplist.grid(row = 6,column = 1)

#the variable 'c' mentioned here holds String Value, by default ""
c1=StringVar()
droplist1=OptionMenu(window,c1, *list_of_city)
droplist1.config(width=25)
c1.set('Select your City')
droplist1.grid(row = 8,column = 1)

a1 = Entry(window).grid(row = 0,column = 1)
b1 = Entry(window).grid(row = 2,column = 1)
c1 = Entry(window).grid(row = 4,column = 1)
d1 = Entry(window).grid(row = 10,column = 1)
f1 = Entry(window).grid(row = 14,column = 1)

def result():
messagebox.showinfo("information","Contact Details Saved Successfully")
btn = ttk.Button(window ,text="Submit",command=result).grid(row=40,column=3)
window.mainloop()
Run the program and form will get displayed
User should enter the required contact details
Upon submitting the form, user will get the success response message