import tkinter as tk from tkinter import ttk # Initialize main window root = tk.Tk() root.title("Promille Rechner") root.geometry("400x600") geschlecht = tk.DoubleVar(value=0.7) gewicht = tk.DoubleVar(value=75.0) drinks = [] alkoholgehalt = tk.StringVar(value="0.000‰") def add_drink(): ml = float(ml_var.get()) percent = float(percent_var.get()) if ml and percent: drinks.append([ml,percent]) update_drinks_display() ml_var.set("") percent_var.set("") def remove_last_drink(): if drinks: # if drinks is not empty drinks.pop() update_drinks_display() def clear_drinks(): drinks = [] update_drinks_display() def update_drinks_display(): drinks_text.config(state=tk.NORMAL) drinks_text.delete(1.0, tk.END) drinks_nums = '\n'.join(f"{drink[0]} ml, {drink[1]}%" for drink in drinks) drinks_text.insert(tk.END, drinks_nums) drinks_text.config(state=tk.DISABLED) alkohol_masse = sum([drink[0] * (drink[1]/100) * 0.8 for drink in drinks]) promille = alkohol_masse / float(gewicht.get()) * float(geschlecht.get()) alkoholgehalt.set(f"{round(promille, 3)}‰") # Ueberschrift tk.Label(root, text="Promille Rechner", font=("Arial", 14)).pack(pady=5) # Geschlecht frame_gender = tk.Frame(root) frame_gender.pack() tk.Radiobutton(frame_gender, text="Männlich", variable=geschlecht, value=0.7).pack(side=tk.LEFT) tk.Radiobutton(frame_gender, text="Weiblich", variable=geschlecht, value=0.6).pack(side=tk.LEFT) tk.Radiobutton(frame_gender, text="Minderjährig", variable=geschlecht, value=0.8).pack(side=tk.LEFT) # Gewicht tk.Label(root, text="Gewicht (kg):").pack() tk.Entry(root, textvariable=gewicht).pack() # Separator ttk.Separator(root, orient="horizontal").pack(fill="x", pady=5) # Drink inputs ml_var = tk.StringVar() percent_var = tk.StringVar() tk.Label(root, text="ml:").pack() tk.Entry(root, textvariable=ml_var).pack() tk.Label(root, text="%:").pack() tk.Entry(root, textvariable=percent_var).pack() tk.Button(root, text="Hinzufügen", command=add_drink).pack() # Drinks display tk.Label(root, text="Getraenke").pack() drinks_text = tk.Text(root, height=5, state=tk.DISABLED) drinks_text.pack() # Buttons to clear drinks tk.Button(root, text="Letztes entfernen", command=remove_last_drink).pack() tk.Button(root, text="Alle entfernen", command=clear_drinks).pack() # Promille text tk.Label(root, text="Dein Promillegehalt").pack() alk_label = tk.Label(root, textvariable=alkoholgehalt).pack() root.mainloop()