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