|
| 1 | +import tkinter as tk |
| 2 | + |
| 3 | +calculations = 'Addition' |
| 4 | + |
| 5 | +def callback (selection): |
| 6 | + global calculations |
| 7 | + calculations = selection |
| 8 | + |
| 9 | +def calculate (): |
| 10 | + global calculations |
| 11 | + val1 = 0 if not entry.get() else float (entry.get()) |
| 12 | + val2 = 0 if not entry2.get() else float (entry2.get()) |
| 13 | + |
| 14 | + if calculations == 'Addition': |
| 15 | + output.config(text = val1 + val2) |
| 16 | + elif calculations == 'Subtraktion': |
| 17 | + output.config(text = val1 - val2) |
| 18 | + elif calculations == 'Multiplikation': |
| 19 | + output.config (text = val1 * val2 ) |
| 20 | + elif calculations == 'Division': |
| 21 | + try: |
| 22 | + output.config (text = val1 / val2) |
| 23 | + except ZeroDivisionError: |
| 24 | + output.config (text = 'Durch 0 ist nicht möglich!') |
| 25 | + |
| 26 | +window = tk.Tk () |
| 27 | +window.geometry ("750x500") |
| 28 | +window.title ('Taschenrechner') |
| 29 | + |
| 30 | +OptionList = ['Addition', 'Subtraktion', 'Multiplikation', 'Division'] |
| 31 | + |
| 32 | +var1 = tk.StringVar(window) |
| 33 | +var1.set (OptionList [0]) |
| 34 | + |
| 35 | +entry = tk.Entry(window) |
| 36 | +entry.grid(row=0 , column=1) |
| 37 | + |
| 38 | +entry2 = tk.Entry(window) |
| 39 | +entry2.grid(row=0 , column=2) |
| 40 | + |
| 41 | +button = tk.Button(window, text='Berechnen', command=calculate) |
| 42 | +button.grid(row=0, column=0) |
| 43 | + |
| 44 | +opt = tk.OptionMenu (window, var1, *OptionList, command=callback) |
| 45 | +opt.config(font= ('Arial', 14)) |
| 46 | +opt.grid (row=2 , column=0) |
| 47 | + |
| 48 | +output = tk.Label(window) |
| 49 | +output.grid(row=3) |
| 50 | + |
| 51 | + |
| 52 | +window.mainloop() |
0 commit comments