Untitled
unknown
plain_text
8 months ago
1.8 kB
22
Indexable
import tkinter as tk
w = tk.Tk()
w.geometry("400x400")
w.title("Calculator")
result_label = tk.Label(w, text="0")
result_label.pack()
result = ""
def update_result(value):
global result
if value == "=":
result_label.configure(text=str(eval(result)))
result = ""
else:
if result == "0":
result = value
else:
result += value
result_label.configure(text=result)
btn_7 = tk.Button(w, text="7", command=lambda: update_result("7"))
btn_7.pack()
btn_8 = tk.Button(w, text="8", command=lambda: update_result("8"))
btn_8.pack()
btn_9 = tk.Button(w, text="9", command=lambda: update_result("9"))
btn_9.pack()
btn_4 = tk.Button(w, text="4", command=lambda: update_result("4"))
btn_4.pack()
btn_5 = tk.Button(w, text="5", command=lambda: update_result("5"))
btn_5.pack()
btn_6 = tk.Button(w, text="6", command=lambda: update_result("6"))
btn_6.pack()
btn_1 = tk.Button(w, text="1", command=lambda: update_result("1"))
btn_1.pack()
btn_2 = tk.Button(w, text="2", command=lambda: update_result("2"))
btn_2.pack()
btn_3 = tk.Button(w, text="3", command=lambda: update_result("3"))
btn_3.pack()
btn_0 = tk.Button(w, text="0", command=lambda: update_result("0"))
btn_0.pack()
btn_add = tk.Button(w, text="+", command=lambda: update_result("+"))
btn_add.pack()
btn_subtract = tk.Button(w, text="-", command=lambda: update_result("-"))
btn_subtract.pack()
btn_multiply = tk.Button(w, text="*", command=lambda: update_result("*"))
btn_multiply.pack()
btn_divide = tk.Button(w, text="/", command=lambda: update_result("/"))
btn_divide.pack()
btn_equal = tk.Button(w, text="=", command=lambda: update_result("="))
btn_equal.pack()
w.mainloop()
Editor is loading...
Leave a Comment