Creating a NSS data entry application form using tkinter

# Tkinter imports

import tkinter as tk

from tkinter import ttk

# For creating the filename

from datetime import datetime

# For file operations

from pathlib import Path

# For creating the CSV file

import csv

# Create a dict to hold our variables

variables = dict()

# Variable to store the number of records saved

records_saved = 0

# Configure the root window

root = tk.Tk()

root.title('NSS Data Entry Application')

root.columnconfigure(0, weight=1)

# Application heading

ttk.Label(

 root,

 text="NSS Data Entry Application",

 font=("TkDefaultFont", 16)

).grid()

####################

# Data Record Form #

####################

# Build the data record form in a Frame

# to keep geometry management simpler

drf = ttk.Frame(root)

drf.grid(padx=10, sticky=(tk.E + tk.W))

drf.columnconfigure(0, weight=1)

##############################

# Record information Frame #

##############################

r_info = ttk.LabelFrame(drf, text='Student Information')

r_info.grid(sticky=(tk.W + tk.E))

# Rollno

variables['Rollno'] = tk.IntVar()

ttk.Label(r_info, text='Roll number').grid(row=0, column=0)

tk.Entry(

 r_info, textvariable=variables['Rollno']

).grid(row=1, column=0, sticky=(tk.W + tk.E))

# Course

course_values = ['BA', 'Bsc', 'Bcom']

variables['course'] = tk.StringVar()

ttk.Label(r_info, text='Course').grid(row=0, column=1)

ttk.Combobox(

 r_info, textvariable=variables['course'], values=course_values

).grid(row=1, column=1, sticky=(tk.W + tk.E))

# Semester

variables['sem'] = tk.StringVar()

ttk.Label(r_info, text='Semester').grid(row=2, column=0)

labframe = ttk.Frame(r_info)

for sem in ('I', 'III', 'V'):

 ttk.Radiobutton(

 labframe, value=sem, text=sem, variable=variables['sem']

).pack(side=tk.LEFT, expand=True)

labframe.grid(row=3, column=0, sticky=(tk.W + tk.E))

#volunteer days

variables['voldays'] = tk.DoubleVar()

ttk.Label(r_info,text="Volunteer days").grid(row=4, column=0)

ttk.Spinbox(

 r_info, textvariable=variables['voldays'],

 from_=1, to=10, increment=1,

).grid(row=4, column=1, sticky=(tk.W + tk.E))

#volunteer?

variables['vol'] = tk.BooleanVar(value=False)

ttk.Checkbutton(

 r_info, variable=variables['vol'],

 text='Volunteer?'

).grid(row=5, column=0, sticky=tk.W, pady=5)

ttk.Label(r_info, text="Notes").grid()

# we can't use a variable for a Text input

notes_inp = tk.Text(drf, width=75, height=10)

notes_inp.grid(sticky=(tk.W + tk.E))

########################

# Save & Reset Buttons #

########################

buttons = ttk.Frame(drf)

buttons.grid(sticky=tk.E + tk.W)

save_button = ttk.Button(buttons, text='Save')

save_button.pack(side=tk.RIGHT)

reset_button = ttk.Button(buttons, text='Reset')

reset_button.pack(side=tk.RIGHT)

##############

# Status Bar #

##############

# This is attached to the root window, not the form!

status_variable = tk.StringVar()

ttk.Label(

 root, textvariable=status_variable

).grid(sticky=tk.W + tk.E, row=99, padx=10)

#############

# Functions #

#############

def on_reset():

 """Called when reset button is clicked, or after save"""

for variable in variables.values():

 if isinstance(variable, tk.BooleanVar):

 variable.set(False)

else:

 variable.set('')

 # reset notes_inp

 notes_inp.delete('1.0', tk.END)

reset_button.configure(command=on_reset)

def on_save():

 """Handle save button clicks"""

global records_saved

# For now, we save to a hardcoded filename with a datestring.

# If it doesnt' exist, create it,

# otherwise just append to the existing file

 datestring = datetime.today().strftime("%Y-%m-%d")

 filename = f"abq_data_record_{datestring}.csv"

 newfile = not Path(filename).exists()

 # get the data from the variables

 data = dict()

for key, variable in variables.items():

 try:

 data[key] = variable.get()

except tk.TclError:

 status_variable.set(

 f'Error in field: {key}. Data was not saved!')

 return

 # get the Text widget contents separately

 data['Notes'] = notes_inp.get('1.0', tk.END)

 # Append the record to a CSV

with open(filename, 'a', newline='') as fh:

 csvwriter = csv.DictWriter(fh, fieldnames=data.keys())

 if newfile:

 csvwriter.writeheader()

 csvwriter.writerow(data)

 records_saved += 1

status_variable.set(

 f"{records_saved} records saved this session")

 on_reset()

save_button.configure(command=on_save)

# Reset the form

on_reset()

####################

# Execute Mainloop #

####################

root.mainloop()









Comments

Popular posts from this blog

Diagonal Relationship between Beryllium and Aluminium || Relation between Beryllium and Aluminium

KVS CBSE Worksheets CHAPTER 1 Electric Charges and Fields Class 12

Kinematics Important Questions Of Physics

QUANTUM NUMBERS (Principal, Azimuthal, Magnetic and Spin)

NCERT Chemical Reactions and Equations Solutions || NCERT Solution Class 10 Science Chapter 1 || Chemistry || Science ||