How to create data entry form using Tkinter with MS access using python codes

Rover
Rover
3.3 هزار بار بازدید - پارسال - Welcome to this tutorial on
Welcome to this tutorial on how to create a data entry form using Tkinter with MS Access using Python codes. In this video, you'll learn step by step how to build a graphical user interface (GUI) using Tkinter, a popular Python library for creating desktop applications, and how to connect it to an MS Access database to perform data entry.

First, we'll start by creating a new database in MS Access and defining the table structure for the data we want to enter. Then, we'll create a Python script using the pyodbc library to establish a connection to the database from Python. We'll use this connection to perform basic operations such as reading and writing data.

Next, we'll dive into the Tkinter library and build a graphical user interface (GUI) to display a form for entering data. We'll create widgets such as text boxes, labels, buttons, and dropdown menus to design an intuitive and user-friendly interface for data entry.

After designing the interface, we'll connect it to the MS Access database using the pyodbc library and write code to insert new records into the database when the user clicks the submit button. We'll also add error handling code to ensure that the user enters valid data and handle any exceptions that might occur.

By the end of this tutorial, you'll have a solid understanding of how to use Python, Tkinter, and MS Access to create a data entry form for your own projects. Whether you're a beginner or an experienced Python developer, this tutorial will provide you with the knowledge and skills you need to build your own GUI applications. So, let's get started!

Codes
import tkinter as tk
import tkinter.messagebox as messagebox
import pyodbc

Define the connection string
conn_str = (
   r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};'
   r'DBQ=C:\Users\Franz\Desktop/Sample.accdb;'
)

Establish a connection to the database
conn = pyodbc.connect(conn_str)

Create a cursor object to execute SQL commands
cursor = conn.cursor()

Create the tkinter form
root = tk.Tk()

Set the title and window size
root.title("My Form")
root.geometry("400x250")

Define a function to insert data into the database
def insert_data():
   Get the values from the form
   name = name_entry.get()
   age = age_entry.get()

   Check if the name and age fields are filled in
   if not name or not age:
       messagebox.showerror("Error", "Please fill in all fields.")
       return

   Check if age is a valid integer
   try:
       age = int(age)
   except ValueError:
       messagebox.showerror("Error", "Age must be a number.")
       return

   Execute the SQL command to insert the data
   cursor.execute("INSERT INTO test (Sname, Sage) VALUES (?, ?)", name, age)

   Commit the transaction
   conn.commit()

   Clear the form
   name_entry.delete(0, tk.END)
   age_entry.delete(0, tk.END)

   Show a success message
   messagebox.showinfo("Success", "Data inserted successfully.")

Define a function to clear the form
def clear_form():
   name_entry.delete(0, tk.END)
   age_entry.delete(0, tk.END)

Add a header label
header_label = tk.Label(root, text="My Form", font=("Arial", 20))
header_label.pack(pady=10)

Add the form elements
name_label = tk.Label(root, text="Name:")
name_entry = tk.Entry(root, font=("Arial", 12))
age_label = tk.Label(root, text="Age:")
age_entry = tk.Entry(root, font=("Arial", 12))
submit_button = tk.Button(root, text="Submit", command=insert_data)
clear_button = tk.Button(root, text="Clear", command=clear_form)
success_label = tk.Label(root, fg="green", font=("Arial", 12))

Set the form element styles
name_label.configure(font=("Arial", 12))
age_label.configure(font=("Arial", 12))
submit_button.configure(font=("Arial", 12, "bold"), bg="#388E3C", fg="white", pady=5, padx=10)
clear_button.configure(font=("Arial", 12, "bold"), bg="#388E3C", fg="white", pady=5, padx=10)
success_label.configure(font=("Arial", 12))

Pack the form elements
name_label.pack()
name_entry.pack(pady=5)
age_label.pack()
age_entry.pack(pady=5)
submit_button.pack(pady=10)
clear_button.pack(pady=5)
success_label.pack(pady=5)

Set keyboard focus to the name field
name_entry.focus_set()

Start the tkinter main loop
root.mainloop()

Close the database connection
conn.close()
پارسال در تاریخ 1402/02/25 منتشر شده است.
3,307 بـار بازدید شده
... بیشتر