Statement

Statement

Above UI we are going to create by Tkinter for Statment(Debit or Credit History) for exesting user and open transaction details when click on it in our Bank management system desktop application. For that first we create a window and configure its title, size and color. Here we are creating a window at Top Level so we can add this window at the top of any other window.

    
       
    root.title("Statement")

    root.configure(bg="#585858")
    root.resizable(width=0, height=0)
    win_width = 1280
    win_height = int(1280 * 56.25 / 100)
    root.geometry(str(win_width) + "x" + str(win_height))
    Util.center(root)
    # Get screen size
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()
    Util.set_font_size(screen_width, screen_height, win_width, win_height)

    self.width = win_width * 96 / 100
    self.height = win_height * 94 / 100
    

After creation of window, we will add a canvas background.

    
    bg_canvas = RoundBackgroundFrame(root, self.width, self.height, padding, cornerradius, self.color, "#585858")
    bg_canvas.place(width=self.width, height=self.height, x=win_width / 2 - self.width / 2,y=win_height / 2 - self.height / 2)

    class RoundBackgroundFrame(tk.Canvas):

        def __init__(self, parent, width, height, padding, cornerradius, color, bg):
            tk.Canvas.__init__(self, parent, borderwidth=0, relief="flat", highlightthickness=0, bg=bg)
            self.width = width
            self.height = height
            self.padding = padding
            self.cornerradius = cornerradius
            self.color = color
  
            original = Image.open("images/corner.png")
            resized = original.resize((int(40), int(40)), Image.ANTIALIAS)
            self.image_r_t = ImageTk.PhotoImage(resized)

            original = original.rotate(90, expand=0)
            resized = original.resize((int(40), int(40)), Image.ANTIALIAS)
            self.image_l_t = ImageTk.PhotoImage(resized)

            original = original.rotate(90, expand=0)
            resized = original.resize((int(40), int(40)), Image.ANTIALIAS)
            self.image_l_b = ImageTk.PhotoImage(resized)

            original = original.rotate(90, expand=0)
            resized = original.resize((int(40), int(40)), Image.ANTIALIAS)
            self.image_r_b = ImageTk.PhotoImage(resized)

            self.shape(width, height, padding, cornerradius, color)
            (x0, y0, x1, y1) = self.bbox("all")
            width = (x1 - x0)
            height = (y1 - y0)
            self.configure(width=width, height=height)

        def shape(self, width, height, padding, cornerradius, color):
            self.create_polygon((padding, height - cornerradius - padding, padding, cornerradius + padding,
                                padding + cornerradius, padding, width - padding - cornerradius, padding,
                                width - padding, cornerradius + padding, width - padding,
                                height - cornerradius - padding, width - padding - cornerradius, height - padding,
                                padding + cornerradius, height - padding), fill=color, outline=color)

            self.create_image(width - self.image_r_t.width(), 0, image=self.image_r_t, anchor=NW)
            self.create_image(0, 0, image=self.image_l_t, anchor=NW)
            self.create_image(0, height - self.image_l_b.width(), image=self.image_l_b, anchor=NW)
            self.create_image(width - self.image_r_b.width(), height - self.image_r_b.width(), image=self.image_r_b, anchor=NW)


    

Once your window is created with custom background, we will create a frame with some reduced dimensions.

    
    self.width = self.width * 98 / 100
    self.height = self.height * 96 / 100
    self.base_frame = Frame(root, width=self.width, height=self.height, bg=self.color)
    bg_canvas.create_window(self.width / 100, self.height * 2 / 100, anchor=NW, window=self.base_frame)

    #Add Heading    
    add_heading_label(self.base_frame, self.color, AppConstant.FONT_SIZE, self.height, self.width)

    def add_heading_label(base_frame, color, font_size, height, width):
        label_heading = Label(base_frame, text="Statement", anchor=CENTER, bg=color,font=("Lucida Grande", font_size + 6))
        label_heading.place(width=width * 90 / 100, height=height * 7 / 100, x=width * 5 / 100, y=height * 0.5 / 100)

    #Add Line
    add_line_border(self.base_frame, self.color, self.height, self.width)
   
    def add_line_border(base_frame, color, height, width):
        line_canvas = Canvas(base_frame, bg=color, borderwidth=0, relief="flat", highlightthickness=0)
        line_canvas.place(width=width - 4, height=5, x=2, y=height * 10 / 100)
        line_canvas.create_line(0, 0, width, 0, fill="#787878")

    #Add Statement Frame
    add_statement_frame(self.base_frame, self.color, self.height, self.width)
   
    def add_statement_frame(base_frame, color, height, width):
        sfw = width * 0.94
        sfh = height * 0.87
        button_font = ("Lucida Grande", AppConstant.FONT_SIZE - 6)

        label_frame_statement = LabelFrame(base_frame, text="Statement", font=button_font, pady=sfw * 2 / 100,
                                    padx=sfw * 2 / 100, bg=color)
        label_frame_statement.place(width=sfw, height=sfh, x=width * 0.03, y=height * 0.12)

        
    

