When I click on the button (Report_But) the report function runs but when I select a date object from the tkcalendar, the my_upd function remains silent (does not run). When I remove the class and adjust the alignment, the code runs perfectly
Please how can nested functions run inside a class
Here is the code
from tkinter import *
class Main:
def __init__(self):
self.mainw = Tk()
self.mainw.title("Daily sales report")
width = 1000
height = 700
screen_width = self.mainw.winfo_screenwidth()
screen_height = self.mainw.winfo_screenheight()
x = (screen_width / 2) - (width / 2)
y = (screen_height / 2) - (height / 2)
self.mainw.geometry("%dx%d+%d+%d" % (width, height, x, y))
self.mainw.resizable(0, 0)
self.frame = Frame(self.mainw, width=1000, height=700, bg="#FFFFFF")
self.frame.place(x=0, y=0)
self.frameinfo = self.frame.place_info()
self.reportbut = Button(self.frame, text="Report_But", font="roboto 14", bd=5, width=8, bg="#FFFFFF",
command=self.report)
self.reportbut.place(x=10, y=5, height=40, width = 200)
def report(self):
import tkinter as tk
from tkinter import ttk
from tkcalendar import DateEntry
import sqlite3
self.my_win = tk.Tk()
self.my_win.geometry("1350x800") # Size of the window
self.my_win.title("Christ the light limitd") # Adding a title
self.sel = tk.StringVar()
self.cal = DateEntry(self.my_win, selectmode='day', textvariable=self.sel)
self.cal.grid(row=0, column=0, padx=20, pady=30)
def my_upd(*args):
if (len(self.sel.get()) > 4):
date = self.cal.get_date() # get selected date object from calendar
date1 = date.strftime('%y-%m-%d') # format for MySQL date column
date2 = date.strftime("%d-%B-%Y") # format to display at label
self.le1.config(text=date2) # display date at Label
conn = sqlite3.connect(r'E:\report.db')
c = conn.cursor()
query = ("select * from sales where Date = :d")
c.execute(query, (date1,))
saleslist = c.fetchall()
for item in self.trv.get_children(): # delete all previous listings
self.trv.delete(item)
total = 0 # to store total sale of the selected date
for data in saleslist:
self.trv.insert("", 'end', iid=data[0], text=data[0],
values=(data[0], data[1], data[2], data[3], data[4], data[5], data[6]))
total = round(total + (data[5]), 2)
self.le2.config(text="Total: " + str(total) + "ssp") # show total value
self.le1 = tk.Label(self.my_win, font=('Times', 22, 'bold'), fg='blue')
self.le1.grid(row=0, column=1)
self.trv = ttk.Treeview(self.my_win, selectmode='browse')
self.trv.grid(row=1, column=1, padx=20, pady=20)
# number of columns
self.trv["columns"] = ("1", "2", "3", "4", "5", "6", "7")
self.trv['height'] = 20
# Defining heading
self.trv['show'] = 'headings'
# width of columns and alignment
self.trv.column("1", width=100, anchor='c')
self.trv.column("2", width=100, anchor='c')
self.trv.column("3", width=100, anchor='c')
self.trv.column("4", width=100, anchor='c')
self.trv.column("5", width=100, anchor='c')
self.trv.column("6", width=100, anchor='c')
self.trv.column("7", width=100, anchor='c')
# respective columns
self.trv.heading("1", text="Transaction ID")
self.trv.heading("2", text="Invoice No.")
self.trv.heading("3", text="Product ID")
self.trv.heading("4", text="Description")
self.trv.heading("5", text="Quantity")
self.trv.heading("6", text="Total Price")
self.trv.heading("7", text="Date")
self.sel.trace('w', my_upd)
self.le2 = tk.Label(self.my_win, font=('Times', 22, 'bold'), fg='red')
self.le2.grid(row=1, column=2, sticky='ne', pady=20)
self.my_win.mainloop()
w=Main()
w.mainw.mainloop()
sorry I could not post the error message here but when you run this code, you will understand my query.