Advertisement
Stillkill

RPS

May 27th, 2018
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.04 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(),"pageOne":StringVar(), "pageTwo1":StringVar(), "pageTwo2":StringVar(), "pageThree":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.          
  27.     def showFrame(self, cont, d):
  28.         self.frame = self.frames[cont]
  29.         self.update(d)
  30.         self.frame.tkraise()
  31.  
  32.     def update(self, d):
  33.         for k, v in d.items():
  34.             if isinstance(v, str):
  35.                 self.data[k].set(v)
  36.             else:
  37.                 self.data[k] = v
  38.  
  39.     def GetVars():
  40.         return(self.PageOneResult, self.PageTwoResult1, self.PageTwoResult2, self.PageThreeResult)
  41.  
  42.     def get_data(self):
  43.         return self.data
  44.  
  45. class StartPage(tk.Frame):
  46.     def __init__(self, parent, controller):
  47.         tk.Frame.__init__(self, parent)
  48.         self.controller = controller
  49.         self.info = StringVar()
  50.         self.d = {}
  51.         self.controller.data['digit'].get()
  52.         self.label1 = Label(self, text="Page 1: Factorals", font=FONT)
  53.         self.label1.grid(row=0, column=1,sticky=NW)
  54.         self.label2 = Label(self, text="Page 2: Quadratic Formula", font=FONT)
  55.         self.label2.grid(row=1, column=1,sticky=NW)
  56.         self.label1 = Label(self, text="Page 3: Pythagrean Theorem", font=FONT)
  57.         self.label1.grid(row=2, column=1,sticky=NW)
  58.         self.label1 = Label(self, text="Page 4: Results", font=FONT)
  59.         self.label1.grid(row=3, column=1,sticky=NW)
  60.         self.button1 = Button(self, text="Next Page",command=self.NextPage)
  61.         self.button1.grid(row=4, column=0)
  62.         self.button2 = Button(self, text="Quit", command = self.quit)
  63.         self.button2.grid(row=4, column=2, sticky=NW)
  64.  
  65.     def exit(self):
  66.         #Issue: Execution causes process crash : prior = LOW
  67.         exit("Process terminated by user")
  68.        
  69.  
  70.     def NextPage(self):
  71.         self.d['digit']=self.info.get()
  72.         self.controller.showFrame(PageOne, self.d)
  73.  
  74. class PageOne(tk.Frame):
  75.     def __init__(self, parent, controller):
  76.         tk.Frame.__init__(self, parent)
  77.         self.controller = controller
  78.         self.info = StringVar()
  79.         self.d = {}
  80.         self.errorMessage = StringVar()
  81.         self.errorMessage.set(" ")
  82.         self.userNumString = StringVar()
  83.         self.TitleLabel = Label(self, text="Page 1: Factorals")
  84.         self.TitleLabel.grid(row=0, column=1)
  85.         self.userEntry = Entry(self, textvariable=self.userNumString)
  86.         self.userEntry.grid(row=1, column=1)
  87.         self.calcButton = Button(self, text="Calculate", command=self.CalcFactoral)
  88.         self.calcButton.grid(row=1, column=2)
  89.         self.errorMess = Label(self, textvariable=self.errorMessage)
  90.         self.errorMess.grid(row=2, column=1)
  91.         self.buttonNextPage = Button(self, text="Next Page", command=self.PageSwitchTwo)
  92.         self.buttonNextPage.grid(row=3, column=3)
  93.         self.buttonQuit = Button(self, text="Quit", command=exit)
  94.         self.buttonQuit.grid(row=3, column=1)
  95.         self.PrevButton = Button(self, text="Previous Page", command=self.PageSwitchStart)
  96.         self.PrevButton.grid(row=3, column=2)
  97.  
  98.     def CalcFactoral(self):
  99.         try:
  100.             userNum = int(self.userNumString.get())
  101.             self.result = math.factorial(userNum)
  102.             self.resultString = str(self.result)
  103.             self.d['pageOne']=self.resultString
  104.             self.errorMessage.set("Calculation completed succesfully!")
  105.         except OverflowError:
  106.             self.errorMessage.set('OverflowError: Argument should not exceed 2147483647')
  107.             pass
  108.  
  109.         except ValueError:
  110.             self.errorMessage.set("Value Error: Given value must be an integer greater than 1")
  111.             pass
  112.  
  113.     def PageSwitchStart(self):
  114.         self.controller.showFrame(StartPage, self.d)
  115.  
  116.     def PageSwitchTwo(self):
  117.        
  118.         self.controller.showFrame(PageTwo, self.d)
  119.  
  120.     def exit(self):
  121.         exit("Process terminated by user")
  122.  
  123. class PageTwo(tk.Frame):
  124.     def __init__(self, parent, controller):
  125.         tk.Frame.__init__(self, parent)
  126.         self.controller = controller
  127.         self.errorMessage = StringVar()
  128.         self.errorMessage.set(" ")
  129.         self.Result1 = StringVar()
  130.         self.Result2 = StringVar()
  131.         self.d = {}
  132.         self.num1 = StringVar()
  133.         self.num1.set("Enter Variable A")
  134.         self.num2 = StringVar()
  135.         self.num2.set("Enter Variable B")
  136.         self.num3 = StringVar()
  137.         self.num3.set("Enter Variable C")
  138.         self.num1Entry = Entry(self, textvariable=self.num1)
  139.         self.num1Entry.grid(row=1, column=1)
  140.         self.num2Entry = Entry(self, textvariable=self.num2)
  141.         self.num2Entry.grid(row=2, column=1)
  142.         self.num3Entry = Entry(self, textvariable=self.num3)
  143.         self.num3Entry.grid(row=3, column=1)
  144.         self.calcButton = Button(self, text="Calculate", command=self.calc)
  145.         self.calcButton.grid(row=4, column=2)
  146.         self.PrevButton = Button(self, text="Previous Page", command=self.one)
  147.         self.PrevButton.grid(row=4, column=3)
  148.         self.buttonQuit = Button(self, text="Quit", command=exit)
  149.         self.buttonQuit.grid(row=4, column=1)
  150.         self.buttonNext = Button(self, text="Next Page", command=self.PageSwitchThree)
  151.         self.buttonNext.grid(row=4, column=4)
  152.         self.errorMess = Label(self, textvariable=self.errorMessage)
  153.         self.errorMess.grid(row=5, column=1)
  154.  
  155.     def start(self):
  156.         self.controller.showFrame(StartPage, self.d)
  157.  
  158.     def one(self):
  159.         self.controller.showFrame(PageOne, self.d)
  160.  
  161.     def PageSwitchThree(self):
  162.         self.controller.showFrame(PageThree, self.d)
  163.  
  164.     def calc(self):
  165.         try:
  166.             a = int(self.num1.get())
  167.             b = int(self.num2.get())
  168.             c = int(self.num3.get())
  169.             d = (b ** 2) - (4 * a * c)
  170.             #Two page two results to store both quadratic roots, cuz math yo
  171.             self.Result1.set((-b - math.sqrt(d) / 2 * a))
  172.             self.Result2.set((-b + math.sqrt(d) / 2 * a))
  173.             self.d['pageTwo1']=self.Result1.get()
  174.             self.d['pageTwo2']=self.Result2.get()
  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 = IntVar()
  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.errorMessageLab = Label(self, textvariable=self.errorMessage)
  196.         self.errorMessageLab.grid(row=5, column=1)
  197.         self.radButtonForB = Radiobutton(self, text="Solve for B", variable=self.v, value=1)
  198.         self.radButtonForB.grid(row=1, column=2)
  199.         self.radButtonForC = Radiobutton(self, text="Solve for C", variable=self.v, value=2)
  200.         self.radButtonForC.grid(row=2, column=2)
  201.         self.varAEntry = Entry(self, textvariable=self.VarA)
  202.         self.varAEntry.grid(row=1, column=1)
  203.         self.var2Entry = Entry(self, textvariable=self.Var2)
  204.         self.var2Entry.grid(row=2, column=1)
  205.         self.calcButton = Button(self, text="Calculate", command=self.Calc)
  206.         self.calcButton.grid(row=4, column=3)
  207.         self.BackPageButton = Button(self, text="Previous page", command=self.PageSwitchTwo)
  208.         self.BackPageButton.grid(row=4, column=1)
  209.         self.NextPageButton = Button(self, text="Next page", command=self.PageSwitchFour)
  210.         self.NextPageButton.grid(row=4, column=2)
  211.        
  212.  
  213.     def Calc(self):
  214.         check = self.v.get()
  215. #       self.A = self.VarA
  216. #       self.A = int(A)
  217. #       Var2 = self.Var2.get()
  218. #       Var2 = int(Var2)
  219.         if check == 1:
  220.             #solve for B
  221.             try:
  222.                 ans = math.sqrt(int(self.Var2.get()) * int(self.Var2.get()) - int(self.VarA.get()) * int(self.VarA.get()))
  223.                 self.d['pageThree']=str(ans)
  224.             except ValueError:
  225.                 self.errorMessage.set("ValueError: Numeral Set given results in an imaginary number")
  226.             self.errorMessage.set("Calculation completed for B successfully")
  227.         elif check == 2:
  228.             #solve for C
  229.             ans = math.sqrt(int(self.VarA.get()) * int(self.VarA.get()) + int(self.Var2.get()) * int(self.Var2.get()))
  230.             self.d['pageThree']=str(ans)
  231.             self.errorMessage.set("Calculation For C completed successfully")
  232.         else:
  233.             self.errorMessage.set('Try agian bud')
  234.  
  235.     def PageSwitchTwo(self):
  236.         self.controller.showFrame(PageTwo, self.d)
  237.  
  238.     def PageSwitchFour(self):
  239.         self.controller.showFrame(PageFour, self.d)
  240.  
  241.     def exit(self):
  242.             exit("Process terminated by user")
  243.  
  244. class PageFour(tk.Frame):
  245.     def __init__(self, parent, controller):
  246.         tk.Frame.__init__(self, parent)
  247.         self.controller = controller
  248.         self.errorMessage = StringVar()
  249.         self.errorMessage.set(" ")
  250.         self.d = {}
  251.         self.PageOneResult = StringVar()
  252.         self.PageOneResult.set('<No Data>')
  253.         #Two page two results to store both quadratic roots, cuz math yo
  254.         self.PageTwoResult1 = StringVar()
  255.         self.PageTwoResult1.set('<No Data>')
  256.         self.PageTwoResult2 = StringVar()
  257.         self.PageTwoResult2.set('<No Data>')
  258.         self.PageThreeResult = StringVar()
  259.         self.PageThreeResult.set('<No Data>')
  260.         self.titleLable = Label(self, text="Results Page", font=FONT)
  261.         self.titleLable.grid(row=1, column=1)
  262.         self.getResButton = Button(self, text="Get Results", command=self.Get_Results)
  263.         self.getResButton.grid(row=1, column=2)
  264.         self.resLableP1 = Label(self, textvariable=self.PageOneResult)
  265.         self.resLableP1.grid(row=2, column=0)
  266.         self.resLableP2_1 = Label(self, textvariable=self.PageTwoResult1)
  267.         self.resLableP2_1.grid(row=3, column=0)
  268.         self.resLableP2_2 = Label(self, textvariable=self.PageTwoResult2)
  269.         self.resLableP2_2.grid(row=4, column=0)
  270.         self.resLableP3 = Label(self, textvariable=self.PageThreeResult)
  271.         self.resLableP3.grid(row=5, column=0)
  272.         self.errorMessageLab = Label(self, textvariable=self.errorMessage)
  273.         self.errorMessageLab.grid(row=6, column=1)
  274.         #fancy labels
  275.         self.p1Lab = Label(self, text="<-- Page 1")
  276.         self.p1Lab.grid(row=2, column=1)
  277.         self.p2Lab = Label(self, text="<-- Page 2")
  278.         self.p2Lab.grid(row=3, column=1)
  279.         self.p2_2Lab = Label(self, text="<-- Page 2 (some functions will have 2 results, others may not)")
  280.         self.p2_2Lab.grid(row=4, column=1)
  281.         self.p3Lab = Label(self, text="<-- Page 3")
  282.         self.p3Lab.grid(row=5, column=1)
  283.  
  284.     def Get_Results(self):
  285.         try:
  286.             self.controller = self.controller.get_data()
  287.             self.PageOneResult.set(self.controller['pageOne'].get())
  288.             self.PageTwoResult1.set(self.controller['pageTwo1'].get())
  289.             self.PageTwoResult2.set(self.controller['pageTwo2'].get())
  290.             self.PageThreeResult.set(self.controller['pageThree'].get())
  291.  
  292.         except AttributeError:
  293.             self.errorMessage.set("AttributeError: 'dict' object has no attribute 'get_data' (don't click the button twice)")
  294.  
  295.  
  296.  
  297. app = App()
  298. app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement