Validate the user inputs before executing MySQL insert command.
Insert data to MySQL table and get the confirmation by reading number of rows affected by the insert query.
Add the parent ( node ) to Treeview using the same user entered input data
In the first part of the code below the layout and the Treeview is declared. Using the Treeview and button we will insert the user entered data to the MySQL table and Treeview.
add_data(): the main function
The function add_data() is executed once the button Add record is clicked.
flag
We have set one flag first and keep it true, if any of the validation fails then we will make this flag to False. Based on the final value of the flag we will insert data to MySQL table.
flag_validation=True # set the flag
In our validation we are checking for these issues.
Name should not be less than 2 char length ( Can't be blank )
Class should not be less than 2 char length ( Option must be selected)
Gender should not be less than 2 char length ( One choice must be there )
The mark must be an integer
if(len(my_name) < 2 or len(my_class)<2 or len(my_gender) < 2 ):
flag_validation=False
try:
val = int(my_mark) # checking mark as integer
except:
flag_validation=False
If validation is cleared then data is inserted to MySQL database using insert query. After inserting the row to MySQL table we will get confirmation from MySQL by reading the lastrowid which gives us the value of the auto-incremented id field value. This id value is given by MySQL and can be treated as success of data insert operation. Based on this id value we will insert the data to MySQL table.
if(id.lastrowid>=1): # data is inserted successfully
trv.insert("",'end',
values=(id.lastrowid,my_name,my_class,my_mark,my_gender))
You can copy the full code at the end of this page.
Code Explanation: Breaking Down the Activities
Insert Data into MySQL with Tkinter Treeview: Python GUI Tutorial | #Python #Tkinter #PythonGUI
1. Setting Up the Main Window
from tkinter import ttk
import tkinter as tk
from tkinter import *
my_w = tk.Tk() # Create the main Tkinter window
my_w.geometry('400x500') # Set window size
my_w.title("www.plus2net.com") # Set window title
The code initializes a Tkinter window and sets its dimensions and title.
This is the foundation for the GUI where all widgets will be added.
from sqlalchemy import create_engine, text
from sqlalchemy.exc import SQLAlchemyError
my_conn = create_engine("mysql+mysqldb://root:test@localhost/my_tutorial").connect()
The create_engine function initializes the connection.
---
6. Validating input before adding to database
def add_data(): # on click of button
flag_validation=True # set the flag for input validation
my_name=t1.get("1.0",END) # read name
my_class=options.get() # read class
my_mark=t3.get("1.0",END) # read mark
my_gender=radio_v.get() # read gender
# length of my_name , my_class and my_gender more than 2
if(len(my_name) < 2 or len(my_class)<2 or len(my_gender) < 2 ):
flag_validation=False
try:
val = int(my_mark) # checking mark as integer
except:
flag_validation=False
if(flag_validation):
my_str.set("Adding data...")