Advertisement
Stillkill

Final Project

May 22nd, 2018
432
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.55 KB | None | 0 0
  1. #manual imports
  2. from tkinter import *
  3. from tkinter import ttk
  4. import tkinter as tk
  5. import math as math
  6.  
  7. FONT = ("Verdana", 14)
  8.  
  9. class App(tk.Tk):
  10.     def __init__(self, *args, **kwargs): # __init__
  11.         tk.Tk.__init__(self, *args, **kwargs) # initialize tk object
  12.         tk.Tk.wm_title(self, "Object GUI's") # Set Title
  13.         self.geometry("600x200") # Set screen size for now
  14.         self.container = tk.Frame(self) # creating a main frame (not a mainframe)
  15.         self.container.grid(row=0, column=0) # making the main fram visible
  16.         self.frames = {}  #making an empty dict to store all of our pages
  17.         # TODO: make a dictionary to store two StringVar()'s, call it data
  18.         self.data = {"digit":StringVar(), "multiple":StringVar()}
  19.         self.d = {}
  20.         # A loop to make an object for each page
  21.         for F in (StartPage, PageOne, PageTwo, PageThree, PageFour): # This tupple contains all the pages
  22.             frame = F(self.container, self)
  23.             self.frames[F] = frame
  24.             frame.grid(row=0, column=0, sticky="nsew")
  25.         self.showFrame(StartPage, self.d) # Call the starting page
  26.         self.PageOneResult = StringVar()
  27.         #Two page two results to store both quadratic roots, cuz math yo
  28.         self.PageTwoResult1 = StringVar()
  29.         self.PageTwoResult2 = StringVar()
  30.         self.PageThreeResult = StringVar()
  31.         self.PageOneResult.set('No Input')
  32.         self.PageTwoResult1.set("No Input")
  33.         self.PageTwoResult2.set("No Input")
  34.         self.PageThreeResult.set("No Input")
  35.  
  36.     def showFrame(self, cont, d):
  37.         self.frame = self.frames[cont]
  38.         self.update(d)
  39.         self.frame.tkraise()
  40.  
  41.     def update(self, d):
  42.         for k, v in d.items():
  43.             if isinstance(v, str):
  44.                 self.data[k].set(v)
  45.             else:
  46.                 self.data[k] = v
  47.     def get_data(self):
  48.         return self.data
  49.  
  50. class StartPage(tk.Frame):
  51.     def __init__(self, parent, controller):
  52.         tk.Frame.__init__(self, parent)
  53.         self.controller = controller
  54.         self.info = StringVar()
  55.         self.d = {}
  56.         self.controller.data['digit'].get()
  57.         self.label1 = Label(self, text="Page 1: Factorals", font=FONT)
  58.         self.label1.grid(row=0, column=1,sticky=NW)
  59.         self.label2 = Label(self, text="Page 2: Quadratic Formula", font=FONT)
  60.         self.label2.grid(row=1, column=1,sticky=NW)
  61.         self.label1 = Label(self, text="Page 3: Pythagrean Theorem", font=FONT)
  62.         self.label1.grid(row=2, column=1,sticky=NW)
  63.         self.label1 = Label(self, text="Page 4: Results", font=FONT)
  64.         self.label1.grid(row=3, column=1,sticky=NW)
  65.         self.button1 = Button(self, text="Next Page",command=self.NextPage)
  66.         self.button1.grid(row=4, column=0)
  67.         self.button2 = Button(self, text="Quit", command = self.quit)
  68.         self.button2.grid(row=4, column=2, sticky=NW)
  69.  
  70.     def exit(self):
  71.         #Issue: Execution causes process crash : prior = LOW
  72.         exit("Process terminated by user")
  73.        
  74.  
  75.     def NextPage(self):
  76.         self.d['digit']=self.info.get()
  77.         self.controller.showFrame(PageOne, self.d)
  78.  
  79. class PageOne(tk.Frame):
  80.     def __init__(self, parent, controller):
  81.         tk.Frame.__init__(self, parent)
  82.         self.controller = controller
  83.         self.info = StringVar()
  84.         self.d = {}
  85.         self.errorMessage = StringVar()
  86.         self.errorMessage.set(" ")
  87.         self.userNumString = StringVar()
  88.         self.TitleLabel = Label(self, text="Page 1: Factorals")
  89.         self.TitleLabel.grid(row=0, column=1)
  90.         self.userEntry = Entry(self, textvariable=self.userNumString)
  91.         self.userEntry.grid(row=1, column=1)
  92.         self.calcButton = Button(self, text="Calculate", command=self.CalcFactoral)
  93.         self.calcButton.grid(row=1, column=2)
  94.         self.errorMess = Label(self, textvariable=self.errorMessage)
  95.         self.errorMess.grid(row=2, column=1)
  96.         self.buttonNextPage = Button(self, text="Next Page", command=self.PageSwitchTwo)
  97.         self.buttonNextPage.grid(row=3, column=3)
  98.         self.buttonQuit = Button(self, text="Quit", command=exit)
  99.         self.buttonQuit.grid(row=3, column=1)
  100.         self.PrevButton = Button(self, text="Previous Page", command=self.PageSwitchStart)
  101.         self.PrevButton.grid(row=3, column=2)
  102.  
  103.     def CalcFactoral(self):
  104.         try:
  105.             userNum = int(self.userNumString.get())
  106.             self.result = math.factorial(userNum)
  107.             self.resultString = str(self.result)
  108.             app.PageOneResult.set(self.resultString)
  109.  
  110.             self.errorMessage.set("Calculation completed succesfully!")
  111.            
  112.         except ValueError:
  113.             self.errorMessage.set("Value Error: Given value must be an integer greater than 1")
  114.     def PageSwitchStart(self):
  115.         self.d['multiple']=self.info.get()
  116.         self.controller.showFrame(StartPage, self.d)
  117.  
  118.     def PageSwitchTwo(self):
  119.         self.d['multiple']=self.info.get()
  120.         self.controller.showFrame(PageTwo, self.d)
  121.  
  122.     def exit(self):
  123.         exit("Process terminated by user")
  124.  
  125. class PageTwo(tk.Frame):
  126.     def __init__(self, parent, controller):
  127.         tk.Frame.__init__(self, parent)
  128.         self.controller = controller
  129.         self.errorMessage = StringVar()
  130.         self.errorMessage.set(" ")
  131.         self.info = StringVar()
  132.         self.d = {}
  133.         self.num1 = StringVar()
  134.         self.num1.set("Enter Variable A")
  135.         self.num2 = StringVar()
  136.         self.num2.set("Enter Variable B")
  137.         self.num3 = StringVar()
  138.         self.num3.set("Enter Variable C")
  139.         self.num1Entry = Entry(self, textvariable=self.num1)
  140.         self.num1Entry.grid(row=1, column=1)
  141.         self.num2Entry = Entry(self, textvariable=self.num2)
  142.         self.num2Entry.grid(row=2, column=1)
  143.         self.num3Entry = Entry(self, textvariable=self.num3)
  144.         self.num3Entry.grid(row=3, column=1)
  145.         self.calcButton = Button(self, text="Calculate", command=self.calc)
  146.         self.calcButton.grid(row=4, column=2)
  147.         self.PrevButton = Button(self, text="Previous Page", command=self.one)
  148.         self.PrevButton.grid(row=4, column=3)
  149.         self.buttonQuit = Button(self, text="Quit", command=exit)
  150.         self.buttonQuit.grid(row=4, column=1)
  151.         self.buttonNext = Button(self, text="Next Page", command=self.PageSwitchThree)
  152.         self.buttonNext.grid(row=4, column=4)
  153.         self.errorMess = Label(self, textvariable=self.errorMessage)
  154.         self.errorMess.grid(row=5, column=1)
  155.  
  156.     def start(self):
  157.         self.controller.showFrame(StartPage, self.d)
  158.  
  159.     def one(self):
  160.         self.controller.showFrame(PageOne, self.d)
  161.  
  162.     def PageSwitchThree(self):
  163.         self.d['multiple']=self.info.get()
  164.         self.controller.showFrame(PageThree, self.d)
  165.  
  166.     def calc(self):
  167.         try:
  168.             a = int(self.num1.get())
  169.             b = int(self.num2.get())
  170.             c = int(self.num3.get())
  171.             d = (b ** 2) - (4 * a * c)
  172.             #Two page two results to store both quadratic roots, cuz math yo
  173.             app.PageTwoResult1.set((-b - math.sqrt(d) / 2 * a))
  174.             app.PageTwoResult2.set((-b + math.sqrt(d) / 2 * a))
  175.             self.errorMessage.set("Calculation completed succesfully!")
  176.  
  177.         except ValueError:
  178.             #Git Issue: Must find way to account for imaginary solutions : prior = HIGH
  179.             self.errorMessage.set("ValueError: math domain error")
  180.  
  181.  
  182. class PageThree(tk.Frame):
  183.     def __init__(self, parent, controller):
  184.         tk.Frame.__init__(self, parent)
  185.         self.controller = controller
  186.         self.errorMessage = StringVar()
  187.         self.errorMessage.set(" ")
  188.         self.info = StringVar()
  189.         self.d = {}
  190.         self.v = 3
  191.         self.VarA = StringVar()
  192.         self.VarA.set("Enter variable A")
  193.         self.Var2 = StringVar()
  194.         self.Var2.set("Enter variable B or C")
  195.         self.radButtonForB = Radiobutton(self, text="Solve for B", variable=self.v, value=1)
  196.         self.radButtonForB.grid(row=1, column=2)
  197.         self.radButtonForC = Radiobutton(self, text="Solve for C", variable=self.v, value=2)
  198.         self.radButtonForC.grid(row=2, column=2)
  199.         self.varAEntry = Entry(self, textvariable=self.VarA)
  200.         self.varAEntry.grid(row=1, column=1)
  201.         self.var2Entry = Entry(self, textvariable=self.Var2)
  202.         self.var2Entry.grid(row=2, column=1)
  203.         self.calcButton = Button(self, text="Calculate", command=self.Calc)
  204.         self.BackPageButton = Button(self, text="Previous page", command=self.PageSwitchTwo)
  205.         self.BackPageButton.grid(row=4, column=1)
  206.         self.NextPageButton = Button(self, text="Next page", command=self.PageSwitchFour)
  207.         self.NextPageButton.grid(row=4, column=2)
  208.  
  209.     def Calc(self):
  210.         check = int(self.v.get())
  211.         A = int(self.VarA.get())
  212.         Var2 = int(self.Var2.get())
  213.  
  214.         if check == 1:
  215.             #a²+b²=c²
  216.             #solve for variable B
  217.             ans = sqrt((Var2 ** 2) - (A ** 2))
  218.             app.PageThreeResult.set(str(ans))
  219.         elif check == 2:
  220.             #solve for variable C
  221.             ans = sqrt((A ** 2) - (Var2 ** 2))
  222.             app.PageThreeResult.set(str(ans))
  223.         else:
  224.             #throw error, decider var out of range
  225.             self.errorMessage.set("Error: self.v was out of range")
  226.             pass
  227.  
  228.     def PageSwitchTwo(self):
  229.         self.d['multiple']=self.info.get()
  230.         self.controller.showFrame(PageTwo, self.d)
  231.  
  232.     def PageSwitchFour(self):
  233.         self.d['multiple']=self.info.get()
  234.         self.controller.showFrame(PageFour, self.d)
  235.  
  236.     def exit(self):
  237.             exit("Process terminated by user")
  238.  
  239. class PageFour(tk.Frame):
  240.     def __init__(self, parent, controller):
  241.         tk.Frame.__init__(self, parent)
  242.         self.controller = controller
  243.         self.errorMessage = StringVar()
  244.         self.errorMessage.set(" ")
  245.         self.info = StringVar()
  246.         self.d = {}
  247.         self.titleLable = Label(self, text="Results Page", font=FONT)
  248.         self.titleLable.grid(row=1, column=1)
  249.         self.Page1Lable = Label(self, text="Page One Results: ")
  250.         self.Page1Lable.grid(row=2, column=1)
  251.         self.Page1ResLable = Label(self, textvariable=app.PageOneResult)
  252.         self.Page1ResLable.grid(row=2, column=2)
  253.         self.Page2lable = Lable(self, text="Page Two Results: ")
  254.         self.Page2Lable.grid(row=3, column=1)
  255.         self.page2Res1lable = Label(self, textvariable=app.PageTwoResult1)
  256.         self.page2Res1lable.grid(row=3, column=2)
  257.         self.page2Res2lable = Label(self, textvariable=app.PageTwoResult2)
  258.         self.page2Res2lable.grid(row=3, column=3)
  259.         self.page3lable = Label(self, text="Page Three Results")
  260.         self.page3lable.grid(row=4, column=1)
  261.         self.page3Reslable = Label(self, textvariable=app.PageThreeResult)
  262.         self.page3Reslable.grid(row=4, column=2)
  263.  
  264.  
  265. app = App()
  266. app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement