CS_Project_Database_UI_with_Tkinter
CS_Project_Database_UI_with_Tkinter
with Tkinter
Overview
This project involves creating a graphical user interface (GUI) to interact with a MySQL
database using Python’s Tkinter and mysql.connector.
The application provides functionalities to:
- View and interact with data from different tables in the database.
- Add new records to the selected table.
- Fetch and display data dynamically based on the table selection.
Features:
1. Table Selection: Users can select a table from a dropdown menu.
2. Display Table Data: A treeview displays the data from the selected table.
3. Add New Record: Users can fill out a form to add new records to the selected table.
4. Validation and Error Handling: Ensures that all fields are filled before submission and
handles database errors gracefully.
1. Database Connection
The fetch_data() function retrieves data from the database based on a provided query.
# Get columns
columns = get_table_columns(self.connection, table_name)
self.tree["columns"] = columns
self.tree["show"] = "headings"
# Add columns to treeview
for col in columns:
self.tree.heading(col, text=col)
self.tree.column(col, width=100)
The add_record() function allows users to add a new record to the selected table. It checks
for form validation and executes an INSERT query.
def add_record(self):
"""Add a new record to the selected table."""
try:
if not self.current_table:
messagebox.showwarning("Warning", "Please select a table first!")
return
if not all(values):
messagebox.showwarning("Validation Error", "Please fill all fields!")
return
except Exception as e:
messagebox.showerror("Error", f"Failed to add record: {e}")
def create_entry_form(self):
"""Create entry form for adding records."""
if not self.current_table:
return
self.entry_widgets = {}
columns = get_table_columns(self.connection, self.current_table)
Finally, the run() method initializes and runs the Tkinter event loop.
def run(self):
"""Start the application."""
try:
self.root.mainloop()
finally:
if self.connection:
self.connection.close()
Conclusion
This project demonstrates how to use Python's Tkinter library to build a simple yet
functional database management interface.
It provides the ability to select tables, view data, and add records, all while handling
common database operations and errors.