Как сделать так, чтобы мой калькулятор правильно вводил операторы, заменяя вводимые операторы вместо того, чтобы вводить их вместе
Вместо того, чтобы калькулятор вводил несколько входов рядом друг с другом, например, "+-/**-+", я хочу, чтобы существующий оператор заменял все, что нажимается, если рядом с ним нет числа. Я пытаюсь использовать оператор if в функции input(), чтобы проверить, введен ли оператор, а затем вводить только числа. Вот так
def input(x):
if x in "+-/*.:
output.insert('end', x)
Но это заставляет меня вводить только числа в калькулятор, я хотел бы сначала предотвратить это, но затем перейти к вводу операторов, а также делать то, что я ранее упоминал с операторами.
from tkinter import *
window = Tk()
window.title('Calculator')
#inserts numbers or operators into output
def input(x):
output.insert('end', x)
#clears calculator
def clear():
output.delete('1.0', 'end')
#solves equations
def solve():
equation = output.get('end -1 lines linestart', 'end -1 lines lineend')
try:
answer = eval(equation)
output.insert('end', '\n')
output.insert('end', answer)
except:
output.insert('end', '\n' + 'ERROR' + '\n')
#output for calculator
output = Text(window, font = 'none 12 bold', height = 2, width = 25, wrap = 'word')
output.grid(row = 0, column = 0, columnspan = 4, pady = 10)
###buttons
#clear and operators
b_clear = Button(window, text = 'C', width = 7, height = 3, command = clear)
b_clear.grid(row = 1, column = 2, padx = (10, 0))
b_div = Button(window, text = '/', width = 7, height = 3, command = lambda: input('/'))
b_div.grid(row = 1, column = 3, padx = 10)
b_mult = Button(window, text = '*', width = 7, height = 3, command = lambda: input('*'))
b_mult.grid(row = 2, column = 3)
b_subt = Button(window, text = '-', width = 7, height = 3, command = lambda: input('-'))
b_subt.grid(row = 3, column = 3)
b_add = Button(window, text = '+', width = 7, height = 3, command = lambda: input('+'))
b_add.grid(row = 4, column = 3)
b_equal = Button(window, text = '=', width = 7, height = 3, command = solve)
b_equal.grid(row = 5, column = 3, pady = (0, 10))
#numbers
b_9 = Button(window, text = '9', width = 7, height = 3, command = lambda: input('9'))
b_9.grid(row = 2, column = 2, padx = (10, 0), pady = 10)
b_8 = Button(window, text = '8', width = 7, height = 3, command = lambda: input('8'))
b_8.grid(row = 2, column = 1)
b_7 = Button(window, text = '7', width = 7, height = 3, command = lambda: input('7'))
b_7.grid(row = 2, column = 0, padx = 10)
b_6 = Button(window, text = '6', width = 7, height = 3, command = lambda: input('6'))
b_6.grid(row = 3, column = 2, padx = (10, 0))
b_5 = Button(window, text = '5', width = 7, height = 3, command = lambda: input('5'))
b_5.grid(row = 3, column = 1)
b_4 = Button(window, text = '4', width = 7, height = 3, command = lambda: input('4'))
b_4.grid(row = 3, column = 0)
b_3 = Button(window, text = '3', width = 7, height = 3, command = lambda: input('3'))
b_3.grid(row = 4, column = 2, padx = (10, 0), pady = 10)
b_2 = Button(window, text = '2', width = 7, height = 3, command = lambda: input('2'))
b_2.grid(row = 4, column = 1)
b_1 = Button(window, text = '1', width = 7, height = 3, command = lambda: input('1'))
b_1.grid(row = 4, column = 0)
b_0 = Button(window, text = '0', width = 7, height = 3, command = lambda: input('0'))
b_0.grid(row = 5, column = 0, pady = (0, 10))
b_decimal = Button(window, text = '.', width = 7, height = 3, command = lambda: input('.'))
b_decimal.grid(row = 5, column = 1, pady = (0, 10))
b_negative = Button(window, text = '(-)', width = 7, height = 3, command = lambda: input('-'))
b_negative.grid(row = 5, column = 2, padx = (10, 0), pady = (0, 10))
#run calculator
window.mainloop()