Пишу калькулятор на python tkinter и столкнулся с такой проблемой:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\viksl\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "C:\Users\viksl\Desktop\Calc.py", line 29, in calculate
value = float(value)
ValueError: could not convert string to float: '56%'
Я пытался по разному решить проблему, но ничего не получилось. Сама проблема в этой части кода:
def calculate():
value = calc.get()
if value[-1] in '+-/*':
value = value + value[:-1]
elif value[-1] in '%':
value = float(value)
value = str(value/100)
calc.delete(0, tk.END)
calc.insert(0, eval(value))
Сам код:
import tkinter as tk
from tkinter import messagebox
def info2():
messagebox.showinfo('Об авторе', 'Программу выполнил студент группы 19-ВТ2 \nСлепчатов Виктор')
def add_digit(digit):
value = calc.get()
if value[0] == '0' and len(value) == 1:
value = value[1:]
calc.delete(0, tk.END)
calc.insert(0, value+digit)
def add_operation(operation):
value = calc.get()
if value[-1] in '+-*/%':
value = value[:-1]
elif '+' in value or '-' in value or '/' in value or '*' in value or '%' in value:
calculate()
value = calc.get()
calc.delete(0, tk.END)
calc.insert(0, value+operation)
def calculate():
value = calc.get()
if value[-1] in '+-/*':
value = value + value[:-1]
elif value[-1] in '%':
value = float(value)
value = str(value/100)
calc.delete(0, tk.END)
calc.insert(0, eval(value))
'''
def add_perc():
value = calc.get()
value = float(value)
value = str(value/100)
calc.delete(0, tk.END)
calc.insert(0, eval(value))
'''
def clear():
calc.delete(0, tk.END)
calc.insert(0, 0)
def make_digit(digit):
return tk.Button(text=digit, bd=5, font=('Arial', 13), command=lambda: add_digit(digit))
def make_operation_button(operation):
return tk.Button(text=operation, bd=5, font=('Arial', 13), command=lambda: add_operation(operation))
def make_calc_button(operation):
return tk.Button(text=operation, bd=5, font=('Arial', 13), command=calculate)
def make_clear_button(operation):
return tk.Button(text=operation, bd=5, font=('Arial', 13), command=clear)
win = tk.Tk()
win.geometry = (f"240x280+100+200")
win.title('Калькулятор')
calc = tk.Entry(win, justify=tk.RIGHT, font=('Arial', 15), width=15)
calc.insert(0,'0')
calc.grid(row=0, column=0, columnspan=4, stick='we', padx=5)
avtor = tk.Button(text='Об авторе', bd=5, width=15, command=info2)
avtor.grid(row=5, column=0, columnspan=3, stick='wens', padx=5, pady=5)
'''
per = tk.Button(text='%', bd=5, font=('Arial', 13), command=add_perc)
per.grid(row=5, column=3, stick='wens', padx=5, pady=5)
'''
make_digit('1').grid(row=1, column=0, stick='wens', padx=5, pady=5)
make_digit('2').grid(row=1, column=1, stick='wens', padx=5, pady=5)
make_digit('3').grid(row=1, column=2, stick='wens', padx=5, pady=5)
make_digit('4').grid(row=2, column=0, stick='wens', padx=5, pady=5)
make_digit('5').grid(row=2, column=1, stick='wens', padx=5, pady=5)
make_digit('6').grid(row=2, column=2, stick='wens', padx=5, pady=5)
make_digit('7').grid(row=3, column=0, stick='wens', padx=5, pady=5)
make_digit('8').grid(row=3, column=1, stick='wens', padx=5, pady=5)
make_digit('9').grid(row=3, column=2, stick='wens', padx=5, pady=5)
make_digit('0').grid(row=4, column=0, stick='wens', padx=5, pady=5)
make_operation_button('+').grid(row=1, column=3, stick='wens', padx=5, pady=5)
make_operation_button('-').grid(row=2, column=3, stick='wens', padx=5, pady=5)
make_operation_button('*').grid(row=3, column=3, stick='wens', padx=5, pady=5)
make_operation_button('/').grid(row=4, column=3, stick='wens', padx=5, pady=5)
make_operation_button('%').grid(row=5, column=3, stick='wens', padx=5, pady=5)
make_calc_button('=').grid(row=4, column=2, stick='wens', padx=5, pady=5)
make_clear_button('C').grid(row=4, column=1, stick='wens', padx=5, pady=5)
win.grid_columnconfigure(0, minsize=60)
win.grid_columnconfigure(1, minsize=60)
win.grid_columnconfigure(2, minsize=60)
win.grid_columnconfigure(3, minsize=60)
win.grid_rowconfigure(1, minsize=60)
win.grid_rowconfigure(2, minsize=60)
win.grid_rowconfigure(3, minsize=60)
win.grid_rowconfigure(4, minsize=60)
win.grid_rowconfigure(5, minsize=60)
win.mainloop()