Advertisement
Stillkill

Final Project v2

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