class loginPage(Frame): ''' This is a frame class and thus it implements Frame, it also contains the required showItems and hideItems function which I believe are present in each frame class tomake things a bit simpler ''' def __init__(self, parent, main, **kw): Frame.__init__(self, parent, **kw) self.main = main self.label_head = Label(text='Welcome to the Stock Exchange', font=LARGE_FONT) self.l_user = Label(text='Username') self.user = Entry() self.l_pass = Label(text='Password') self.password = Entry(show='*') self.login_back_b = Button(text='Login', command=lambda: self.login_check()) self.sign_up_b = Button( text='Sign Up', command=lambda: self.main.show_frame(SIGN_UP_PAGE, LOGIN_PAGE)) self.view_stock_b = Button( text='View Stock', command=lambda: self.main.show_frame(VIEW_STOCK, LOGIN_PAGE)) self.main.bind('<Return>', self.keyPress) def showItems(self, main): self.label_head.grid(column=0, row=0, columnspan=2) self.l_user.grid(column=0, row=1) self.user.grid(column=1, row=1) self.l_pass.grid(column=0, row=2) self.password.grid(column=1, row=2) self.login_back_b.grid(column=0, row=3, columnspan=2) self.sign_up_b.grid(column=0, row=4) self.view_stock_b.grid(column=1, row=4) def hideItems(self, main): self.label_head.grid_forget() self.l_user.grid_forget() self.user.grid_forget() self.l_pass.grid_forget() self.password.grid_forget() self.login_back_b.grid_forget() self.sign_up_b.grid_forget() self.view_stock_b.grid_forget() def keyPress(self, event): ''' this is the binding fucntion for any keyPress event ''' if event.keysym == 'Return': self.login_check() def login_check(self): ''' This function checks whether a given user is registered on the system or not then continues to chech whether they have entered the correct password. it also creates messagesboxes to give the proper error message to the user ''' main = self.main user = str(self.user.get()) password = bytes(str(self.password.get()), 'utf-8') main.login = False check1 = user in main.pass_dict check3 = user == 'admin' and password == 'admin' if check1: check2 = main.pass_dict[user] == password if check1 and check2: main.login = True else: main.login = False if check3: main.login = True main.admin = True main.show_frame(ADMIN_PAGE, LOGIN_PAGE) if main.login: if not check3: main.present_user, main.p_user_dict = user, main.users_dict[ user] main.show_frame(VIEW_STOCK, LOGIN_PAGE) self.password.delete(0, END) self.user.delete(0, END) else: self.password.delete(0, END) showinfo(message='Incorrect Username or Password entered')
class signUpPage(Frame): ''' The user is taken to this Frame if they choose to sign up this class implements the Frame Class in Tkinter and contains the required functions that will allow it to work well in this project ''' def __init__(self, parent, main, **kw): Frame.__init__(self, parent, **kw) self.main = main self.label_head = Label(text='Sign Up Page', font=MED_FONT) self.l_user = Label(text='Username') self.user = Entry(text='must have atleast 5 chars') self.l_pass = Label(text='Password') self.l_pass2 = Label(text='re-enter') self.password = Entry(show='*') self.password2 = Entry(show='*') self.sign_up_b = Button(text='Sign Up', command=lambda: self.sign_up(main)) self.back_b = Button( text='Back', command=lambda: self.main.show_frame(LOGIN_PAGE, SIGN_UP_PAGE)) self.age = BooleanVar() self.age_c = Checkbutton(text='Are you above 16 years of age', variable=self.age, onvalue=True, offvalue=False) self.balance = BooleanVar() self.balance_c = Checkbutton( text='Do you have 10000 rupees in \nyour bank account', variable=self.balance, onvalue=True, offvalue=False) def showItems(self, main): self.label_head.grid(column=0, row=0, columnspan=2) self.l_user.grid(column=0, row=1) self.user.grid(column=1, row=1) self.l_pass.grid(column=0, row=2) self.l_pass2.grid(column=0, row=3) self.password.grid(column=1, row=2) self.password2.grid(column=1, row=3) self.age_c.grid(column=0, columnspan=2, row=4) self.balance_c.grid(column=0, columnspan=2, row=5) self.sign_up_b.grid(column=0, row=6, columnspan=2) self.back_b.grid(column=0, row=7, columnspan=2) def hideItems(self, main): self.label_head.grid_forget() self.l_user.grid_forget() self.user.grid_forget() self.l_pass.grid_forget() self.l_pass2.grid_forget() self.password.grid_forget() self.password2.grid_forget() self.sign_up_b.grid_forget() self.back_b.grid_forget() self.age_c.grid_forget() self.balance_c.grid_forget() def sign_up(self, main): ''' Similar to the login check function it does the necessary checks to make sure that a person actually is above 18 (not really) and has the required amount of money to open an account on this platform also it creates pop ups when ever an exceptional circumstance is reached ''' password1 = self.password.get() password2 = self.password2.get() username = self.user.get() bool1 = username not in main.users_dict.keys() bool2 = (password1 == password2) bool3 = len(password1) >= 5 bool4 = username == 'admin' bool5 = self.age.get() bool6 = self.balance.get() if not bool1: num = str(randint(100, 999)) showinfo(message='Username already exists, try: ' + username + num) self.password.delete(0, END) self.password2.delete(0, END) elif not bool2: showinfo(message='Passwords dont match') self.password.delete(0, END) self.password2.delete(0, END) elif not bool3: showinfo(message='Password must be more than 5 characters') self.password.delete(0, END) self.password2.delete(0, END) elif bool4: showinfo(message='Please don\'t use that username it is reserved') elif not bool5: showinfo(message='You must be 16 years or older to join') elif not bool6: showinfo(message='You need to have 10000 to create an account') else: buff = [] for name in main.shares_dict.keys(): temp = {} temp['name'] = name temp['tot_owned'] = '0' temp['money_spent'] = '0' buff.append(temp) self.main.users_dict[username] = buff self.main.pass_dict[username] = bytes(password1, 'utf-8') k = getData.key() self.main.pass_dict2[username] = encode(bytes(password1, 'utf-8')) self.main.accounts[username] = { 'balance': str(10**4), 'profit': '0' } self.main.present_user = username self.main.p_user_dict = self.main.users_dict[username] self.main.login = True self.main.show_frame(VIEW_STOCK, SIGN_UP_PAGE) self.password.delete(0, END) self.password2.delete(0, END) self.user.delete(0, END)
class retailPage(Frame): ''' the main page that actually branches into 2 seperate frames for buying and selling this frame provides the use with the ''' def __init__(self, parent, main, **kw): Frame.__init__(self, parent, **kw) self.main = main self.Head = Label(font=MED_FONT) self.retail_back_b = Button(text='BACK', command=self.back_fb) self.stock_name = StringVar() self.stock_name_ = '' self.stock_name_c = Combobox(textvariable=self.stock_name) self.stock_name_l = Label(text='Stock name: ') self.amount = Entry() self.amount_l = Label(text='Number of stocks :') self.check_avail_b = Button(text='Check') self.cost_l = Label(text='Cost: ') self.cost = Label() self.buy_stock_b = Button(text='BUY Stock') self.sellp_l = Label(text='Current Selling Price: ') self.profit_l = Label(text='PROFIT/LOSS') self.sell_stock_b = Button(text='SELL Stock') self.page = None def showItems(self, main): self.Head.grid(column=0, row=0, columnspan=2) self.Head.configure(text='logged in as %s' % main.present_user) if self.page == 'b': self.buy() elif self.page == 's': self.sell() def hideItems(self, main): self.Head.grid_forget() def buy(self): '''This function creates the buy window ''' self.retail_back_b.grid(column=1, row=5) self.retail_back_b.config(command=self.back_fb) self.stock_name_l.grid(column=0, row=1) self.stock_name_c.grid(column=1, row=1) self.stock_name_c.config(values=self.main.shares_dict.keys()) self.amount_l.grid(column=0, row=2) self.amount.grid(column=1, row=2) self.check_avail_b.config(command=self.check_avail_buy) self.check_avail_b.grid(column=0, row=3) self.cost_l.grid(column=0, row=4, columnspan=2) self.buy_stock_b.config(command=self.buy_stock, text='Buy Stock', state='disabled') self.buy_stock_b.grid(column=0, row=5) def sell(self): '''This function creats the sell window''' self.retail_back_b.grid(column=1, row=6) self.retail_back_b.config(command=self.back_fs) self.check_avail_b.config(command=self.check_avail_sell) self.stock_name_l.grid(column=0, row=1) self.stock_name_c.grid(column=1, row=1) self.stock_name_c.config(values=self.main.shares_dict.keys()) self.amount_l.grid(column=0, row=2) self.amount.grid(column=1, row=2) self.check_avail_b.grid(column=0, row=3) self.sellp_l.grid(column=0, row=4, columnspan=2) self.profit_l.grid(column=0, row=5, columnspan=2) self.sell_stock_b.config(command=self.sell_stock, state='disabled', text='Check') self.sell_stock_b.grid(column=0, row=6) def back_fb(self): '''Back from buy i.e. removes all the items needed to make buy''' self.retail_back_b.grid_forget() self.Head.grid(column=0, row=0, columnspan=2) self.stock_name_l.grid_forget() self.stock_name_c.grid_forget() self.amount_l.grid_forget() self.amount.grid_forget() self.check_avail_b.grid_forget() self.cost_l.grid_forget() self.buy_stock_b.grid_forget() self.buy_stock_b.config(state='disabled') self.main.show_frame(VIEW_STOCK, RETAIL_PAGE) self.stock_name.set('') self.amount.delete(0, END) def back_fs(self): ''' Back from sell i.e. removes all the items needed to make it sell window''' self.Head.grid(column=0, row=0, columnspan=2) self.retail_back_b.grid_forget() self.check_avail_b.grid_forget() self.stock_name_l.grid_forget() self.stock_name_c.grid_forget() self.amount_l.grid_forget() self.amount.grid_forget() self.sellp_l.grid_forget() self.profit_l.grid_forget() self.sell_stock_b.grid_forget() self.main.show_frame(VIEW_STOCK, RETAIL_PAGE) self.stock_name.set('') self.amount.delete(0, END) self.check_avail_b.grid_forget() def check_avail_buy(self): ''' Performs a check whether the number of shares requisted are available or not and then check whether the person has the required amounts of fund or not for the transaction to go through''' name = self.stock_name.get() l2 = self.main.accounts[self.main.present_user] if name in self.main.shares_dict.keys(): li = self.main.shares_dict[name] else: self.stock_name.delete(0, END) showinfo(meassage='Enter a Valid Stock name') available_num = int(li['tot_amount']) - int(li['tot_sold']) req = int(self.amount.get()) cost = req * int(li['cost']) if req < 0: showinfo(message='Enter a Valid amount') elif req > available_num: showinfo(message='Enter an amount less than ' + str(available_num)) elif cost > int(l2['balance']): showinfo(message='You have only %s in you account' % l2['balance']) else: self.cost_l.config(text='Cost: \t' + li['cost'] + '*' + str(req) + '=' + str(cost)) self.buy_stock_b.config(state='normal') def check_avail_sell(self): ''' Performs a check whether the user has enough stocks to sell''' name = self.stock_name.get() if name in self.main.shares_dict.keys(): li = self.main.shares_dict[name] else: self.stock_name.delete(0, END) showinfo(message='Enter a Valid Stock name') req = int(self.amount.get()) if req < 0: showinfo(message='Please Enter a Valid amount') self.amount.delete(0, END) li = self.main.p_user_dict ok = False for i in li: if name == i['name']: ok = True buff = i if req > int(buff['tot_owned']): showinfo(message='You dont have that many stocks try less than ' + buff['tot_owned']) self.amount.delete(0, END) cost = self.main.shares_dict[name]['cost'] tot_cost = req * int(cost) try: spent = req * float(buff['money_spent']) / int(buff['tot_owned']) except: spent = 0 pol = tot_cost - spent if pol >= 0: self.profit_l.config() elif pol < 0: self.profit_l.config() if req <= int(buff['tot_owned']): self.sellp_l.config(text='Current Selling Price: \t' + cost) self.profit_l.config(text="PROFIT-LOSS: \t" + str(pol)) self.sell_stock_b.config(command=self.sell_stock, text='Sell Stock') showinfo(message= 'Everthing is ok, \nClicking Sell will execute the trade') self.sell_stock_b.config(state='normal') def buy_stock(self): '''Finally Executes the transaction and asks the user for conformation one last time''' name = self.stock_name.get() li = self.main.shares_dict[name] for i in range(len(self.main.p_user_dict)): if name == self.main.p_user_dict[i]['name']: index = i req = int(self.amount.get()) tot_cost = req * int(li['cost']) self.main.shares_dict[name]['tot_sold'] = str( int(self.main.shares_dict[name]['tot_sold']) + req) self.main.p_user_dict[index]['tot_owned'] = str( int(self.main.p_user_dict[index]['tot_owned']) + req) self.main.p_user_dict[index]['money_spent'] = str( int(self.main.p_user_dict[index]['money_spent']) + tot_cost) self.main.users_dict[ self.main.present_user][index] = self.main.p_user_dict[index] balance = int(self.main.accounts[self.main.present_user]['balance']) self.main.accounts[self.main.present_user]['balance'] = str(balance - tot_cost) self.cost_l.config(text='Cost: ') showinfo( message='You have just bought %s of the stock %s at the price %s' % (str(req), name, str(tot_cost))) self.stock_name.set('') self.main.show_frame(VIEW_STOCK, RETAIL_PAGE) self.back_fb() def sell_stock(self): '''Asks the user for conformation and then completes the transaction of selling, at this point the profit field is also updated''' name = self.stock_name.get() req = int(self.amount.get()) li = self.main.p_user_dict for i in range(len(li)): if name == li[i]['name']: ok = True buff = i tot_cost = req * int(self.main.shares_dict[name]['cost']) try: spent = req * float( self.main.p_user_dict[buff]['money_spent']) / int( self.main.p_user_dict[buff]['tot_owned']) except ZeroDivisionError: spent = 0 self.main.shares_dict[name]['tot_sold'] = str( int(self.main.shares_dict[name]['tot_sold']) - req) self.main.p_user_dict[buff]['tot_owned'] = str( int(self.main.p_user_dict[buff]['tot_owned']) - req) pol = tot_cost - spent diff = int(self.main.p_user_dict[buff]['money_spent']) - tot_cost self.main.p_user_dict[buff]['money_spent'] = str( diff) if diff > 0 else '0' self.main.users_dict[self.main.present_user] = self.main.p_user_dict profit = int(self.main.accounts[self.main.present_user]['profit']) self.main.accounts[self.main.present_user]['profit'] = str( int(profit + pol)) balance = int(self.main.accounts[self.main.present_user]['balance']) self.main.accounts[self.main.present_user]['balance'] = str(balance + tot_cost) self.retail_back_b.grid_forget() self.profit_l.config(text='PROFIT/LOSS: ') self.sellp_l.config(text='Current selling price: ') self.main.show_frame(VIEW_STOCK, RETAIL_PAGE) self.back_fs()
class adminPage(Frame): def __init__(self, parent, main, **kw): Frame.__init__(self, parent, **kw) self.main = main self.label_head = Label(text='Admin Page', font=MED_FONT) self.adm_stock_l = Label(text='Stock Name: ') self.adm_stock = StringVar() self.adm_stock_c = Combobox(textvariable=self.adm_stock) self.adm_ncost_l = Label(text='New Cost of Stock: ') self.adm_namount_l = Label(text='New number of stocks added: ') self.adm_namount = Entry() self.adm_ncost = Entry() self.adm_update_b = Button(text='UPDATE', command=self.update) self.back_b = Button(text='Log Out', command=self.back) self.view_f_text = Text(height=20, width=30) self.view_scroll = Scrollbar(orient=VERTICAL, command=self.view_f_text.yview) self.view_f_text.config(yscrollcommand=self.view_scroll.set) self.new_stock = False def back(self): self.main.show_frame(LOGIN_PAGE, ADMIN_PAGE) self.main.login = False self.main.admin = False def showItems(self, main): self.label_head.grid(column=0, row=0, columnspan=3) self.adm_stock_l.grid(column=0, row=1) self.adm_stock_c.grid(column=1, row=1) self.adm_stock_c.config(values=main.shares_dict.keys()) self.adm_ncost_l.grid(column=0, row=2) self.adm_ncost.grid(column=1, row=2) self.adm_namount_l.grid(column=0, row=3) self.adm_namount.grid(column=1, row=3) self.adm_update_b.grid(column=0, row=4) self.back_b.grid(column=1, row=4) self.view_stock() self.view_f_text.grid(column=0, row=5, sticky='nsew', columnspan=2) self.view_scroll.grid(column=2, row=5, sticky='ns') def hideItems(self, main): self.label_head.grid_forget() self.adm_stock_l.grid_forget() self.adm_stock_c.grid_forget() self.adm_ncost_l.grid_forget() self.adm_ncost.grid_forget() self.adm_namount_l.grid_forget() self.adm_namount.grid_forget() self.adm_update_b.grid_forget() self.back_b.grid_forget() self.view_f_text.grid_forget() self.view_scroll.grid_forget() def update(self): stock_name = self.adm_stock.get() ncost = int(self.adm_ncost.get()) namount = self.adm_namount.get() if ncost < 0: showinfo(message='Enter valid cost, Please check data') self.adm_ncost.delete(0, END) elif namount == '': namount = '0' elif int(namount) < 0: showinfo(message='Enter valid amount, Please check data') self.adm_namount.delete(0, END) elif stock_name not in self.main.shares_dict: if not self.new_stock: showinfo( message= 'Are you sure you want to add this new stock \n Check the details once again' ) self.new_stock = True else: self.main.shares_dict[stock_name] = {} self.main.shares_dict[stock_name]['cost'] = str(ncost) self.main.shares_dict[stock_name]['tot_amount'] = namount self.main.shares_dict[stock_name]['name'] = stock_name self.main.shares_dict[stock_name]['tot_sold'] = '0' self.new_stock = False buff = { 'tot_owned': '0', 'name': stock_name, 'money_spent': '0' } for name in self.main.users_dict: self.main.users_dict[name].append(buff) self.adm_namount.delete(0, END) self.adm_ncost.delete(0, END) self.view_stock() self.adm_stock.set('') else: self.main.shares_dict[stock_name]['cost'] = str(ncost) tot_amount = int(self.main.shares_dict[stock_name]['tot_amount']) self.main.shares_dict[stock_name]['tot_amount'] = str(tot_amount + int(namount)) self.adm_namount.delete(0, END) self.adm_ncost.delete(0, END) self.view_stock() def view_stock(self): self.view_f_text.config(state='normal') self.view_f_text.delete('1.0', 'end') key = self.main.shares_dict.keys() for name in key: li = self.main.shares_dict[name] txt_b1 = ' ' * 10 + 'Name: ' self.view_f_text.insert('end', txt_b1) self.view_f_text.insert('end', name + '\n') txt_b2 = ' ' * 10 + 'Cost Price: ' self.view_f_text.insert('end', txt_b2) self.view_f_text.insert('end', li['cost'] + '\n') available_num = int(li['tot_amount']) - int(li['tot_sold']) txt_b3 = ' ' * 10 + 'Available amount: ' self.view_f_text.insert('end', txt_b3) self.view_f_text.insert('end', str(available_num) + '\n') txt_b4 = ' ' * 10 + '*' * 20 self.view_f_text.insert('end', '\n' + txt_b4 + '\n\n') self.view_f_text.config(state='disabled')