I'm trying to make a Density/Volume/Mass calculator. When I enter the values in the Entry widgets and click the button, I get a ValueError: could not convert string to float: '' message. What am I missing? Why doesnt it see the entered values? Oddly, I first made only the density calculator part, and it worked perfectly. Now it doesn't.
import tkinter as tk
from tkinter import ttk
def density_calculator():
def density():
vol = float(vol_value.get())
mas = float(mass_value.get())
dens = mas / vol
calculated_density = ttk.Label(root, text=f"{dens}", font=("Helvetica", 20))
calculated_density.place(x=280, y=290)
density_w = tk.Tk()
density_w.title("Density Calculator")
density_w.geometry("600x400")
density_w.resizable(False, False)
vol_value = tk.StringVar()
mass_value = tk.StringVar()
volume = ttk.Label(density_w, text="Volume (cm^3)", relief="ridge", font=("Helvetica", 20))
mass = ttk.Label(density_w, text="Mass (g)", relief="ridge", font=("Helvetica", 20))
density_label = ttk.Label(density_w, text="Density (g/cm3)", relief="ridge", font=("Helvetica", 20))
volume_entry = ttk.Entry(density_w, textvariable=vol_value)
mass_entry = ttk.Entry(density_w, textvariable=mass_value)
volume.place(x=65, y=50)
mass.place(x=405, y=50)
density_label.place(x=200, y=250)
volume_entry.place(x=100, y=100)
mass_entry.place(x=400, y=100)
done_button = ttk.Button(density_w, text="Calculate", command=density)
done_button.place(x=480, y=350)
density_w.mainloop()
def volume_calculator():
def volume():
vol = float(dens_value.get())
mas = float(mass_value.get())
dens = mas / vol
calculated_density = ttk.Label(root, text=f"{dens}", font=("Helvetica", 20))
calculated_density.place(x=280, y=290)
volume_w = tk.Tk()
volume_w.title("Volume Calculator")
volume_w.geometry("600x400")
volume_w.resizable(False, False)
dens_value = tk.StringVar()
mass_value = tk.StringVar()
density = ttk.Label(volume_w, text="Density (g/cm^3)", relief="ridge", font=("Helvetica", 20))
mass = ttk.Label(volume_w, text="Mass (g)", relief="ridge", font=("Helvetica", 20))
volume_label = ttk.Label(volume_w, text="Volume (cm3)", relief="ridge", font=("Helvetica", 20))
density_entry = ttk.Entry(volume_w, textvariable=dens_value)
mass_entry = ttk.Entry(volume_w, textvariable=mass_value)
density.place(x=65, y=50)
mass.place(x=405, y=50)
volume_label.place(x=200, y=250)
density_entry.place(x=100, y=100)
mass_entry.place(x=400, y=100)
done_button = ttk.Button(volume_w, text="Calculate", command=volume)
done_button.place(x=480, y=350)
volume_w.mainloop()
root = tk.Tk()
root.title("Density, Volume, Mass Calculator")
root.geometry("200x100")
root.resizable(False, False)
density_choice = ttk.Radiobutton(root, text="Calculate Density", command=density_calculator)
volume_choice = ttk.Radiobutton(root, text="Calculate Volume", command=volume_calculator)
mass_choice = ttk.Radiobutton(root, text="Calculate Density")
density_choice.place(x=0, y=0)
volume_choice.place(x=0, y=20)
root.mainloop()
CodePudding user response:
After changing the following two things your code is now working fine
- Replace StringVar variable with entry variable when getting value
- Use actual window name in the Label creation for showing result
Final code is following
import tkinter as tk
from tkinter import ttk
def density_calculator():
def density():
vol = float(volume_entry.get())
mas = float(mass_entry.get())
dens = round(mas / vol, 2)
calculated_density = ttk.Label(density_w, text=f"{dens}", font=("Helvetica", 20))
calculated_density.place(x=280, y=290)
density_w = tk.Tk()
density_w.title("Density Calculator")
density_w.geometry("600x400")
density_w.resizable(False, False)
vol_value = tk.StringVar()
mass_value = tk.StringVar()
volume = ttk.Label(density_w, text="Volume (cm^3)", relief="ridge", font=("Helvetica", 20))
mass = ttk.Label(density_w, text="Mass (g)", relief="ridge", font=("Helvetica", 20))
density_label = ttk.Label(density_w, text="Density (g/cm3)", relief="ridge", font=("Helvetica", 20))
volume_entry = ttk.Entry(density_w, textvariable=vol_value)
mass_entry = ttk.Entry(density_w, textvariable=mass_value)
volume.place(x=65, y=50)
mass.place(x=405, y=50)
density_label.place(x=200, y=250)
volume_entry.place(x=100, y=100)
mass_entry.place(x=400, y=100)
done_button = ttk.Button(density_w, text="Calculate", command=density)
done_button.place(x=480, y=350)
density_w.mainloop()
def volume_calculator():
def volume():
vol = float(density_entry.get())
mas = float(mass_entry.get())
dens = round(mas / vol, 2)
calculated_density = ttk.Label(volume_w, text=f"{dens}", font=("Helvetica", 20))
calculated_density.place(x=280, y=290)
volume_w = tk.Tk()
volume_w.title("Volume Calculator")
volume_w.geometry("600x400")
volume_w.resizable(False, False)
dens_value = tk.StringVar()
mass_value = tk.StringVar()
density = ttk.Label(volume_w, text="Density (g/cm^3)", relief="ridge", font=("Helvetica", 20))
mass = ttk.Label(volume_w, text="Mass (g)", relief="ridge", font=("Helvetica", 20))
volume_label = ttk.Label(volume_w, text="Volume (cm3)", relief="ridge", font=("Helvetica", 20))
density_entry = ttk.Entry(volume_w, textvariable=dens_value)
mass_entry = ttk.Entry(volume_w, textvariable=mass_value)
density.place(x=65, y=50)
mass.place(x=405, y=50)
volume_label.place(x=200, y=250)
density_entry.place(x=100, y=100)
mass_entry.place(x=400, y=100)
done_button = ttk.Button(volume_w, text="Calculate", command=volume)
done_button.place(x=480, y=350)
volume_w.mainloop()
root = tk.Tk()
root.title("Density, Volume, Mass Calculator")
root.geometry("200x100")
root.resizable(False, False)
density_choice = ttk.Radiobutton(root, text="Calculate Density", command=density_calculator)
volume_choice = ttk.Radiobutton(root, text="Calculate Volume", command=volume_calculator)
mass_choice = ttk.Radiobutton(root, text="Calculate Density")
density_choice.place(x=0, y=0)
volume_choice.place(x=0, y=20)
root.mainloop()
Output is Following