Advertisement
Guest User

Untitled

a guest
Jan 12th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 21.50 KB | None | 0 0
  1. import tkinter as tk                # python 3
  2. from tkinter import font  as tkfont # python 3
  3. from tkinter import messagebox
  4. from tkinter import *
  5. from bank_system import *
  6. from globalfunc import *
  7. from customer import Customer
  8. from admin import Admin
  9. from account import Account
  10.  
  11. #import tkFont as tkfont  # python 2
  12.  
  13. current_customer = None
  14. current_admin = None
  15. bank_system = BankSystem() #enables the program to refer to functions within the BankSystem class
  16.  
  17.  
  18.  
  19. #------ Global Functions -------#
  20.  
  21. def print_balance():
  22.     tk.messagebox.showwarning(
  23.         "New balance",
  24.         "Your new balance is: %.2f ... Please login again to confirm adjustments to account" %current_customer.get_account().balance)
  25.  
  26.  
  27.  
  28.  
  29. class SampleApp(tk.Tk):
  30.  
  31.  
  32.  
  33.     def __init__(self, *args, **kwargs):
  34.         tk.Tk.__init__(self, *args, **kwargs)
  35.  
  36.         self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
  37.         self.body_font = tkfont.Font(family='arial', size=14, weight='normal', slant='roman')
  38.  
  39.         # the container is where we'll stack a bunch of frames
  40.         # on top of each other, then the one we want visible
  41.         # will be raised above the others
  42.         container = tk.Frame(self)
  43.         container.pack(side="top", fill="both", expand=True)
  44.         container.grid_rowconfigure(0, weight=1)
  45.         container.grid_columnconfigure(0, weight=1)
  46.  
  47.         self.frames = {}
  48.         for F in (StartPage, CustomerLogin, AdminMenu,AdminLogin, CustomerMenu , CustDepositMoney, CustWithdrawMoney,
  49.                   CustUpdateName, CustUpdateAdd, CustomerAccountOptions, AdminCustomerInformation, EditCustomerDetails,
  50.                   DeleteCustomer):
  51.             page_name = F.__name__
  52.             frame = F(parent=container, controller=self)
  53.             self.frames[page_name] = frame
  54.  
  55.             # put all of the pages in the same location;
  56.             # the one on the top of the stacking order
  57.             # will be the one that is visible.
  58.             frame.grid(row=0, column=0, sticky="nsew")
  59.  
  60.         self.show_frame("StartPage")
  61.  
  62.     def show_frame(self, page_name):
  63.         '''Show a frame for the given page name'''
  64.         frame = self.frames[page_name]
  65.         frame.tkraise()
  66.         return frame
  67.  
  68.  
  69. class StartPage(tk.Frame):
  70.  
  71.     def __init__(self, parent, controller):
  72.         tk.Frame.__init__(self, parent)
  73.         self.controller = controller
  74.         label = tk.Label(self, text="Welcome to the Login Page", font=controller.title_font)
  75.         label.pack(side="top", fill="x", pady=10)
  76.  
  77.         button1 = tk.Button(self, text="Go to Customer login",
  78.                             command=lambda: controller.show_frame("CustomerLogin"))
  79.         button2 = tk.Button(self, text="Go to Admin login",
  80.                             command=lambda: controller.show_frame("AdminLogin"))
  81.         button1.pack()
  82.         button2.pack()
  83.  
  84.  
  85. class CustomerLogin(tk.Frame):
  86.  
  87.  
  88.     def __init__(self, parent, controller,):
  89.         bank_system.customers_list = []
  90.         bank_system.admins_list = []
  91.         bank_system.load_bank_data()
  92.  
  93.         global usernameentry
  94.         global passwordentry
  95.  
  96.         tk.Frame.__init__(self, parent,)
  97.         self.controller = controller
  98.         label = tk.Label(self, text="Customer Login", font=controller.title_font)
  99.         label.pack(side="top", fill="x", pady=10)
  100.         label2 = tk.Label(self, text="Enter Username:")
  101.         label2.pack(side="top", fill="x", pady=3)
  102.         usernameentry = tk.Entry(self,)
  103.         usernameentry.pack(side="top", expand=50)
  104.         label3 = tk.Label(self, text="Enter Password:")
  105.         label3.pack(side="top", fill="x", pady=3)
  106.         passwordentry = tk.Entry(self, show="*")
  107.         passwordentry.pack(side="top", expand=50)
  108.  
  109.         LoginButton = tk.Button(self, text="Login",
  110.                                 command=self.CustomerLogin)
  111.         LoginButton.pack(side="top", expand=50)
  112.         button = tk.Button(self, text="Go to the start page",
  113.                            command=lambda:  controller.show_frame("StartPage"),).pack(side="right",)
  114.  
  115.     #def passwordentry_delete(self):
  116.        # passwordentry.delete(first=20,last=22)
  117.  
  118.     def CustomerLogin(self):
  119.         name = str(usernameentry.get())
  120.         password = str(passwordentry.get())
  121.         found_customer = bank_system.search_customers_by_name(name)
  122.         if found_customer == None:
  123.             tk.messagebox.showwarning(
  124.                 "ERROR!",
  125.                 "Customer not found! - Please Try Again!")
  126.         else:
  127.             if (found_customer.check_password(password) == True):
  128.                 print('Customer found - Running customer page')
  129.                 frame = self.controller.show_frame("CustomerMenu")
  130.                 global current_customer
  131.                 current_customer = found_customer
  132.                 frame.refresh()
  133.             else:
  134.                 tk.messagebox.showwarning(
  135.                     "ERROR!",
  136.                     "Incorrect Password - Please Try Again!")
  137.  
  138.  
  139. class CustomerMenu(tk.Frame):
  140.     global nameprint
  141.     def __init__(self, parent, controller):
  142.         global bal
  143.         global getname
  144.  
  145.  
  146.         tk.Frame.__init__(self, parent)
  147.         self.controller = controller
  148.  
  149.         label = tk.Label(self, text="Customer Menu", font=controller.title_font)
  150.         label.pack(side="top", fill="x",)
  151.  
  152.         depositbutton = tk.Button(self, text="Deposit Money", command=lambda: controller.show_frame("CustDepositMoney"))
  153.         depositbutton.pack()
  154.  
  155.         withdrawbutton = tk.Button(self, text="Withdraw Money", command=lambda: controller.show_frame("CustWithdrawMoney"))
  156.         withdrawbutton.pack()
  157.  
  158.         label2 = tk.Label(self, text="Current Account Balance:", font=controller.body_font)
  159.         label2.pack(side="top",)
  160.  
  161.         self.bal = tk.StringVar()
  162.         label3 = tk.Label(self, textvariable=self.bal,)
  163.         label3.pack(side="top",)
  164.  
  165.  
  166.         profilelabel = tk.Label(self, text="Customer information:", font=controller.title_font)
  167.         profilelabel.pack(side="top")
  168.  
  169.  
  170.         labelnameprint = tk.Label(self, text="Customer Name:", font=controller.body_font)
  171.         labelnameprint.pack(side="top",)
  172.  
  173.         self.nameprint = tk.StringVar()
  174.         namelabel = tk.Label(self, textvariable=self.nameprint,)
  175.         namelabel.pack(side="top",)
  176.  
  177.         labeladdressprint = tk.Label(self, text="Customer Address:", font=controller.body_font)
  178.         labeladdressprint.pack(side="top",)
  179.  
  180.         self.addressprint = tk.StringVar()
  181.         addresslabel = tk.Label(self, textvariable=self.addressprint,)
  182.         addresslabel.pack(side="top",)
  183.  
  184.         updatename = tk.Button(self, text="Update name", command=lambda: controller.show_frame("CustUpdateName"))
  185.         updatename.pack()
  186.  
  187.         updateaddress = tk.Button(self, text="Update address", command=lambda: controller.show_frame("CustUpdateAdd"))
  188.         updateaddress.pack()
  189.  
  190.         returnbutton = tk.Button(self, text="Back",
  191.                            command=lambda: controller.show_frame("StartPage"))
  192.         returnbutton.pack(side="left")
  193.  
  194.  
  195.  
  196.     def refresh(self):
  197.  
  198.         self.bal.set("Balance: £%.2f" % current_customer.get_account().balance)
  199.         self.nameprint.set(current_customer.get_name())
  200.  
  201.         addr = ""
  202.         for a in current_customer.get_address():
  203.             addr += "%s\n" % a
  204.  
  205.         self.addressprint.set(addr)
  206.  
  207.  
  208.  
  209.  
  210. class CustDepositMoney(tk.Frame):
  211.  
  212.  
  213.  
  214.  
  215.     def __init__(self, parent, controller):
  216.         global depositentry
  217.         global deposit
  218.  
  219.         tk.Frame.__init__(self, parent)
  220.         self.controller = controller
  221.         label = tk.Label(self, text="Deposit Money", font=controller.title_font)
  222.         label.pack(side="top", fill="x",)
  223.  
  224.         label2 = tk.Label(self, text="Enter Amount:", font=controller.body_font)
  225.         label2.pack(side="top", fill="x",)
  226.         depositentry = tk.Entry(self,)
  227.         depositentry.pack(side="top")
  228.  
  229.         submitbutton = tk.Button(self, text="Submit Request",command=self.DepositFunc)
  230.         submitbutton.pack(side="top")
  231.  
  232.         returnbutton = tk.Button(self, text="Back",
  233.                            command=lambda: controller.show_frame("StartPage"))
  234.         returnbutton.pack(side="left")
  235.  
  236.  
  237.     def DepositFunc(self,):
  238.         global amount
  239.         global deposit
  240.         amount = float(depositentry.get())
  241.         current_customer.get_account().deposit(amount)
  242.         self.controller.show_frame("StartPage")
  243.         print_balance()
  244.  
  245.  
  246.  
  247.  
  248. class CustWithdrawMoney(tk.Frame):
  249.  
  250.     def __init__(self, parent, controller):
  251.         global withdrawentry
  252.  
  253.         tk.Frame.__init__(self, parent)
  254.         self.controller = controller
  255.  
  256.         label = tk.Label(self, text="Withdraw Money", font=controller.title_font)
  257.         label.pack(side="top", fill="x", )
  258.  
  259.         label2 = tk.Label(self, text="Enter Amount:", font=controller.body_font)
  260.         label2.pack(side="top", fill="x", )
  261.         withdrawentry = tk.Entry(self, )
  262.         withdrawentry.pack(side="top")
  263.  
  264.         withdrawbutton = tk.Button(self, text="Submit Request", command=self.WithdrawFunc)
  265.         withdrawbutton.pack(side="top")
  266.  
  267.         returnbutton = tk.Button(self, text="Back",
  268.                            command=lambda: controller.show_frame("StartPage"))
  269.         returnbutton.pack(side="left")
  270.  
  271.     def WithdrawFunc(self,):
  272.         global amount
  273.         amount = float(withdrawentry.get())
  274.         withdraw = current_customer.get_account().withdraw(amount)
  275.         self.controller.show_frame("StartPage")
  276.         print_balance()
  277.  
  278. class CustUpdateName(tk.Frame):
  279.  
  280.     def __init__(self, parent, controller):
  281.  
  282.         global NameEntry
  283.  
  284.         tk.Frame.__init__(self, parent)
  285.         self.controller = controller
  286.  
  287.         label = tk.Label(self, text="Update Name", font=controller.title_font)
  288.         label.pack(side="top", fill="x", )
  289.  
  290.         label2 = tk.Label(self, text="Enter Name:", font=controller.body_font)
  291.         label2.pack(side="top", fill="x", )
  292.  
  293.         NameEntry = tk.Entry(self, )
  294.         NameEntry.pack(side="top")
  295.  
  296.         submitbutton = tk.Button(self, text="Submit", command=self.updatename)
  297.         submitbutton.pack()
  298.  
  299.         returnbutton = tk.Button(self, text="Back",
  300.                                  command=lambda: controller.show_frame("StartPage"))
  301.         returnbutton.pack(side="left")
  302.  
  303.     def updatename(self):
  304.         global name
  305.         name = str(NameEntry.get())
  306.         updatename = current_customer.update_name(name)
  307.         tk.messagebox.showwarning(
  308.             "Success!",
  309.             "Name has been updated!")
  310.         self.controller.show_frame("AdminMenu")
  311.  
  312. class CustUpdateAdd(tk.Frame):
  313.  
  314.     def __init__(self, parent, controller):
  315.  
  316.         global AddrEntry
  317.         global AddrEntry2
  318.         global AddrEntry3
  319.         global AddrEntry4
  320.  
  321.         tk.Frame.__init__(self, parent)
  322.         self.controller = controller
  323.  
  324.         label = tk.Label(self, text="Update Address", font=controller.title_font)
  325.         label.pack()
  326.         label2 = tk.Label(self, text="Enter New Address:", font=controller.body_font)
  327.         label2.pack()
  328.  
  329.         label3 = tk.Label(self, text="Enter House Name/Number:")
  330.         label3.pack()
  331.         AddrEntry = tk.Entry(self, )
  332.         AddrEntry.pack()
  333.  
  334.  
  335.         label4 = tk.Label(self, text="Enter Street Name:")
  336.         label4.pack()
  337.         AddrEntry2 = tk.Entry(self, )
  338.         AddrEntry2.pack()
  339.  
  340.  
  341.         label5 = tk.Label(self, text="Enter City:")
  342.         label5.pack()
  343.         AddrEntry3 = tk.Entry(self, )
  344.         AddrEntry3.pack()
  345.  
  346.  
  347.         label6 = tk.Label(self, text="Enter Postcode:")
  348.         label6.pack()
  349.         AddrEntry4 = tk.Entry(self, )
  350.         AddrEntry4.pack()
  351.  
  352.  
  353.         submitbutton = tk.Button(self, text="Submit", command=self.updateaddress)
  354.         submitbutton.pack()
  355.  
  356.         returnbutton = tk.Button(self, text="Back",
  357.                                  command=lambda: controller.show_frame("StartPage"))
  358.         returnbutton.pack(side="left")
  359.  
  360.     def updateaddress(self):
  361.         line1 = str(AddrEntry.get())
  362.         line2 = str(AddrEntry2.get())
  363.         line3 = str(AddrEntry3.get())
  364.         line4 = str(AddrEntry4.get())
  365.         current_customer.update_address(line1, line2, line3, line4)
  366.         tk.messagebox.showwarning(
  367.             "Success!",
  368.             "Name has been updated!")
  369.         self.controller.show_frame("AdminMenu")
  370.  
  371.  
  372. #################################################################################################
  373. #######################     BELOW CONTAINS THE ADMIN PART OF THE GUI      #######################
  374. #################################################################################################
  375.  
  376. class AdminLogin(tk.Frame):
  377.     def __init__(self, parent, controller, ):
  378.  
  379.         bank_system.customers_list = []
  380.         bank_system.admins_list = []
  381.         bank_system.load_bank_data()
  382.  
  383.         global usernameentry
  384.         global passwordentry
  385.  
  386.         tk.Frame.__init__(self, parent, )
  387.         self.controller = controller
  388.         label = tk.Label(self, text="Customer Login", font=controller.title_font)
  389.         label.pack(side="top", fill="x", pady=10)
  390.         label2 = tk.Label(self, text="Enter Username:")
  391.         label2.pack(side="top", fill="x", pady=3)
  392.         usernameentry = tk.Entry(self, )
  393.         usernameentry.pack(side="top", expand=50)
  394.         label3 = tk.Label(self, text="Enter Password:")
  395.         label3.pack(side="top", fill="x", pady=3)
  396.         passwordentry = tk.Entry(self, show="*")
  397.         passwordentry.pack(side="top", expand=50)
  398.  
  399.         LoginButton = tk.Button(self, text="Login",
  400.                                 command=self.AdminLoginPage)
  401.         LoginButton.pack(side="top", expand=50)
  402.         button = tk.Button(self, text="Go to the start page",
  403.                            command=lambda: controller.show_frame("StartPage"), ).pack(side="right", )
  404.  
  405.     def AdminLoginPage(self):
  406.         name = str(usernameentry.get())
  407.         password = str(passwordentry.get())
  408.         found_admin = bank_system.search_admin_by_name(name)
  409.         if found_admin == None:
  410.             tk.messagebox.showwarning(
  411.                 "ERROR!",
  412.                 "Admin not found! - Please Try Again!")
  413.         else:
  414.             if (found_admin.check_password(password) == True):
  415.                 print('Admin found - Running admin menu')
  416.                 frame = self.controller.show_frame("AdminMenu")
  417.                 global current_admin
  418.                 current_admin = found_admin
  419.             else:
  420.                 tk.messagebox.showwarning(
  421.                     "ERROR!",
  422.                     "Incorrect Password - Please Try Again!")
  423.  
  424.  
  425. class AdminMenu(tk.Frame):
  426.     def __init__(self, parent, controller):
  427.         tk.Frame.__init__(self, parent)
  428.         self.controller = controller
  429.  
  430.         label = tk.Label(self, text="Admin Dashboard", font=controller.title_font)
  431.         label.pack(side="top", fill="x", pady=10)
  432.         label2 = tk.Label(self, text="Admin Options", font=controller.body_font)
  433.         label2.pack(side="top", fill="x", pady=10)
  434.  
  435.  
  436.         custaccopbutton = tk.Button(self, text= "Customer Account Options", command=lambda: controller.show_frame("CustomerAccountOptions"))
  437.         custaccopbutton.pack()
  438.  
  439.         editcustomerdetailsbutton = tk.Button(self, text= "Edit Customer Details", command=lambda: controller.show_frame("EditCustomerDetails"))
  440.         editcustomerdetailsbutton.pack()
  441.  
  442.         deletecustomer = tk.Button(self, text="Delete Customer", command=lambda: controller.show_frame("DeleteCustomer"))
  443.         deletecustomer.pack()
  444.  
  445.         printcustdetails = tk.Button(self, text="Print All Customer Details", command=self.showcustinfo)
  446.         printcustdetails.pack()
  447.     def showcustinfo(self):
  448.         PrintAllCustomers()
  449.  
  450.  
  451.  
  452.  
  453. class CustomerAccountOptions(tk.Frame):
  454.     def __init__(self, parent, controller):
  455.         global customerusernameadmin
  456.         tk.Frame.__init__(self, parent)
  457.         self.controller = controller
  458.  
  459.         label = tk.Label(self, text="Customer Account Options", font=controller.title_font)
  460.         label.pack(side="top", fill="x", pady=10)
  461.  
  462.         customerusernameadmin = tk.Entry(self,)
  463.         customerusernameadmin.pack()
  464.  
  465.  
  466.         nextbutton = tk.Button(self, text="Next", command= self.AdminCustomerLogin)
  467.         nextbutton.pack()
  468.  
  469.  
  470.     def AdminCustomerLogin(self):
  471.         name = str(customerusernameadmin.get())
  472.         found_customer = bank_system.search_customers_by_name(name)
  473.         if found_customer == None:
  474.             tk.messagebox.showwarning(
  475.                 "ERROR!",
  476.                 "Customer not found! - Please Try Again!")
  477.         else:
  478.             print('Customer found - Running customer page')
  479.             frame = self.controller.show_frame("CustomerMenu")
  480.             global current_customer
  481.             current_customer = found_customer
  482.             frame.refresh()
  483.  
  484. class EditCustomerDetails(tk.Frame):
  485.     def __init__(self, parent, controller):
  486.         global customerusernameadmin
  487.         global AdminCustomerLogin
  488.         global custnameentry
  489.         tk.Frame.__init__(self, parent)
  490.         self.controller = controller
  491.  
  492.  
  493.         label2 = tk.Label(self, text="Edit Customer details", font=controller.title_font)
  494.         label2.pack(side="top", fill="x", pady=10)
  495.  
  496.         label2 = tk.Label(self, text="Enter customers name:", font=controller.body_font)
  497.         label2.pack(side="top", fill="x", pady=10)
  498.  
  499.  
  500.         custnameentry = tk.Entry(self,)
  501.         custnameentry.pack()
  502.  
  503.         nextbutton = tk.Button(self, text="Next", command= self.AdminCustomerLogindetails)
  504.         nextbutton.pack()
  505.  
  506.     def AdminCustomerLogindetails(self):
  507.         name = str(custnameentry.get())
  508.         found_customer = bank_system.search_customers_by_name(name)
  509.         if found_customer == None:
  510.             tk.messagebox.showwarning(
  511.                 "ERROR!",
  512.                 "Customer not found! - Please Try Again!")
  513.         else:
  514.             print('Customer found - Running customer page')
  515.             frame = self.controller.show_frame("AdminCustomerInformation")
  516.             global current_customer
  517.             current_customer = found_customer
  518.             frame.refresh()
  519.  
  520.  
  521. class AdminCustomerInformation(tk.Frame):
  522.     def __init__(self, parent, controller):
  523.         tk.Frame.__init__(self, parent)
  524.         self.controller = controller
  525.         #self.draw()
  526.  
  527.     def draw(self):
  528.         controller = self.controller
  529.  
  530.         profilelabel = tk.Label(self, text="Customer information:", font=controller.title_font)
  531.         profilelabel.pack(side="top")
  532.  
  533.         labelname = tk.Label(self, text="Customer Name",font=controller.body_font)
  534.         labelname.pack(side="top", )
  535.  
  536.         labelnameprint = tk.Label(self, text="%s" % current_customer.name,)
  537.         labelnameprint.pack(side="top", )
  538.  
  539.         self.nameprint = tk.StringVar()
  540.         namelabel = tk.Label(self, textvariable=self.nameprint, )
  541.         namelabel.pack(side="top", )
  542.  
  543.         address = ""
  544.         for a in current_customer.get_address():
  545.             address += "%s\n" % a
  546.  
  547.         labeladdr = tk.Label(self, text="Customer Address:", font=controller.body_font)
  548.         labeladdr.pack(side="top", )
  549.  
  550.         labeladdressprint = tk.Label(self, text="%s" % address, )
  551.         labeladdressprint.pack(side="top", )
  552.  
  553.         self.addressprint = tk.StringVar()
  554.         addresslabel = tk.Label(self, textvariable=self.addressprint, )
  555.         addresslabel.pack(side="top", )
  556.  
  557.         updatename = tk.Button(self, text="Update name", command=lambda: controller.show_frame("CustUpdateName"))
  558.         updatename.pack()
  559.  
  560.         updateaddress = tk.Button(self, text="Update address", command=lambda: controller.show_frame("CustUpdateAdd"))
  561.         updateaddress.pack()
  562.     def refresh(self):
  563.         for child in self.winfo_children():
  564.             child.destroy()
  565.  
  566.         self.draw()
  567.  
  568.  
  569. class DeleteCustomer(tk.Frame):
  570.     def __init__(self, parent, controller):
  571.         global deleteent
  572.         tk.Frame.__init__(self, parent)
  573.         self.controller = controller
  574.  
  575.         profilelabel = tk.Label(self, text="Delete Customer:", font=controller.title_font)
  576.         profilelabel.pack(side="top")
  577.  
  578.         labelnameprint = tk.Label(self, text="Customer Name:", font=controller.body_font)
  579.         labelnameprint.pack(side="top", )
  580.  
  581.         deleteent = tk.Entry(self,)
  582.         deleteent.pack()
  583.  
  584.         deletebutt = tk.Button(self, text="Update name", command=self.AdminDeleteCustomer)
  585.         deletebutt.pack()
  586.  
  587.     def AdminDeleteCustomer(self):
  588.         global deleteent
  589.         customer_name = str(deleteent.get())
  590.         customer_account = bank_system.search_customers_by_name(customer_name)
  591.         if customer_account != None:
  592.             bank_system.customers_list.remove(customer_account)
  593.        # else:
  594.             #print("\nOnly administrators with full admin rights can remove a customer from the bank system!\n")
  595.  
  596.         tk.messagebox.showwarning(
  597.             "Success!",
  598.             "User has been deleted.")
  599.         self.controller.show_frame("AdminMenu ")
  600.  
  601. class PrintAllCustomers():
  602.     def __init__(self):
  603.         self.parent = tk.Toplevel()
  604.  
  605.         info = self.printcustfunc()
  606.         text = tk.Label(self.parent,text=info)
  607.         text.pack()
  608.  
  609.  
  610.     def printcustfunc(self):
  611.         output = ""
  612.         for c in bank_system.customers_list:
  613.             output += "%s" % c.print_details()
  614.             output += "------------------------\n"
  615.         return output
  616.  
  617. if __name__ == "__main__":
  618.     app = SampleApp()
  619.     app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement