Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #manual imports
- from tkinter import *
- from tkinter import ttk
- import tkinter as tk
- import math as math
- FONT = ("Verdana", 14)
- class App(tk.Tk):
- def __init__(self, *args, **kwargs): # __init__
- tk.Tk.__init__(self, *args, **kwargs) # initialize tk object
- tk.Tk.wm_title(self, "Object GUI's") # Set Title
- self.geometry("600x200") # Set screen size for now
- self.container = tk.Frame(self) # creating a main frame (not a mainframe)
- self.container.grid(row=0, column=0) # making the main fram visible
- self.frames = {} #making an empty dict to store all of our pages
- # TODO: make a dictionary to store two StringVar()'s, call it data
- self.data = {"digit":StringVar(),"pageOne":StringVar(), "pageTwo1":StringVar(), "pageTwo2":StringVar(), "pageThree":StringVar(), "multiple":StringVar()}
- self.d = {}
- # A loop to make an object for each page
- for F in (StartPage, PageOne, PageTwo, PageThree, PageFour): # This tupple contains all the pages
- frame = F(self.container, self)
- self.frames[F] = frame
- frame.grid(row=0, column=0, sticky="nsew")
- self.showFrame(StartPage, self.d) # Call the starting page
- def showFrame(self, cont, d):
- self.frame = self.frames[cont]
- self.update(d)
- self.frame.tkraise()
- def update(self, d):
- for k, v in d.items():
- if isinstance(v, str):
- self.data[k].set(v)
- else:
- self.data[k] = v
- def GetVars():
- return(self.PageOneResult, self.PageTwoResult1, self.PageTwoResult2, self.PageThreeResult)
- def get_data(self):
- return self.data
- class StartPage(tk.Frame):
- def __init__(self, parent, controller):
- tk.Frame.__init__(self, parent)
- self.controller = controller
- self.info = StringVar()
- self.d = {}
- self.controller.data['digit'].get()
- self.label1 = Label(self, text="Page 1: Factorals", font=FONT)
- self.label1.grid(row=0, column=1,sticky=NW)
- self.label2 = Label(self, text="Page 2: Quadratic Formula", font=FONT)
- self.label2.grid(row=1, column=1,sticky=NW)
- self.label1 = Label(self, text="Page 3: Pythagrean Theorem", font=FONT)
- self.label1.grid(row=2, column=1,sticky=NW)
- self.label1 = Label(self, text="Page 4: Results", font=FONT)
- self.label1.grid(row=3, column=1,sticky=NW)
- self.button1 = Button(self, text="Next Page",command=self.NextPage)
- self.button1.grid(row=4, column=0)
- self.button2 = Button(self, text="Quit", command = self.quit)
- self.button2.grid(row=4, column=2, sticky=NW)
- def exit(self):
- #Issue: Execution causes process crash : prior = LOW
- exit("Process terminated by user")
- def NextPage(self):
- self.d['digit']=self.info.get()
- self.controller.showFrame(PageOne, self.d)
- class PageOne(tk.Frame):
- def __init__(self, parent, controller):
- tk.Frame.__init__(self, parent)
- self.controller = controller
- self.info = StringVar()
- self.d = {}
- self.errorMessage = StringVar()
- self.errorMessage.set(" ")
- self.userNumString = StringVar()
- self.TitleLabel = Label(self, text="Page 1: Factorals")
- self.TitleLabel.grid(row=0, column=1)
- self.userEntry = Entry(self, textvariable=self.userNumString)
- self.userEntry.grid(row=1, column=1)
- self.calcButton = Button(self, text="Calculate", command=self.CalcFactoral)
- self.calcButton.grid(row=1, column=2)
- self.errorMess = Label(self, textvariable=self.errorMessage)
- self.errorMess.grid(row=2, column=1)
- self.buttonNextPage = Button(self, text="Next Page", command=self.PageSwitchTwo)
- self.buttonNextPage.grid(row=3, column=3)
- self.buttonQuit = Button(self, text="Quit", command=exit)
- self.buttonQuit.grid(row=3, column=1)
- self.PrevButton = Button(self, text="Previous Page", command=self.PageSwitchStart)
- self.PrevButton.grid(row=3, column=2)
- def CalcFactoral(self):
- try:
- userNum = int(self.userNumString.get())
- self.result = math.factorial(userNum)
- self.resultString = str(self.result)
- self.d['pageOne']=self.resultString
- self.errorMessage.set("Calculation completed succesfully!")
- except OverflowError:
- self.errorMessage.set('OverflowError: Argument should not exceed 2147483647')
- pass
- except ValueError:
- self.errorMessage.set("Value Error: Given value must be an integer greater than 1")
- pass
- def PageSwitchStart(self):
- self.controller.showFrame(StartPage, self.d)
- def PageSwitchTwo(self):
- self.controller.showFrame(PageTwo, self.d)
- def exit(self):
- exit("Process terminated by user")
- class PageTwo(tk.Frame):
- def __init__(self, parent, controller):
- tk.Frame.__init__(self, parent)
- self.controller = controller
- self.errorMessage = StringVar()
- self.errorMessage.set(" ")
- self.Result1 = StringVar()
- self.Result2 = StringVar()
- self.d = {}
- self.num1 = StringVar()
- self.num1.set("Enter Variable A")
- self.num2 = StringVar()
- self.num2.set("Enter Variable B")
- self.num3 = StringVar()
- self.num3.set("Enter Variable C")
- self.num1Entry = Entry(self, textvariable=self.num1)
- self.num1Entry.grid(row=1, column=1)
- self.num2Entry = Entry(self, textvariable=self.num2)
- self.num2Entry.grid(row=2, column=1)
- self.num3Entry = Entry(self, textvariable=self.num3)
- self.num3Entry.grid(row=3, column=1)
- self.calcButton = Button(self, text="Calculate", command=self.calc)
- self.calcButton.grid(row=4, column=2)
- self.PrevButton = Button(self, text="Previous Page", command=self.one)
- self.PrevButton.grid(row=4, column=3)
- self.buttonQuit = Button(self, text="Quit", command=exit)
- self.buttonQuit.grid(row=4, column=1)
- self.buttonNext = Button(self, text="Next Page", command=self.PageSwitchThree)
- self.buttonNext.grid(row=4, column=4)
- self.errorMess = Label(self, textvariable=self.errorMessage)
- self.errorMess.grid(row=5, column=1)
- def start(self):
- self.controller.showFrame(StartPage, self.d)
- def one(self):
- self.controller.showFrame(PageOne, self.d)
- def PageSwitchThree(self):
- self.controller.showFrame(PageThree, self.d)
- def calc(self):
- try:
- a = int(self.num1.get())
- b = int(self.num2.get())
- c = int(self.num3.get())
- d = (b ** 2) - (4 * a * c)
- #Two page two results to store both quadratic roots, cuz math yo
- self.Result1.set((-b - math.sqrt(d) / 2 * a))
- self.Result2.set((-b + math.sqrt(d) / 2 * a))
- self.d['pageTwo1']=self.Result1.get()
- self.d['pageTwo2']=self.Result2.get()
- self.errorMessage.set("Calculation completed succesfully!")
- except ValueError:
- #Git Issue: Must find way to account for imaginary solutions : prior = HIGH
- self.errorMessage.set("ValueError: math domain error")
- class PageThree(tk.Frame):
- def __init__(self, parent, controller):
- tk.Frame.__init__(self, parent)
- self.controller = controller
- self.errorMessage = StringVar()
- self.errorMessage.set(" ")
- self.info = StringVar()
- self.d = {}
- self.v = IntVar()
- self.VarA = StringVar()
- self.VarA.set("Enter variable A")
- self.Var2 = StringVar()
- self.Var2.set("Enter variable B or C")
- self.errorMessageLab = Label(self, textvariable=self.errorMessage)
- self.errorMessageLab.grid(row=5, column=1)
- self.radButtonForB = Radiobutton(self, text="Solve for B", variable=self.v, value=1)
- self.radButtonForB.grid(row=1, column=2)
- self.radButtonForC = Radiobutton(self, text="Solve for C", variable=self.v, value=2)
- self.radButtonForC.grid(row=2, column=2)
- self.varAEntry = Entry(self, textvariable=self.VarA)
- self.varAEntry.grid(row=1, column=1)
- self.var2Entry = Entry(self, textvariable=self.Var2)
- self.var2Entry.grid(row=2, column=1)
- self.calcButton = Button(self, text="Calculate", command=self.Calc)
- self.calcButton.grid(row=4, column=3)
- self.BackPageButton = Button(self, text="Previous page", command=self.PageSwitchTwo)
- self.BackPageButton.grid(row=4, column=1)
- self.NextPageButton = Button(self, text="Next page", command=self.PageSwitchFour)
- self.NextPageButton.grid(row=4, column=2)
- def Calc(self):
- check = self.v.get()
- # self.A = self.VarA
- # self.A = int(A)
- # Var2 = self.Var2.get()
- # Var2 = int(Var2)
- if check == 1:
- #solve for B
- try:
- ans = math.sqrt(int(self.Var2.get()) * int(self.Var2.get()) - int(self.VarA.get()) * int(self.VarA.get()))
- self.d['pageThree']=str(ans)
- except ValueError:
- self.errorMessage.set("ValueError: Numeral Set given results in an imaginary number")
- self.errorMessage.set("Calculation completed for B successfully")
- elif check == 2:
- #solve for C
- ans = math.sqrt(int(self.VarA.get()) * int(self.VarA.get()) + int(self.Var2.get()) * int(self.Var2.get()))
- self.d['pageThree']=str(ans)
- self.errorMessage.set("Calculation For C completed successfully")
- else:
- self.errorMessage.set('Try agian bud')
- def PageSwitchTwo(self):
- self.controller.showFrame(PageTwo, self.d)
- def PageSwitchFour(self):
- self.controller.showFrame(PageFour, self.d)
- def exit(self):
- exit("Process terminated by user")
- class PageFour(tk.Frame):
- def __init__(self, parent, controller):
- tk.Frame.__init__(self, parent)
- self.controller = controller
- self.errorMessage = StringVar()
- self.errorMessage.set(" ")
- self.d = {}
- self.PageOneResult = StringVar()
- self.PageOneResult.set('<No Data>')
- #Two page two results to store both quadratic roots, cuz math yo
- self.PageTwoResult1 = StringVar()
- self.PageTwoResult1.set('<No Data>')
- self.PageTwoResult2 = StringVar()
- self.PageTwoResult2.set('<No Data>')
- self.PageThreeResult = StringVar()
- self.PageThreeResult.set('<No Data>')
- self.titleLable = Label(self, text="Results Page", font=FONT)
- self.titleLable.grid(row=1, column=1)
- self.getResButton = Button(self, text="Get Results", command=self.Get_Results)
- self.getResButton.grid(row=1, column=2)
- self.resLableP1 = Label(self, textvariable=self.PageOneResult)
- self.resLableP1.grid(row=2, column=0)
- self.resLableP2_1 = Label(self, textvariable=self.PageTwoResult1)
- self.resLableP2_1.grid(row=3, column=0)
- self.resLableP2_2 = Label(self, textvariable=self.PageTwoResult2)
- self.resLableP2_2.grid(row=4, column=0)
- self.resLableP3 = Label(self, textvariable=self.PageThreeResult)
- self.resLableP3.grid(row=5, column=0)
- self.errorMessageLab = Label(self, textvariable=self.errorMessage)
- self.errorMessageLab.grid(row=6, column=1)
- #fancy labels
- self.p1Lab = Label(self, text="<-- Page 1")
- self.p1Lab.grid(row=2, column=1)
- self.p2Lab = Label(self, text="<-- Page 2")
- self.p2Lab.grid(row=3, column=1)
- self.p2_2Lab = Label(self, text="<-- Page 2 (some functions will have 2 results, others may not)")
- self.p2_2Lab.grid(row=4, column=1)
- self.p3Lab = Label(self, text="<-- Page 3")
- self.p3Lab.grid(row=5, column=1)
- def Get_Results(self):
- try:
- self.controller = self.controller.get_data()
- self.PageOneResult.set(self.controller['pageOne'].get())
- self.PageTwoResult1.set(self.controller['pageTwo1'].get())
- self.PageTwoResult2.set(self.controller['pageTwo2'].get())
- self.PageThreeResult.set(self.controller['pageThree'].get())
- except AttributeError:
- self.errorMessage.set("AttributeError: 'dict' object has no attribute 'get_data' (don't click the button twice)")
- app = App()
- app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement