Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import messagebox, StringVar, OptionMenu
- from datetime import datetime
- def format_temperature(temp):
- return int(temp) if temp.is_integer() else temp
- def display_statistics(temperatures):
- if not temperatures:
- messagebox.showinfo("Статистика", "История температур пуста.")
- return
- avg_temp = sum(temperatures) / len(temperatures)
- max_temp = max(temperatures)
- min_temp = min(temperatures)
- stats = (
- f"Средняя температура: {format_temperature(avg_temp)}°C\n"
- f"Максимальная температура: {format_temperature(max_temp)}°C\n"
- f"Минимальная температура: {format_temperature(min_temp)}°C"
- )
- messagebox.showinfo("Статистика", stats)
- def add_temperature(temperatures, entry):
- try:
- t = float(entry.get())
- if t is not None:
- temperatures.append(t)
- if t < 0:
- messagebox.showwarning("Предупреждение", "Winter is coming ⚠️❄️☃️")
- else:
- messagebox.showinfo("Информация", "Don't worry, be happy")
- entry.delete(0, tk.END)
- except ValueError:
- messagebox.showerror("Ошибка", "Пожалуйста, введите корректное число.")
- def save_history(temperatures, format_type):
- if not temperatures:
- messagebox.showinfo("Сохранение истории", "История температур пуста.")
- return
- filename = f"temperature_history.{format_type}"
- try:
- with open(filename, "w") as file:
- for temp in temperatures:
- file.write(f"{format_temperature(temp)}°C\n")
- messagebox.showinfo("Сохранение истории", f"История температур успешно сохранена в '{filename}'.")
- except Exception as e:
- messagebox.showerror("Ошибка", f"Не удалось сохранить файл: {e}")
- def update_time(label):
- current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
- label.config(text=f"Текущая дата и время: {current_time}")
- label.after(1000, update_time, label)
- def main():
- root = tk.Tk()
- root.title("Датчик уличной температуры")
- root.geometry("400x400")
- root.configure(bg="#e0f7fa")
- title_label = tk.Label(root, text="Датчик уличной температуры", font=("Helvetica", 16, "bold"), bg="#e0f7fa")
- title_label.pack(pady=20)
- time_label = tk.Label(root, bg="#e0f7fa", font=("Helvetica", 12))
- time_label.pack(pady=10)
- update_time(time_label)
- instruction_label = tk.Label(root, text="Введите среднесуточную температуру (°C):", bg="#e0f7fa", font=("Helvetica", 12))
- instruction_label.pack(pady=10)
- entry = tk.Entry(root, font=("Helvetica", 12), width=10)
- entry.pack(pady=10)
- frame = tk.Frame(root, bg="#e0f7fa")
- frame.pack(pady=10)
- temperatures = []
- format_var = StringVar(value="txt")
- formats = ["txt", "csv", "json", "xml", "log"]
- format_menu = OptionMenu(frame, format_var, *formats)
- format_menu.config(bg="#FFC107")
- format_menu.pack(pady=10)
- button_width = 30
- button_height = 2
- btn_add = tk.Button(frame, text="Добавить температуру",
- command=lambda: add_temperature(temperatures, entry),
- bg="#4CAF50", fg="white", font=("Helvetica", 12), width=button_width, height=button_height)
- btn_stats = tk.Button(frame, text="Показать статистику температур",
- command=lambda: display_statistics(temperatures),
- bg="#2196F3", fg="white", font=("Helvetica", 12), width=button_width, height=button_height)
- btn_save = tk.Button(frame, text="Сохранить историю температур",
- command=lambda: save_history(temperatures, format_var.get()),
- bg="#FFC107", fg="black", font=("Helvetica", 12), width=button_width, height=button_height)
- btn_exit = tk.Button(frame, text="Завершить программу",
- command=root.quit,
- bg="#f44336", fg="white", font=("Helvetica", 12), width=button_width, height=button_height)
- btn_add.pack(pady=10)
- btn_stats.pack(pady=10)
- btn_save.pack(pady=10)
- btn_exit.pack(pady=10)
- root.mainloop()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement