Mar-03-2020, 08:03 PM
With the help of the function sumAbsorptionArea, in Line 301, I want to sum up the two functions: getBottomChoice and getWallChoice. And after that, I just want to print out the value of the variable sum. But, no matter I try, I didn't find the solution yet...
import tkinter as tk from tkinter import ttk from tkinter import * import math LARGE_FONT = ("Verdana", 12) #================================ class reverberationTime =========================================================================== class ReverberationTime(tk.Tk): def __init__(self,*args,**kwargs): tk.Tk.__init__(self, *args, **kwargs) tk.Tk.wm_title(self, "reverberation time") tk.Tk.iconbitmap(self,"C:/Users/PC/Desktop/Icons/speaker.ico") container = tk.Frame(self) container.pack(side="top", fill="both", expand=True) container.grid_rowconfigure(0, weight=1) container.grid_columnconfigure(0, weight=1) self.frames = {} for F in (StartPage, PageOne, PageTwo, PageThree): frame = F(container, self) self.frames[F] = frame frame.grid(row=0, column=0, sticky="nsew") self.show_frame(StartPage) def show_frame(self,cont): frame = self.frames[cont] frame.tkraise() #================================ superclass Page =========================================================================== class Pages: p = 0 z = 1 abs_flä = 1 #-----bottoms: Parkett ------ parkett_125 = 0.04 parkett_250 = 0.04 parkett_500 = 0.05 parkett_1000 = 0.06 parkett_2000 = 0.06 parkett_4000 = 0.06 #----bottoms: Linoleum ------ linoleum_125 = 0.02 linoleum_250 = 0.02 linoleum_500 = 0.03 linoleum_1000 = 0.03 linoleum_2000 = 0.04 linoleum_4000 = 0.04 #----bottoms: PVC ------ pvc_125 = 0.01 pvc_250 = 0.02 pvc_500 = 0.01 pvc_1000 = 0.03 pvc_2000 = 0.05 pvc_4000 = 0.05 #-----walls: Tapete ------ tapete_125 = 0.02 tapete_250 = 0.03 tapete_500 = 0.04 tapete_1000 = 0.05 tapete_2000 = 0.07 tapete_4000 = 0.08 #----walls: Glattputz ------ glattputz_125 = 0.02 glattputz_250 = 0.02 glattputz_500 = 0.03 glattputz_1000 = 0.03 glattputz_2000 = 0.04 glattputz_4000 = 0.06 #----walls: Mauerziegelwand ------ mauerziegelwand_125 = 0.03 mauerziegelwand_250 = 0.03 mauerziegelwand_500 = 0.03 mauerziegelwand_1000 = 0.04 mauerziegelwand_2000 = 0.05 mauerziegelwand_4000 = 0.06 #================================ class StartPage =========================================================================== class StartPage(tk.Frame): def __init__(self, parent, controller): tk.Frame.__init__(self,parent) label = tk.Label(self, text="reverberation time", font=LARGE_FONT) label.pack(pady = 10, padx = 10) button = ttk.Button(self, text="welcome!", command=lambda: controller.show_frame(PageOne)).pack() #================================ class PageOne =========================================================================== class PageOne(tk.Frame, Pages): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) NORMAL_FONT=("Arial",11) title = tk.Label(self, text="Please enter the room dimensions.", font=LARGE_FONT) title.pack(pady = 10, padx = 10) frame = Frame(self) frame.pack(pady=20) self.lenght = StringVar() self.width = StringVar() self.height = StringVar() self.v = StringVar() dimensions = Frame(frame) dimensions.pack(side='left', pady=5) entryfields = Frame(frame) entryfields.pack(side='right', pady=5) lblLenght = Label(dimensions, text="lenght:", font=NORMAL_FONT) lblLenght.pack(pady=3) lblWidth = Label(dimensions, text="width:", font=NORMAL_FONT) lblWidth.pack(pady=4) lblHeight = Label(dimensions, text="height:", font=NORMAL_FONT) lblHeight.pack(pady=4) lblVolume = Label(dimensions, textvariable = self.v) lblVolume.pack() entLength = Entry(entryfields, textvariable = self.lenght) entLength.pack(pady=6) entWidth = Entry(entryfields, textvariable = self.width) entWidth.pack(pady=6) entHeight = Entry(entryfields, textvariable = self.height) entHeight.pack(pady=6) btncalculate = ttk.Button(self, text="calculate") btncalculate.pack() btncalculate.bind("<Button-1>", self.calculate) btnPageTwo = ttk.Button(self, text="Page 2", command=lambda: controller.show_frame(PageTwo)) btnPageTwo.pack() btnStartPage = ttk.Button(self, text="Start Page", command=lambda: controller.show_frame(StartPage)) btnStartPage.pack() def calculate(self, carryout): try: l = float(self.lenght.get()) b = float(self.width.get()) h = float(self.height.get()) m = l*b*h self.v.set("volume: % .2f m³" % m) Pages.p = m except ValueError: tk.messagebox.showinfo('No valid input.','Please enter only numbers!',icon = 'warning') #================================ class PageTwo =========================================================================== class PageTwo(tk.Frame, Pages): def __init__(self, parent, controller): tk.Frame.__init__(self, parent) NORMAL_FONT=("Arial",11) title = tk.Label(self, text="Please select the bottom abilities.", font=LARGE_FONT) title.pack(pady = 10, padx = 10) bottomMaterial =StringVar() self.bottom = StringVar() wallMaterial =StringVar() self.wall = StringVar() frame = Frame(self) frame.pack(pady=20) dimensions = Frame(frame) dimensions.pack(side='left', pady=5) entBottom = Entry(dimensions, textvariable = self.bottom) entBottom.grid(pady = 5) self.cboBottomMaterial = ttk.Combobox(dimensions, textvariable = bottomMaterial, state = 'readonly',font = ('arial', 14, 'bold'), width = 19) self.cboBottomMaterial ['value'] = ('Parkett', 'Linoleum', 'PVC') self.cboBottomMaterial.current(0) self.cboBottomMaterial.grid() btnBottomChoice = ttk.Button(dimensions, text = "OK", command = self.getBottomChoice) btnBottomChoice.grid() entWall = Entry(dimensions, textvariable = self.wall) entWall.grid(pady = 5) self.cboWallMaterial = ttk.Combobox(dimensions, textvariable = wallMaterial, state = 'readonly',font = ('arial', 14, 'bold'), width = 19) self.cboWallMaterial ['value'] = ('Tapete', 'Glattputz', 'Mauerziegelwand') self.cboWallMaterial.current(0) self.cboWallMaterial.grid() btnWallChoice = ttk.Button(dimensions, text = "OK", command = self.getWallChoice) btnWallChoice.grid() btnsumAbsorptionArea = ttk.Button(self, text="sum") btnsumAbsorptionArea.pack() btnsumAbsorptionArea.bind("<Button-1>", self.sumAbsorptionArea) btnPageTwo = ttk.Button(self, text="Page 1", command=lambda: controller.show_frame(PageOne)) btnPageTwo.pack() btnStartPage = ttk.Button(self, text="Start Page", command=lambda: controller.show_frame(StartPage)) btnStartPage.pack() btnStartPage = ttk.Button(self, text="Page 3", command=lambda: controller.show_frame(PageThree)) btnStartPage.pack() def getBottomChoice(self): if self.cboBottomMaterial.get() == "Parkett": Pages.parkett_125 = Pages.parkett_125 * float(self.bottom.get()) Pages.parkett_250 = Pages.parkett_250 * float(self.bottom.get()) Pages.parkett_500 = Pages.parkett_500 * float(self.bottom.get()) Pages.parkett_1000 = Pages.parkett_1000 * float(self.bottom.get()) Pages.parkett_2000 = Pages.parkett_2000 * float(self.bottom.get()) Pages.parkett_4000 = Pages.parkett_4000 * float(self.bottom.get()) elif self.cboBottomMaterial.get() == "Linoleum": Pages.linoleum_125 = Pages.linoleum_125 * float(self.bottom.get()) Pages.linoleum_250 = Pages.linoleum_250 * float(self.bottom.get()) Pages.linoleum_500 = Pages.linoleum_500 * float(self.bottom.get()) Pages.linoleum_1000 = Pages.linoleum_1000 * float(self.bottom.get()) Pages.linoleum_2000 = Pages.linoleum_2000 * float(self.bottom.get()) Pages.linoleum_4000 = Pages.linoleum_4000 * float(self.bottom.get()) elif self.cboBottomMaterial.get() == "PVC": Pages.pvc_125 = Pages.pvc_125 * float(self.bottom.get()) Pages.pvc_250 = Pages.pvc_250 * float(self.bottom.get()) Pages.pvc_500 = Pages.pvc_500 * float(self.bottom.get()) Pages.pvc_1000 = Pages.pvc_1000 * float(self.bottom.get()) Pages.pvc_2000 = Pages.pvc_2000 * float(self.bottom.get()) Pages.pvc_4000 = Pages.pvc_4000 * float(self.bottom.get()) print(Pages.pvc_2000) elif self.cboBottomMaterial.get() == "": messagebox.showinfo('No valid input.','Please select.',icon = 'warning') def getWallChoice(self): if self.cboWallMaterial.get() == "Tapete": Pages.tapete_125 = Pages.tapete_125 * float(self.wall.get()) Pages.tapete_250 = Pages.tapete_250 * float(self.wall.get()) Pages.tapete_500 = Pages.tapete_500 * float(self.wall.get()) Pages.tapete_1000 = Pages.tapete_1000 * float(self.wall.get()) Pages.tapete_2000 = Pages.tapete_2000 * float(self.wall.get()) Pages.tapete_4000 = Pages.tapete_4000 * float(self.wall.get()) elif self.cboWallMaterial.get() == "Glattputz": Pages.glattputz_125 = Pages.glattputz_125 * float(self.wall.get()) Pages.glattputz_250 = Pages.glattputz_250 * float(self.wall.get()) Pages.glattputz_500 = Pages.glattputz_500 * float(self.wall.get()) Pages.glattputz_1000 = Pages.glattputz_1000 * float(self.wall.get()) Pages.glattputz_2000 = Pages.glattputz_2000 * float(self.wall.get()) Pages.glattputz_4000 = Pages.glattputz_4000 * float(self.wall.get()) elif self.cboWallMaterial.get() == "Mauerziegelwand": Pages.mauerziegelwand_125 = Pages.mauerziegelwand_125 * float(self.wall.get()) Pages.mauerziegelwand_250 = Pages.mauerziegelwand_250 * float(self.wall.get()) Pages.mauerziegelwand_500 = Pages.mauerziegelwand_500 * float(self.wall.get()) Pages.mauerziegelwand_1000 = Pages.mauerziegelwand_1000 * float(self.wall.get()) Pages.mauerziegelwand_2000 = Pages.mauerziegelwand_2000 * float(self.wall.get()) Pages.mauerziegelwand_4000 = Pages.mauerziegelwand_4000 * float(self.wall.get()) print(Pages.mauerziegelwand_4000) elif self.cboWallMaterial.get() == "": messagebox.showinfo('No valid input.','Please select.',icon = 'warning') def sumAbsorptionArea(self, choice): sum = self.getBottomChoice() + self.getWallChoice() print(sum) # self.bottom = StringVar() # self.wall = StringVar() # self.roof = StringVar() # self.window = StringVar() # self.reverberationTime = StringVar() # # # dimensions = Frame(frame) # dimensions.pack(side='left', pady=5) # # entryfields = Frame(frame) # entryfields.pack(side='right', pady=5) # # lblVolume = Label(dimensions, textvariable = self.reverberationTime) # lblVolume.pack() # # lblBottom = Label(dimensions, text="bottom:", font=NORMAL_FONT) # lblBottom.pack(pady=3) # lblWall = Label(dimensions, text="wall:", font=NORMAL_FONT) # lblWall.pack(pady=4) # lblRoof = Label(dimensions, text="roof:", font=NORMAL_FONT) # lblRoof.pack(pady=4) # lblWindow = Label(dimensions, text="window:", font=NORMAL_FONT) # lblWindow.pack(pady=4) # #lblVolume = Label(dimensions, textvariable = self.v).pack() # # # entBottom = Entry(entryfields, textvariable = self.bottom) # entBottom.pack(pady=6) # entWall = Entry(entryfields, textvariable = self.wall) # entWall.pack(pady=6) # entRoof = Entry(entryfields, textvariable = self.roof) # entRoof.pack(pady=6) # entWindow = Entry(entryfields, textvariable = self.window) # entWindow.pack(pady=6) # # # btncalculate = ttk.Button(self, text="calculate") # btncalculate.pack() # btncalculate.bind("<Button-1>", self.calculate) # # # def calculate(self, carryout): # try: # b = float(self.bottom.get()) # w = float(self.wall.get()) # r = float(self.roof.get()) # wi = float(self.window.get()) # abs_fläche = b + w + wi # self.reverberationTime.set("absorption rate: % .2f m³" % abs_fläche) # Pages.abs_flä = abs_fläche # except ValueError: # tk.messagebox.showinfo('No valid input.','Please enter only numbers!',icon = 'warning') #================================ class PageThree =========================================================================== class PageThree(tk.Frame, Pages): def passvariable(self, var): self.s.set("volume: % .2f m³" % Pages.p) def absorptionsrate(self, var): self.abs_rate.set("volume: % .2f m³" % Pages.abs_flä) def __init__(self, parent, controller): tk.Frame.__init__(self, parent) frame = Frame(self) frame.pack(pady=20) result_var = StringVar() selected_var = StringVar() self.s = StringVar() items_for_listbox = ["Musik","Sprache","Vortrag","Spr.+Vor.","Sport"] self.abs_rate = StringVar() def select(event): a = mylistbox.get(ANCHOR) Pages.z = curselection(a) selected_var.set(Pages.z) def curselection(a): if a=="Musik": T_soll_A1 = 0.45*math.log10(Pages.p)+0.07 return (T_soll_A1) elif a=="Sprache": T_soll_A2 = 0.37*math.log10(Pages.p)-0.14 return (T_soll_A2) elif a=="Vortrag": T_soll_A3 = 0.32*math.log10(Pages.p)-0.17 return (T_soll_A3) elif a=="Spr.+Vor.": T_soll_A4 = 0.26*math.log10(Pages.p)-0.14 return (T_soll_A4) elif a=="Sport": T_soll_A5 = 0.75*math.log10(Pages.p)-1 return (T_soll_A5) def calc(): if mylistbox.get(ACTIVE): Abs_Fl_ges = 0.163 * Pages.p / Pages.z Absorber = Abs_Fl_ges - Pages.abs_flä result_var.set(Absorber) elif Pages.z == 0: messagebox.showinfo("No selection") self.dimension = Frame(frame) self.dimension.pack(side='left', pady=5) lblPageTwo = tk.Label(self, text="Page 2", font=LARGE_FONT) lblPageTwo.pack(pady = 10, padx = 10) lblAbs_rate = tk.Label(self.dimension, textvariable = self.abs_rate) lblAbs_rate.pack() pasvar = tk.Label(self.dimension, textvariable = self.s) pasvar.pack() lblselection = tk.Label(self.dimension, textvariable=selected_var) lblselection.pack(expand=YES) selected_var.set("No selection") lblresult = Label(self.dimension, textvariable=result_var) lblresult.pack(expand=YES) result_var.set("No result") listbox_frame = Frame(self.dimension) listbox_frame.pack(expand=YES) mylistbox = Listbox(listbox_frame, height=5, width=10, font=('times',18)) mylistbox.bind('<<ListboxSelect>>', select) mylistbox.grid(row=0, column=0) mylistbox.insert(END, *items_for_listbox) scroll = Scrollbar(listbox_frame, orient=VERTICAL) # the allignment of the scrollbar mylistbox["yscrollcommand"] = scroll.set # link the list with the scroll scroll["command"] = mylistbox.yview # link the scroll with the scroll scroll.grid(row=0, column=1, sticky=N+S) #sticky=N+S+E) btnAbsorber=Button(self.dimension, text="Fläche der Absorber", command=calc) btnAbsorber.pack(expand=YES) btnStartPage = ttk.Button(self, text="Start Page", command=lambda: controller.show_frame(StartPage)) btnStartPage.pack() btnPageOne = ttk.Button(self, text="Page 1", command=lambda: controller.show_frame(PageOne)) btnPageOne.pack() btnPageTwo = ttk.Button(self, text="Page 2", command=lambda: controller.show_frame(PageTwo)) btnPageTwo.pack() btnPassVariable = ttk.Button(self, text="pasvariable") btnPassVariable.pack() btnPassVariable.bind("<Button-1>", self.passvariable) btnAbs_rate = ttk.Button(self, text="pass absorption rate") btnAbs_rate.pack() btnAbs_rate.bind("<Button-1>", self.absorptionsrate) app = ReverberationTime() app.mainloop()