Fetch data from database and show in table form with the use of "Treeview" of Tkinter

    
        conn = Util.connect_db()
        cursor = conn.cursor()
        style = ttk.Style()
    
        style.layout("Custom.Treeview.Heading", [
            ("Custom.Treeheading.cell", {'sticky': 'nswe'}),
            ("Custom.Treeheading.border", {'sticky': 'nswe', 'children': [
                ("Custom.Treeheading.padding", {'sticky': 'nswe', 'children': [
                    ("Custom.Treeheading.image", {'side': 'right', 'sticky': ''}),
                    ("Custom.Treeheading.text", {'sticky': 'we'})
                ]})
            ]}),
        ])
        
        style.map("Custom.Treeview", background=[('selected', '#ad51ad')])
        style.configure("Custom.Treeview", highlightthickness=0, bd=0, font=('Calibri', 11), rowheight=30)

        self.tree = ttk.Treeview(self.base_frame, height=13, show='tree', style="Custom.Treeview")
        self.tree.tag_configure('odd', background='#ffbfff', foreground="#000000", )
        self.tree.tag_configure('even', background='#FFFFFF', foreground="#000000", )
        self.tree.place(x=80, y=200)
        self.tree["columns"] = "1", "2", "3", "4", "5", "6", "7"
        self.tree.column("#0", width=0)
        self.tree.column("#1", width=110)
        self.tree.column("#2", width=150)
        self.tree.column("#3", width=150)
        self.tree.column("#4", width=150)
        self.tree.column("#5", width=150)
        self.tree.column("#6", width=140)
        self.tree.column("#7", width=180)

        self.table_heading()

        vsby = ttk.Scrollbar(self.base_frame, orient="vertical", command=self.tree.yview)
        vsby.place(x=self.width - 80, y=200, height=390)
        self.tree.configure(yscrollcommand=vsby.set)

        self.get_table_data()

        self.tree.column("#1", anchor=tk.CENTER)
        self.tree.column("#2", anchor=tk.CENTER)
        self.tree.column("#3", anchor=tk.CENTER)
        self.tree.column("#4", anchor=tk.CENTER)
        self.tree.column("#5", anchor=tk.CENTER)
        self.tree.column("#6", anchor=tk.CENTER)
        self.tree.column("#7", anchor=tk.CENTER)




        root.attributes('-alpha', 0.0)
        root.attributes('-alpha', 1.0)

    

We have created our own heading for History table, lets configure it.

    

def table_heading(self):

    heading_y = 173
    bg_color = "#800080"
    text_color = "#ffffff"

    canvas = Canvas(self.base_frame, width=1030, height=30)
    canvas.create_rectangle(0, 0, 1030, 30, fill=bg_color)
    canvas.place(x=80, y=heading_y - 4)

    credit_label = Label(self.base_frame, text="Credit",
                        font=('Calibri', AppConstant.FONT_SIZE - 3, 'bold'), background=bg_color,
                        foreground=text_color)
    credit_label.place(x=90, y=heading_y, width=130)
    credit_label.configure(anchor="center")

    debit_label = Label(self.base_frame, text="Debit",
                        font=('Calibri', AppConstant.FONT_SIZE - 3, 'bold'), background=bg_color,
                        foreground=text_color)
    debit_label.place(x=225, y=heading_y, width=120)
    debit_label.configure(anchor="center")

    balance_label = Label(self.base_frame, text="Balance",
                              font=('Calibri', AppConstant.FONT_SIZE - 3, 'bold'), background=bg_color,
                              foreground=text_color)
    balance_label.place(x=380, y=heading_y, width=110)
    balance_label.configure(anchor="center")

    credit_date_label = Label(self.base_frame, text="Credit Date",
                            font=('Calibri', AppConstant.FONT_SIZE - 3, 'bold'), background=bg_color,
                            foreground=text_color)
    credit_date_label.place(x=505, y=heading_y, width=150)
    credit_date_label.configure(anchor="center")

    dedit_date_label = Label(self.base_frame, text="Dedit Date",
                            font=('Calibri', AppConstant.FONT_SIZE - 3, 'bold'), background=bg_color,
                            foreground=text_color)
    dedit_date_label.place(x=660, y=heading_y, width=150)
    dedit_date_label.configure(anchor="center")

    category = Label(self.base_frame, text="Category",
                         font=('Calibri', AppConstant.FONT_SIZE - 3, 'bold'), background=bg_color,
                         foreground=text_color)
    category.place(x=815, y=heading_y, width=130)
    category.configure(anchor="center")

    description = Label(self.base_frame, text="Description",
                        font=('Calibri', AppConstant.FONT_SIZE - 3, 'bold'), background=bg_color,
                        foreground=text_color)
    description.place(x=960, y=heading_y, width=140)
    description.configure(anchor="center")

        

UI part complete, here's logic of add data in history table fetch from database

    
    def get_table_data(self):
        conn = Util.connect_db()
        cursor = conn.cursor()

        cursor.execute('SELECT credit, debit, balance, credit_date, debit_date, category, description FROM statement where user_id IS ?',
                        (AppConstant.USER_ID,))
        sql_output = cursor.fetchall()

        count = 0
        for data in sql_output:
            if count % 2 == 0:
                self.tree.insert('', 'end', values=data, tags=('even',))
            else:
                self.tree.insert('', 'end', values=data, tags=('odd',))
            count = count + 1
               

At last we will manage closing of window

    
    def on_closing():
                        
        root.destroy()
        args[0].deiconify()
                       
    root.protocol("WM_DELETE_WINDOW", on_closing)
                       

Programmer Mirta is for learning and training. Projects might be simple to improve learning. Projects are constantly reviewed to avoid errors, but we cannot assure full correctness of all content. While using Programmer Mitra, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 2021 by Programmer Mitra. All Rights Reserved.