In this article, we will build a simple Portfolio Management System in Python with the help of Tkinter and SQLite. Portfolio Management is a system in which any individual can select investment plans based on some qualities of a particular asset to meet the long-term financial objectives of an individual.
Project overview: Portfolio Management System in Python
Project Name: | Portfolio Management system in Python |
Abstract: | We are going to create a GUI-based project which will have basic portfolio management features. |
Technologies Used: | Python, Tkinter, and SQLite |
IDE: | VS Code |
Python version (Recommended): | 3.x |
Database: | SQLite |
Type: | Desktop Application |
Recommended for: | All Python learners |
Features of the Portfolio Management system
- The user can Register and log in to the system
- The user will be able to choose from a list of available stocks
- The user will be able to add, delete, update, and view all the assets.
Complete Code for Portfolio Management system in Python
Create a folder for the project. Here we will create three python files named – Login.py, Portfolio_frontend.py, and Portfolio_backend.py
Login.py file
In this file, we write the code for creating the GUI for the login screen and the backend logic for registering and logging in for the user.
import sqlite3 from tkinter import* import os # for os.system from tkinter import ttk from tkinter import messagebox as ms # for messagebox def main(): root = Tk() app = user(root) # make database and users (if not exists already) table at programme start up with sqlite3.connect('Users.db') as db: c = db.cursor() c.execute('CREATE TABLE IF NOT EXISTS user (username TEXT NOT NULL ,password TEXT NOT NULL)') db.commit() db.close() #main Class class user: def __init__(self,master): # Window self.master = master # Some Usefull variables self.username = StringVar() self.password = StringVar() self.n_username = StringVar() self.n_password = StringVar() #Create Widgets self.widgets() #Login Function def login(self): #Establish Connection with sqlite3.connect('Users.db') as db: c = db.cursor() #Find user If there is any take proper action find_user = ('SELECT * FROM user WHERE username = ? and password = ?') c.execute(find_user,[(self.username.get()),(self.password.get())]) result = c.fetchall() if result: # close this window self.master.destroy() # open new window os.system('python ' + 'Portfolio_frontend.py') else: ms.showerror('Oops!','Username Not Found.') def new_user(self): #Establish Connection with sqlite3.connect('Users.db') as db: c = db.cursor() #Find Existing username if any take proper action find_user = ('SELECT * FROM user WHERE username = ?') c.execute(find_user,[(self.username.get())]) if c.fetchall(): ms.showerror('Error!','Username Already Taken!') else: ms.showinfo('Success!','Account Created!') self.log() #Create New Account insert = 'INSERT INTO user(username,password) VALUES(?,?)' c.execute(insert,[(self.n_username.get()),(self.n_password.get())]) db.commit() #Frame Packing Methods def log(self): self.username.set('') self.password.set('') self.crf.pack_forget() self.head['text'] = 'Login' self.logf.pack() def cr(self): self.n_username.set('') self.n_password.set('') self.logf.pack_forget() self.head['text'] = 'Register' self.crf.pack() #Draw Widgets def widgets(self): self.head = Label(self.master,text = 'Login or Register',font = ('MV boli',30),pady = 10) self.head.pack() self.logf = Frame(self.master, padx =50,pady = 50) Label(self.logf,text = 'Username: ',font = ('MV Boli',20),pady=10,padx=5).grid(sticky = W) Entry(self.logf,textvariable = self.username,bd = 5,font = ('',15)).grid(row=0,column=1) Label(self.logf,text = 'Password: ',font = ('MV Boli',20),pady=30,padx=5).grid(sticky = W) Entry(self.logf,textvariable = self.password,bd = 5,font = ('',15),show = '*').grid(row=1,column=1) Button(self.logf,text = ' Login ',bd = 1 ,font = ('',15),padx=5,pady=5,command=self.login, bg="black", fg="white").grid() Button(self.logf,text = ' Register ',bd = 1 ,font = ('',15),padx=5,pady=5,command=self.cr, bg="black", fg="white").grid(row=2,column=1) self.logf.pack() self.crf = Frame(self.master, padx =10,pady = 10) Label(self.crf,text = 'Username: ',font = ('',20),pady=30,padx=5).grid(sticky = W) Entry(self.crf,textvariable = self.n_username,bd = 5,font = ('',15)).grid(row=0,column=1) Label(self.crf,text = 'Password: ',font = ('',20),pady=30,padx=5).grid(sticky = W) Entry(self.crf,textvariable = self.n_password,bd = 5,font = ('',15),show = '*').grid(row=1,column=1) Button(self.crf,text = 'Register',bd = 1 ,font = ('',15),padx=5,pady=5,command=self.new_user, bg="black", fg="white").grid() Button(self.crf,text = 'Go to Login',bd = 1 ,font = ('',15),padx=5,pady=5,command=self.log, bg="black", fg="white").grid(row=2,column=1) if __name__=='__main__': root = Tk() root.geometry("600x480+320+100") root.title('Login Form') application = user(root) root.mainloop()
Portfolio_frontend.py file
In this file, we write the code for Creating the GUI for the front end of the Portfolio Management page with the help of Tkinter Library.
from tkinter import * from tkinter import ttk import random from datetime import datetime import tkinter.messagebox import Portfolio_backend class Portfolio: def __init__(self, root): self.root = root self.root.title('Manage your Portfolio') self.root.geometry('1250x600+10+10') self.root.config(bg='#F8C4B4') # =============================Variables============================= self.Mtype = StringVar() self.refno = StringVar() self.fname = StringVar() self.surname = StringVar() self.address = StringVar() self.post = StringVar() self.mobno = StringVar() self.ID = StringVar() self.title = StringVar() self.Company = StringVar() self.borrow = StringVar() self.due = StringVar() self.loan = StringVar() self.yop = StringVar() self.edsn = StringVar() # =========================Functions=================================== def Rec(event): try: global selected_tuple index = self.Listbox_2.curselection()[0] selected_tuple = self.Listbox_2.get(index) self.Entry_0.delete(0, END) self.Entry_0.insert(END, selected_tuple[1]) self.Entry_1.delete(0, END) self.Entry_1.insert(END, selected_tuple[2]) self.Entry_2.delete(0, END) self.Entry_2.insert(END, selected_tuple[3]) self.Entry_3.delete(0, END) self.Entry_3.insert(END, selected_tuple[4]) self.Entry_4.delete(0, END) self.Entry_4.insert(END, selected_tuple[5]) self.Entry_5.delete(0, END) self.Entry_5.insert(END, selected_tuple[6]) self.Entry_6.delete(0, END) self.Entry_6.insert(END, selected_tuple[7]) self.Entry_7.delete(0, END) self.Entry_7.insert(END, selected_tuple[8]) self.Entry_8.delete(0, END) self.Entry_8.insert(END, selected_tuple[9]) self.Entry_9.delete(0, END) self.Entry_9.insert(END, selected_tuple[10]) self.Entry_10.delete(0, END) self.Entry_10.insert(END, selected_tuple[11]) self.Entry_11.delete(0, END) self.Entry_11.insert(END, selected_tuple[12]) self.Entry_12.delete(0, END) self.Entry_12.insert(END, selected_tuple[13]) except IndexError: pass def Insert(): if (len(self.refno.get()) != 0): Portfolio_backend.insert(self.Mtype.get(), self.refno.get(), self.fname.get(), self.surname.get(), self.address.get(), self.post.get( ), self.mobno.get(), self.ID.get(), self.title.get(), self.Company.get(), self.borrow.get(), self.due.get(), self.loan.get()) self.Listbox_2.delete(0, END) self.Listbox_2.insert(END, (self.Mtype.get(), self.refno.get(), self.fname.get(), self.surname.get(), self.address.get(), self.post.get( ), self.mobno.get(), self.ID.get(), self.title.get(), self.Company.get(), self.borrow.get(), self.due.get(), self.loan.get())) def Display(): self.Listbox_2.delete(0, END) for row in Portfolio_backend.view(): self.Listbox_2.insert(END, row, str(' ')) def Exit(): root.destroy() return def Reset(): self.Mtype.set('') self.refno.set('') self.fname.set('') self.surname.set('') self.address.set('') self.post.set('') self.mobno.set('') self.ID.set('') self.title.set('') self.Company.set('') self.borrow.set('') self.due.set('') self.loan.set('') self.Display.delete('1.0', END) self.Listbox_2.delete(0, END) def Delete(): Portfolio_backend.delete(selected_tuple[0]) Reset() Display() def Update(): Portfolio_backend.delete(selected_tuple[0]) Portfolio_backend.insert(self.Mtype.get(), self.refno.get(), self.fname.get(), self.surname.get(), self.address.get(), self.post.get( ), self.mobno.get(), self.ID.get(), self.title.get(), self.Company.get(), self.borrow.get(), self.due.get(), self.loan.get()) self.Listbox_2.delete(0, END) self.Listbox_2.insert(END, (self.Mtype.get(), self.refno.get(), self.fname.get(), self.surname.get(), self.address.get(), self.post.get( ), self.mobno.get(), self.ID.get(), self.title.get(), self.Company.get(), self.borrow.get(), self.due.get(), self.loan.get())) def Details(): self.Display.delete('1.0', END) self.Display.insert(END, 'ID: ' + self.ID.get() + '\n') self.Display.insert(END, 'Title: ' + self.title.get() + '\n') self.Display.insert(END, 'Company: ' + self.Company.get() + '\n') self.Display.insert(END, 'Min qty: ' + self.edsn.get() + '\n') self.Display.insert( END, 'Year: \t' + self.yop.get() + '\n') self.Display.insert(END, 'Issued Date: ' + self.borrow.get() + '\n') self.Display.insert(END, 'Due Date:' + self.due.get() + '\n') self.Display.insert(END, 'Annualized Return(%): ' + self.loan.get() + '%' + '\n') # ============================Frames=========================================== Main_Frame = Frame(self.root, bg='#F8C4B4') Main_Frame.grid() Title_Frame_1 = Frame(Main_Frame, width=1350, bg='#F8C4B4', relief=RIDGE, bd=15, padx=20) Title_Frame_1.pack(side=TOP) self.lblTitle = Label(Title_Frame_1, font=('arial', 15, 'bold'), text='\tPortfolio Management\t', bg='#F8C4B4', padx=13) self.lblTitle.grid() Button_Frame = Frame(Main_Frame, width=1350, height=50, relief=RIDGE, bd=5, bg='#F8C4B4') Button_Frame.pack(side=BOTTOM) Detail_Frame = Frame(Main_Frame, width=1350, height=100, relief=RIDGE, bd=5, bg='#F8C4B4') Detail_Frame.pack(side=BOTTOM) Data_Frame = Frame(Main_Frame, width=1350, height=400, relief=RIDGE, bd=5, bg='#F8C4B4') Data_Frame.pack(side=BOTTOM) Frame_1 = LabelFrame(Data_Frame, width=800, height=400, relief=RIDGE, bd=5, bg='#F8C4B4', text="Membership Info:", padx=20, font=('arial', 10, 'bold')) Frame_1.pack(side=LEFT, padx=3) Frame_2 = LabelFrame(Data_Frame, width=550, height=400, relief=RIDGE, bd=5, bg='#F8C4B4', text="Stocks Availabe:", padx=0, font=('arial', 10, 'bold')) Frame_2.pack(side=RIGHT) # =========================Labels============================= self.Label_1 = Label(Frame_1, text='Member type', font=('arial', 13, 'bold'), pady=2, bg='#F8C4B4') self.Label_1.grid(row=0, column=0, sticky=W) self.Label_2 = Label(Frame_1, text='Reference No.', font=('arial', 13, 'bold'), pady=2, bg='#F8C4B4') self.Label_2.grid(row=1, column=0, sticky=W) self.Label_3 = Label(Frame_1, text='First Name', font=('arial', 13, 'bold'), pady=2, bg='#F8C4B4') self.Label_3.grid(row=2, column=0, sticky=W) self.Label_4 = Label(Frame_1, text='Surname', font=('arial', 13, 'bold'), pady=2, bg='#F8C4B4') self.Label_4.grid(row=3, column=0, sticky=W) self.Label_5 = Label(Frame_1, text='Address', font=('arial', 13, 'bold'), pady=2, bg='#F8C4B4') self.Label_5.grid(row=4, column=0, sticky=W) self.Label_6 = Label(Frame_1, text='Post Code', font=('arial', 13, 'bold'), pady=2, bg='#F8C4B4') self.Label_6.grid(row=5, column=0, sticky=W) self.Label_7 = Label(Frame_1, text='Mobile No.', font=('arial', 13, 'bold'), pady=2, bg='#F8C4B4') self.Label_7.grid(row=6, column=0, sticky=W) self.Label_8 = Label(Frame_1, text='ID', font=('arial', 13, 'bold'), pady=2, bg='#F8C4B4') self.Label_8.grid(row=0, column=2, sticky=W) self.Label_9 = Label(Frame_1, text='Title', font=('arial', 13, 'bold'), pady=2, bg='#F8C4B4') self.Label_9.grid(row=1, column=2, sticky=W) self.Label_10 = Label(Frame_1, text='Company', font=('arial', 13, 'bold'), pady=2, bg='#F8C4B4') self.Label_10.grid(row=2, column=2, sticky=W) self.Label_11 = Label(Frame_1, text='Issued Date', font=('arial', 13, 'bold'), pady=2, bg='#F8C4B4') self.Label_11.grid(row=3, column=2, sticky=W) self.Label_13 = Label(Frame_1, text='Due Date', font=('arial', 13, 'bold'), pady=2, bg='#F8C4B4') self.Label_13.grid(row=4, column=2, sticky=W) self.Label_13 = Label(Frame_1, text='Annualized Return(%)', font=('arial', 13, 'bold'), pady=2, bg='#F8C4B4') self.Label_13.grid(row=5, column=2, sticky=W) # ===========================Entries===================================== self.Entry_0 = ttk.Combobox(Frame_1, values=(' ', 'Individual', 'Group', 'Hybrid'), font=('arial', 13, 'bold'), width=23, textvariable=self.Mtype) self.Entry_0.grid(row=0, column=1) self.Entry_1 = Entry(Frame_1, font=( 'arial', 13, 'bold'), width=25, textvariable=self.refno) self.Entry_1.grid(row=1, column=1, padx=15) self.Entry_2 = Entry(Frame_1, font=( 'arial', 13, 'bold'), width=25, textvariable=self.fname) self.Entry_2.grid(row=2, column=1, padx=15) self.Entry_3 = Entry(Frame_1, font=( 'arial', 13, 'bold'), width=25, textvariable=self.surname) self.Entry_3.grid(row=3, column=1, padx=15) self.Entry_4 = Entry(Frame_1, font=( 'arial', 13, 'bold'), width=25, textvariable=self.address) self.Entry_4.grid(row=4, column=1, padx=15) self.Entry_5 = Entry(Frame_1, font=( 'arial', 13, 'bold'), width=25, textvariable=self.post) self.Entry_5.grid(row=5, column=1, padx=15) self.Entry_6 = Entry(Frame_1, font=( 'arial', 13, 'bold'), width=25, textvariable=self.mobno) self.Entry_6.grid(row=6, column=1, padx=15) self.Entry_7 = Entry(Frame_1, font=( 'arial', 13, 'bold'), width=25, textvariable=self.ID) self.Entry_7.grid(row=0, column=4, padx=15) self.Entry_8 = Entry(Frame_1, font=( 'arial', 13, 'bold'), width=25, textvariable=self.title) self.Entry_8.grid(row=1, column=4, padx=15) self.Entry_9 = Entry(Frame_1, font=( 'arial', 13, 'bold'), width=25, textvariable=self.Company) self.Entry_9.grid(row=2, column=4, padx=15) self.Entry_10 = Entry(Frame_1, font=( 'arial', 13, 'bold'), width=25, textvariable=self.borrow) self.Entry_10.grid(row=3, column=4, padx=15) self.Entry_11 = Entry(Frame_1, font=( 'arial', 13, 'bold'), width=25, textvariable=self.due) self.Entry_11.grid(row=4, column=4, padx=15) self.Entry_12 = Entry(Frame_1, font=( 'arial', 13, 'bold'), width=25, textvariable=self.loan) self.Entry_12.grid(row=5, column=4, padx=15) # =====================Widgets===================================== self.Display = Text(Frame_2, font=( 'arial', 13, 'bold'), width=28, height=11.5) self.Display.grid(row=0, column=2) List_of_Books = [' DOLLEX', ' KFINTECH', ' UMA', ' ELIN', ' ALAN'] # =======================Function for Books Details======================== def SelectedBook(event): value = str(self.Listbox_1.get(self.Listbox_1.curselection())) v = value if (v == ' DOLLEX'): self.ID.set('ISBN 525341') self.title.set('DOLLEX') self.Company.set('Dollex Argotech Ltd') self.yop.set('2022') self.edsn.set('4000') import datetime d1 = datetime.date.today() d2 = datetime.timedelta(days=14) d3 = (d1 + d2) self.borrow.set(d1) self.loan.set('14') self.due.set(d3) Details() elif (v == ' KFINTECH'): self.ID.set('ISBN 345687') self.title.set('KFINTECH') self.Company.set('KFinTech Solutions Ltd') self.yop.set('2019') self.edsn.set('40') import datetime d1 = datetime.date.today() d2 = datetime.timedelta(days=10) d3 = (d1 + d2) self.borrow.set(d1) self.loan.set('10') self.due.set(d3) Details() elif (v == ' UMA'): self.ID.set('ISBN 643842') self.title.set('UMA') self.Company.set('UMA Solutions Ltd') self.yop.set('2017') self.edsn.set('2000') import datetime d1 = datetime.date.today() d2 = datetime.timedelta(days=13) d3 = (d1 + d2) self.borrow.set(d1) self.loan.set('13') self.due.set(d3) Details() elif (v == ' ELIN'): self.ID.set('ISBN 564524') self.title.set('ELIN') self.Company.set('ELIN Electronics Ltd') self.yop.set('2018') self.edsn.set('60') import datetime d1 = datetime.date.today() d2 = datetime.timedelta(days=13) d3 = (d1 + d2) self.borrow.set(d1) self.loan.set('13') self.due.set(d3) Details() elif (v == ' ALAN'): self.ID.set('ISBN 735893') self.title.set('ALAN') self.Company.set('Alan Solutions Ltd') self.yop.set('2021') self.edsn.set('1200') import datetime d1 = datetime.date.today() d2 = datetime.timedelta(days=15) d3 = (d1 + d2) self.borrow.set(d1) self.loan.set('15') self.due.set(d3) Details() # ====================List Box and Scroll Bar================================= self.Listbox_1 = Listbox(Frame_2, font=( 'arial', 13, 'bold'), width=20, height=11) self.Listbox_1.bind('<<ListboxSelect>>', SelectedBook) self.Listbox_1.grid(row=0, column=0) self.Listbox_2 = Listbox(Detail_Frame, font=( 'arial', 13, 'bold'), width=144, height=8) self.Listbox_2.bind('<<ListboxSelect>>', Rec) self.Listbox_2.grid(row=1, column=0) for items in List_of_Books: self.Listbox_1.insert(END, items) # ====================Buttons======================================== Button_1 = Button(Button_Frame, text='Add', font=( 'arial', 10, 'bold'), bg='#FF8787', width=10, command=Insert) Button_1.grid(row=0, column=0, padx=8, pady=5) Button_2 = Button(Button_Frame, text='DISPLAY', font=( 'arial', 10, 'bold'), bg='#FF8787', width=10, command=Display) Button_2.grid(row=0, column=1, padx=8) Button_3 = Button(Button_Frame, text='RESET', font=( 'arial', 10, 'bold'), bg='#FF8787', width=10, command=Reset) Button_3.grid(row=0, column=2, padx=8) Button_4 = Button(Button_Frame, text='UPDATE', font=( 'arial', 10, 'bold'), bg='#FF8787', width=10, command=Update) Button_4.grid(row=0, column=3, padx=8) Button_6 = Button(Button_Frame, text='DELETE', font=( 'arial', 10, 'bold'), bg='#FF8787', width=10, command=Delete) Button_6.grid(row=0, column=5, padx=8) Button_7 = Button(Button_Frame, text='EXIT', font=( 'arial', 10, 'bold'), bg='#FF8787', width=10, command=Exit) Button_7.grid(row=0, column=6, padx=8) if __name__ == '__main__': root = Tk() applicaton = Portfolio(root) root.mainloop()
Portfolio_backend.py file
In this file, we write the backend logic for the Portfolio Management page to be able to communicate with the database and do different operations.
import sqlite3 def connect(): con = sqlite3.connect('Portfolio.db') cur = con.cursor() cur.execute('CREATE TABLE IF NOT EXISTS Portfolio(x INTEGER PRIMARY KEY, Mtype text, refno integer, fname text, \ surname text, address text, post integer, mobno integer, ID text, title text, Company text, \ borrow integer, due integer, loan integer)') con.commit() con.close() def insert(Mtype=' ', refno=' ', fname=' ', surname=' ', address=' ', post=' ', mobno=' ', ID=' ', title=' ', Company=' ', borrow=' ', due=' ', loan=' '): con = sqlite3.connect('Portfolio.db') cur = con.cursor() cur.execute('INSERT INTO Portfolio VALUES (NULL,?,?,?,?,?,?,?,?,?,?,?,?,?)', (Mtype, refno, fname, surname, address, post, mobno, ID, title, Company, borrow, due, loan)) con.commit() con.close() def view(): con = sqlite3.connect('Portfolio.db') cur = con.cursor() cur.execute('SELECT * FROM Portfolio') row = cur.fetchall() con.close() return row def delete(x): con = sqlite3.connect('Portfolio.db') cur = con.cursor() cur.execute('DELETE FROM Portfolio WHERE x = ?', (x,)) con.commit() con.close() def update(x, Mtype=' ', refno=' ', fname=' ', surname=' ', address=' ', post=' ', mobno=' ', ID=' ', title=' ', Company=' ', borrow=' ', due=' ', loan=' '): con = sqlite3.connect('Portfolio.db') cur = con.cursor() cur.execute('UPDATE Portfolio SET Mtype = ? OR refno = ? OR fname = ? OR surname = ? OR address = ? OR post = ? OR \ mobno = ? OR ID = ? OR title = ? OR Company = ? OR borrow = ? OR due = ? OR loan = ?', (Mtype, refno, fname, surname, address, post, mobno, ID, title, Company, borrow, due, loan)) con.commit() con.close() def search(Mtype=' ', refno=' ', fname=' ', surname=' ', address=' ', post=' ', mobno=' ', ID=' ', title=' ', Company=' ', borrow=' ', due=' ', loan=' '): con = sqlite3.connect('Portfolio.db') cur = con.cursor() cur.execute('SELECT * FROM Portfolio WHERE Mtype = ? OR refno = ? OR fname = ? OR surname = ? OR address = ? OR \ post = ? OR mobno = ? OR ID = ? OR title = ? OR Company = ? OR borrow = ? OR due = ? OR loan = ?', (Mtype, refno, fname, surname, address, post, mobno, ID, title, Company, borrow, due, loan)) row = cur.fetchall() con.close() return row connect()
Output for Portfolio Management System in Python
Image Output:
Video Output:
Conclusion
Here, we have built a simple Portfolio Management System in Python with the help of Tkinter Library and SQLite Database. Hopefully, you would have learned something useful from this project. Try to add more features and change the UI of the project to make it more interesting.
Thank you for visiting our website.
Also Read:
- Create your own ChatGPT with Python
- SQLite | CRUD Operations in Python
- Event Management System Project in Python
- Ticket Booking and Management in Python
- Hostel Management System Project in Python
- Sales Management System Project in Python
- Bank Management System Project in C++
- Python Download File from URL | 4 Methods
- Python Programming Examples | Fundamental Programs in Python
- Spell Checker in Python
- Portfolio Management System in Python
- Stickman Game in Python
- Contact Book project in Python
- Loan Management System Project in Python
- Cab Booking System in Python
- Brick Breaker Game in Python
- Tank game in Python
- GUI Piano in Python
- Ludo Game in Python
- Rock Paper Scissors Game in Python
- Snake and Ladder Game in Python
- Puzzle Game in Python
- Medical Store Management System Project in Python
- Creating Dino Game in Python
- Tic Tac Toe Game in Python
- Test Typing Speed using Python App
- Scientific Calculator in Python
- GUI To-Do List App in Python Tkinter
- Scientific Calculator in Python using Tkinter
- GUI Chat Application in Python Tkinter