limit
import sqlite3
my_conn = sqlite3.connect('my_db.db')
We need total number of records in the table. We also have to set the number of records per display. ( variable limit )
r_set=my_conn.execute("SELECT count(*) as no from STUDENT")
data_row=r_set.fetchone()
no_rec=data_row[0] # Total number of rows in table
limit = 8; # No of records to be shown per page.
Basics of displaying Tkinter window is here
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("350x200")
The function my_display() is called to display one set of rows. We pass the variable offset to this function. This is the value to start from where the records will be displayed or this is starting point of display. Initial value of offset is 0, On click of Next button it gets value 8 ( The variable limit is set to 8 ) , then it gets 16 , next 24 and so on. On click of Prev button this value decreases by 8 (the value of variable limit ).
q="SELECT * from student LIMIT "+ str(offset) +","+str(limit)
r_set=my_conn.execute(q);
We display each rows with data by using for loop and each data is displayed by using one Entry. i=0 # row value inside the loop
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
To show blank rows at the last page We will set the Entry value to blank string. This part is only executed when we have to display blank rows ( in last page only).
while (i<limit):#required to blank the balance rows if they are less
for j in range(len(student)):
e = Entry(my_w, width=10, fg='blue')
e.grid(row=i, column=j)
e.insert(END, "")
i=i+1
back = offset - limit # This value is used by Previous button
next = offset + limit # This value is used by Next button
b1 = tk.Button(my_w, text='Next >', command=lambda: my_display(next))
b1.grid(row=12,column=4)
b2 = tk.Button(my_w, text='< Prev', command=lambda: my_display(back))
b2.grid(row=12,column=1)
if(no_rec <= next):
b1["state"]="disabled" # disable next button
else:
b1["state"]="active" # enable next button
Similarly for Previous ( Prev ) button we will check the value of variable back. If the value is greater than or equal to 0 then we can keep the Previous button enabled.
if(back >= 0):
b2["state"]="active" # enable Prev button
else:
b2["state"]="disabled"# disable Prev button
Here is the full code. You can download the .ipynb file.
#https://www.plus2net.com/python/tkinter-sqlite-paging.php
import sqlite3
my_conn = sqlite3.connect('my_db.db')
###### end of connection ####
r_set=my_conn.execute("SELECT count(*) as no from STUDENT")
data_row=r_set.fetchone()
no_rec=data_row[0] # Total number of rows in table
limit = 8; # No of records to be shown per page.
##### tkinter window ######
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("350x200")
def my_display(offset):
q="SELECT * from student LIMIT "+ str(offset) +","+str(limit)
r_set=my_conn.execute(q);
i=0 # row value inside the loop
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
while (i<limit): # required to blank the balance rows if they are less
for j in range(len(student)):
e = Entry(my_w, width=10, fg='blue')
e.grid(row=i, column=j)
e.insert(END, "")
i=i+1
# Show buttons
back = offset - limit # This value is used by Previous button
next = offset + limit # This value is used by Next button
b1 = tk.Button(my_w, text='Next >', command=lambda: my_display(next))
b1.grid(row=12,column=4)
b2 = tk.Button(my_w, text='< Prev', command=lambda: my_display(back))
b2.grid(row=12,column=1)
if(no_rec <= next):
b1["state"]="disabled" # disable next button
else:
b1["state"]="active" # enable next button
if(back >= 0):
b2["state"]="active" # enable Prev button
else:
b2["state"]="disabled"# disable Prev button
my_display(0)
my_w.mainloop()
View and Download tkinter-sqlite-paging ipynb file ( .html format )
08-03-2022 | |
Hello out there, this article helped me a lot - although I have a very slow brain - by digesting it line by line it helped me to get my own pagination problem solved. Took 3 weeks :-). Thank YOU for sharing your knowledge. |