Например 12-8=4 после второго нажатие остается только 4
from tkinter import *
from tkinter import messagebox
from tkinter import ttk
import math
clicks = 0
root = Tk()
root.title('Calculator')
def doubleclick():
global clicks
clicks += 1
return clicks
def convert_base(num, to_base=10, from_base=10):
# first convert to decimal number
if isinstance(num, str):
n = int(num, from_base)
else:
n = int(num)
# now convert decimal to 'to_base' base
alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
if n < to_base:
return alphabet[n]
else:
return convert_base(n // to_base, to_base) + alphabet[n % to_base]
def calc(key):
# global memory
if key == "=":
# Исключение букв
if calc_entry.get()[0] not in '-+0123456789.*/':
calc_entry.insert(END, "Первый символ не число!")
messagebox.showerror("Ошибка!", "Вы ввели не число")
# Счет
try:
result = eval(calc_entry.get())
calc_entry.insert(END, "=" + str(result))
except:
calc_entry.insert(END, "Ошибка!")
messagebox.showerror("Ошибка!", "Проверь правильность даних")
# Очистить поле
elif key == "C":
calc_entry.delete(0, END)
label1.config(text='Hex:')
label2.config(text='Dec:')
label3.config(text='Oct:')
label4.config(text='Bin:')
elif key == "del":
calc_entry.delete(len((calc_entry.get())) - 1)
elif key == "sin" and calc_entry.get() != '':
x = int(calc_entry.get())
calc_entry.insert(0, f"sin({calc_entry.get()})={math.sin(math.radians(x))}")
elif key == "cos" and calc_entry.get() != '':
x = int(calc_entry.get())
calc_entry.insert(0, f"cos({calc_entry.get()})={math.cos(math.radians(x))}")
elif key == "tg" and calc_entry.get() != '':
x = int(calc_entry.get())
if x == 90 or x == 270:
calc_entry.insert(END, " Ошибка!")
messagebox.showerror("Ошибка!", "Проверь правильность даних")
else:
calc_entry.insert(0, f"tg({calc_entry.get()})={math.tan(math.radians(x))}")
elif key == "ctg" and calc_entry.get() != '':
x = int(calc_entry.get())
if x == 180:
calc_entry.insert(END, " Ошибка!")
messagebox.showerror("Ошибка!", "Проверь правильность даних")
else:
calc_entry.insert(0, f"ctg({calc_entry.get()})={math.cosh(math.radians(x))}")
# Смена знака
elif key == "-/+":
if "=" in calc_entry.get():
calc_entry.delete(0, END)
try:
if calc_entry.get()[0] == "-":
calc_entry.delete(0)
else:
calc_entry.insert(0, "-")
except IndexError:
pass
elif key == 'Перевод':
hex_to_lable = convert_base(calc_entry.get(), to_base=16)
dec_to_lable = convert_base(calc_entry.get(), to_base=10)
oct_to_lable = convert_base(calc_entry.get(), to_base=8)
bin_to_lable = convert_base(calc_entry.get(), to_base=2)
label1.config(text=f'Hex:{hex_to_lable}')
label2.config(text=f'Dec:{dec_to_lable}')
label3.config(text=f'Oct:{oct_to_lable}')
label4.config(text=f'Bin:{bin_to_lable}')
else:
if "=" in calc_entry.get():
calc_entry.delete(0, END)
calc_entry.insert(END, key)
# Создание кнопок
bttn_list = [
"7", "8", "9", "+", "-",
"4", "5", "6", "*", "/",
"1", "2", "3", "-/+", "=",
"0", ".", "C", "del", "x^2",
"cos", "sin", "tg", "ctg", "Перевод"
]
r = 1
c = 0
for i in bttn_list:
rel = ""
cmd = lambda x=i: calc(x)
ttk.Button(root, text=i, command=cmd).grid(row=r, column=c)
c += 1
if c > 4:
c = 0
r += 1
calc_entry = Entry(root, width=33)
calc_entry.grid(row=0, column=0, columnspan=5)
label1 = Label(text='Hex:', fg="#eee", bg="#333")
label1.grid()
label2 = Label(text="Dec", fg="#eee", bg="#333")
label2.grid()
label3 = Label(text="Oct", fg="#eee", bg="#333")
label3.grid()
label4 = Label(text="Bin", fg="#eee", bg="#333")
label4.grid()
root.mainloop()