Treeview | Displaying records from MySQL database in Tkinter window using Treeview |
Delete | Deleting selected row from MySQL table and Treeview |
Pagination | Pagination of records from MySQL table using Treeview |
Paging | Pagination of records of MySQL table using Tkinter window |
record display | Displaying record in a Tkinter window by taking user input as row id |
record update | Select-Edit-Update row by using Treeview |
Image display | Displaying Binary data from MySQL Blob column. |
Image display | Displaying all records with Images from MySQL Blob column. |
Image add | Inserting uploaded image to Blob column of MySQL table |
Image add | Adding data to student table with Photo using Blob column of MySQL table |
Image Update | Updating user uploaded image in MySQL Blob column |
record add | Adding user entered data through Tkinter window to MySQL table |
Login | System for Login with add user and listing users with delete option |
Random | Display Random record from MySQL or SQLite Database. |
Ticket No. | Generate unique string id using date after inserting record |
import mysql.connector # Importing the MySQL connector library
my_connect = mysql.connector.connect(
host="localhost", # Hostname of the MySQL server
user="userid", # MySQL username
passwd="password", # MySQL password
database="database_name" # Name of the database to connect
)
####### end of connection ####
my_conn = my_connect.cursor() # Create a cursor object to execute SQL queries
We will use my_conn in our further script as the connection object to get our records.
my_conn = my_connect.cursor() # Create a cursor object for executing SQL queries
my_conn.execute("SELECT * FROM student limit 0,10") # Execute the SQL query to fetch 10 rows
i = 0 # Initialize row counter
for student in my_conn: # Iterate through the result set
for j in range(len(student)): # Iterate through each column in a row
print(student[j], end='') # Print column value without line break
i = i + 1 # Increment the row counter
print() # Line break at the end of one row
This will print 10 rows of records from student table.
import mysql.connector
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("400x250") # width and height of the window
my_connect = mysql.connector.connect(
host="localhost", # Database host
user="id", # Database user
passwd="password", # Database password
database="my_db" # Database name
)
my_conn = my_connect.cursor() # Cursor for executing queries
####### end of connection ####
my_conn.execute("SELECT * FROM student limit 0,10")
i = 0
for student in my_conn:
for j in range(len(student)):
e = Entry(my_w, width=10, fg='blue')
e.grid(row=i, column=j)
e.insert(END, student[j])
i = i + 1
my_w.mainloop()
from sqlalchemy import create_engine, text
my_conn = create_engine("mysql+mysqldb://id:pw@localhost/my_db")
my_conn = my_conn.connect() # connection object, connection string
import tkinter as tk
from tkinter import *
my_w = tk.Tk() # parent window
my_w.geometry("400x250") # width and height of window
####### end of connection ####
r_set = my_conn.execute(text("SELECT * FROM student limit 0,10"))
i = 0
for student in r_set:
for j in range(len(student)):
e = Entry(my_w, width=10, fg='blue')
e.grid(row=i, column=j)
e.insert(END, student[j])
i = i + 1
my_w.mainloop()
Using Label in place of Entry in above code.
for student in r_set:
for j in range(len(student)):
e = Label(my_w,width=10, text=student[j])
e.grid(row=i, column=j)
#e.insert(END, student[j])
i=i+1
We can add border to each cell by using borderwidth=2
, relief='ridge'
and align the text to left by using anchor='w'
.
e = Label(my_w,width=10, text=student[j],
borderwidth=2,relief='ridge', anchor="w")
e=Label(my_w,width=10,text='id',borderwidth=2, relief='ridge',anchor='w',bg='yellow')
e.grid(row=0,column=0)
e=Label(my_w,width=10,text='Name',borderwidth=2, relief='ridge',anchor='w',bg='yellow')
e.grid(row=0,column=1)
e=Label(my_w,width=10,text='Class',borderwidth=2, relief='ridge',anchor='w',bg='yellow')
e.grid(row=0,column=2)
e=Label(my_w,width=10,text='mark',borderwidth=2, relief='ridge',anchor='w',bg='yellow')
e.grid(row=0,column=3)
e=Label(my_w,width=10,text='Gender',borderwidth=2, relief='ridge',anchor='w',bg='yellow')
e.grid(row=0,column=4)
i=1
01-11-2020 | |
Thanks a lot...Loved it!! It works for me! |
21-11-2020 | |
Thanks a lot |
02-12-2020 | |
Hey, your code is working very well. Thank You so much. But there is one query. Every time the table is displayed in the Tk() window it goes to the top left corner. What is to be done if i want it somewhere in the middle? |
04-12-2020 | |
my_w.geometry('400x250+450+350') Here 450 and 350 are x and y position. You can change this value to position it at different locations. You can further improve it by reading user screen resolution and then apply it to dynamically position the window by changing these values. |
04-12-2020 | |
Don't use space while adding values to geometry(). This will not work my_w.geometry('400x250 + 450 + 350') |
02-07-2021 | |
thank you soo much !!!!!!!!!!!! |
20-07-2023 | |
Hi, this is working very well for me, how do I make the labels update each time I make a change to the database? thank you |
22-07-2023 | |
You have to refresh the view to reflect the changes. Here we are using grid view so there is a way to refresh the grid view. Grid view details are here, Grid use grid_forget() to remove than and create again. |