Python Forum
ZeroDivisionError: float division by zero
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ZeroDivisionError: float division by zero
#1
Hi,
in line 132, I always get a ZeroDivisionError. Does anybody have a hint to solve the issue??
Actually, I want to show a popup-window, if the user doesn't select an item of the listbox, as soon as the button below is pressing..

from tkinter import*
import tkinter as tk
from tkinter import messagebox as mb
import math


l = 9.92                         # Länge
b = 10.5                         # Breite
h = 2.34                         # Höhe
V = l*b*h                        # Volumen
A = l*b                          # Nutzfläche = Deckenfläche : 104.16m²
A_f = 30.45                      # Fensterfläche                       
A_w = 2*(l*h)+2*(h*b) - A_f      # Wandfläche insgesamt: 65.12m²
V = l*b*h                        # Volumen des Raumes: 243.73m³
AV = A/V                         # Verhältnis Fläche zu Volumen: 0.43m²

# Constants for the geometry of the main window
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 300
APP_HEIGHT = 300
APP_TITLE = "Simple Applikation"
 
# Variables
b = 0
items_for_listbox = ["Musik","Sprache","Vortrag","Spr.+Vor.","Sport"]

def Abs_Materialen(x):
    for n in range (x):
        if x == 1: 
            Abs_Wand = Wand*(0.03+ 0.03+ 0.03+ 0.04+ 0.05+ 0.06)/6
            return(Abs_Wand)
        if x == 2:   
            Abs_Wand = Wand*(0.11+ 0.22+ 0.34+ 0.35+ 0.51+ 0.43)/6
            return(Abs_Wand)
        if x == 3:   
            Abs_Wand = Wand*(0.02+ 0.02+ 0.03+ 0.03+ 0.04+ 0.06)/6
            return(Abs_Wand)
        if x == 4:   
            Abs_Wand = Wand*(0.03+ 0.03+ 0.02+ 0.04+ 0.05+ 0.05)/6
            return(Abs_Wand)
        if x == 11: 
            Abs_Boden = Boden*(0.04+ 0.04+ 0.05+ 0.06+ 0.06+ 0.06)/6
            return(Abs_Boden)
        if x == 12:   
            Abs_Boden = Boden*(0.20+ 0.15+ 0.10+ 0.09+ 0.06+ 0.10)/6
            return(Abs_Boden)
        if x == 13:   
            Abs_Boden = Boden*(0.15+ 0.08+ 0.07+ 0.06+ 0.06+ 0.06)/6
            return(Abs_Boden)
        if x == 14:   
            Abs_Boden = Boden*(0.02+ 0.04+ 0.07+ 0.19+ 0.29+ 0.35)/6
            return(Abs_Boden)       
        if x == 10: 
            Abs_Fenster = Fenster*(0.28+ 0.20+ 0.11+ 0.06+ 0.03+ 0.02)/6
            return(Abs_Fenster)     

        
def Abs_Objekte(x):
    for n in range (x):
        if x == 1: 
            Abs_MP = MP*(0.15+ 0.25+ 0.55+ 0.80+ 0.90+ 0.90)/6
            return(Abs_MP)
        if x == 2:   
            Abs_WP = WP*(0.05+ 0.10+ 0.15+ 0.35+ 0.45+ 0.60)/6
            return(Abs_WP)
        if x == 3:   
            Abs_Stuhl = Stuhl*(0.05+ 0.15+ 0.20+ 0.10+ 0.03+ 0.03)/6
            return(Abs_Stuhl) 

# Eingabe der Flächen in m²:
Boden = 100
Decke = 100
Wand    = 180
Fenster = 80

WP = 18 # Weibliche Person
MP = 19 # Männliche Person

Stuhl = WP + MP

Abs_Fläche =  Abs_Materialen(4)+ Abs_Materialen(11) +Abs_Objekte(1) + Abs_Objekte(2)+ Abs_Objekte(3)
Abs_Fläche



#------------------------ popup -----------------------------------

def popupmsg(msg):
    popup= tk.Tk()
    popup.wm_title("!")
    popup.iconbitmap("C:/Users/PC/Desktop/Icons/attention.ico")
    label = ttk.Label(popup, text=msg, font=NORM_FONT).pack(side="top", fill="x", pady=10)
    B1 = ttk.Button(popup, text="Okay", command = popup.destroy).pack()
    popup.mainloop()

# -----------------------------calculating -------------------------

def select(event):
    global b
    #a = int(mylistbox.get(tk.ANCHOR))
    a = mylistbox.get(tk.ANCHOR)
    b = curselection(a) 
    selected_var.set(b)
    
def curselection(a):
    
    if a=="Musik":
        T_soll_A1 = 0.45*math.log10(V)+0.07                
        return (T_soll_A1)
                
    elif a=="Sprache":
        T_soll_A2 = 0.37*math.log10(V)-0.14             
        return (T_soll_A2)
                
    elif a=="Vortrag":
        T_soll_A3 = 0.32*math.log10(V)-0.17               
        return (T_soll_A3)
                
    elif a=="Spr.+Vor.":    
        T_soll_A4 = 0.26*math.log10(V)-0.14           
        return (T_soll_A4)
        
    elif a=="Sport":    
        T_soll_A5 = 0.75*math.log10(V)-1              
        return (T_soll_A5)
    
    
def calculate():
    global b
    if mylistbox.get(ACTIVE):
        Abs_Fl_ges = 0.163 * V / b # gemäß Sabinesche Formel:T_soll = (0163*V)/A_abs
        Absorber = Abs_Fl_ges - Abs_Fläche
        result_var.set(Absorber)  
    elif b==0:
        tk.messagebox.showinfo("No selection")
              
# GUI
# App window
app_win = tk.Tk()
app_win.title(APP_TITLE) 
app_win.wm_geometry("%dx%d+%d+%d" % (APP_WIDTH, APP_HEIGHT, APP_XPOS, APP_YPOS))
 
# Main container embeded in the app window
# Container for all of the app widgets
main_frame = tk.Frame(app_win)
main_frame.pack(expand=tk.YES, fill=tk.BOTH)
 
# Label to show your listbox selection
selected_var = tk.StringVar()
tk.Label(main_frame, textvariable=selected_var, font=('times', 12, 'bold')
    ).pack(expand=tk.YES)
selected_var.set("No selection")
 
# Label to show your calculated result
result_var = tk.StringVar()
tk.Label(main_frame, textvariable=result_var, font=('times', 12, 'bold')
    ).pack(expand=tk.YES)
result_var.set("No result")
 
# Container for the listbox & scrollbar (embeded in the main frame)
listbox_frame = tk.Frame(main_frame)
listbox_frame.pack(expand=tk.YES)
     
mylistbox = tk.Listbox(listbox_frame, height=5, width=10, font=('times',18))
mylistbox.bind('<<ListboxSelect>>', select)
mylistbox.grid(row=0, column=0)
mylistbox.insert(tk.END, *items_for_listbox)
  
scroll = tk.Scrollbar(listbox_frame, orient=tk.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=tk.N+tk.S) #sticky=N+S+E)
 
# Button to launch a calculation
button=tk.Button(main_frame, text="Fläche der Absorber",
    command=calculate).pack(expand=tk.YES)
    
app_win.mainloop()
Reply


Messages In This Thread
ZeroDivisionError: float division by zero - by Jionni - Feb-19-2020, 12:04 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] rez = (mass / h) | ZeroDivisionError Maryan 5 2,565 Oct-20-2020, 09:38 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020