import tkinter as tk
from tkinter import messagebox
import math
class VolumeCalculatorApp:
def __init__(self, root):
self.root = root
self.root.title("Калькулятор объема")
self.shape_var = tk.StringVar(value="Куб")
self.shape_var.trace("w", self.update_entries)
self.shapes = ["Куб", "Сфера", "Цилиндр"]
tk.Label(root, text="Выберите фигуру:").grid(
row=0, column=0, padx=10, pady=5)
self.shape_menu = tk.OptionMenu(root, self.shape_var, *self.shapes)
self.shape_menu.grid(row=0, column=1, padx=10, pady=5)
tk.Label(root, text="Длина ребра (Куб) / Радиус (Сфера, Цилиндр):").grid(row=1,
column=0, padx=10, pady=5)
self.entry_1 = tk.Entry(root)
self.entry_1.grid(row=1, column=1, padx=10, pady=5)
self.label_2 = tk.Label(root, text="Высота (Цилиндр):")
self.label_2.grid(row=2, column=0, padx=10, pady=5)
self.entry_2 = tk.Entry(root)
self.entry_2.grid(row=2, column=1, padx=10, pady=5)
self.calculate_button = tk.Button(
root, text="Вычислить объем", command=self.calculate_volume)
self.calculate_button.grid(row=3, column=0, columnspan=2, pady=10)
self.update_entries()
def calculate_volume(self):
shape = self.shape_var.get()
try:
if shape == "Куб":
side = float(self.entry_1.get())
volume = side ** 3
elif shape == "Сфера":
radius = float(self.entry_1.get())
volume = (4/3) * math.pi * radius ** 3
elif shape == "Цилиндр":
radius = float(self.entry_1.get())
height = float(self.entry_2.get())
volume = math.pi * radius ** 2 * height
else:
raise ValueError("Unsupported shape")
messagebox.showinfo(
"Объем", f"Объем {shape.lower()} равен: {volume}")
except ValueError as e:
messagebox.showerror("Ошибка", str(e))
def update_entries(self, *args):
shape = self.shape_var.get()
if shape == "Куб" or shape == "Сфера":
self.label_2.grid_remove()
self.entry_2.grid_remove()
elif shape == "Цилиндр":
self.label_2.grid()
self.entry_2.grid()
if __name__ == "__main__":
root = tk.Tk()
app = VolumeCalculatorApp(root)
root.mainloop()
Результат программы: