Почему я получаю эту ошибку typeError ?: неподдерживаемые типы операндов для +: 'float' и 'NoneType' - PullRequest
0 голосов
/ 06 мая 2020

Привет, я сейчас пытаюсь использовать tkinter для создания калькулятора без кнопок, в основном это обычный старый ввод с использованием функции Text, но у меня довольно много проблем, и я не могу понять, почему.

Вот как выглядит мой код (по крайней мере, важная часть):

def calculator():
    global inputWindow, outputWindow
    displayCalculator = Canvas(mainWindow, width=796, height=796, bg=clrMain)
    displayCalculator.grid(column=2, row=1)

    #inputbaren för kalkulatorn
    inputWindow = Text(displayCalculator, width=70, height=1, bg=clrMain, font=("Times", 15))
    displayCalculator.create_window(4,15, anchor=NW, window=inputWindow) #Skapar objektet

    #knapp för inputbaren
    bEnter = Button(displayCalculator, text="=", command=output, bg=clrButton, width=7, height=1)
    displayCalculator.create_window(710,15, anchor=NW, window=bEnter) #Skapar objektet

    #information
    lblCommands = Label(displayCalculator, text="Addition: + | Subtraktion: - | Multiplikation: * | Divition: / ", bg=clrMain, font=("Arial Bold", 12))
    displayCalculator.create_window(4,52, anchor=NW, window=lblCommands)
    lblExample = Label(displayCalculator, text="100+100 | Ingen mellanrum mellan talen.", bg=clrMain, font=("Arial Bold", 12))
    displayCalculator.create_window(4,84, anchor=NW, window=lblExample)
    #canvasDivider = Canvas(displayCalculator, width=780, height=1, bg=clrMenu)
    #displayCalculator.create_window(4,130, anchor=NW, window=canvasDivider)

    #output window
    outputWindow = Text(displayCalculator, width=70, height=10, bg=clrMain, font=("Times", 15))
    displayCalculator.create_window(4,120, anchor=NW, window=outputWindow) #Skapar objektet7

#outputwindown för kalkylatorn

def calculation(usrInput):
    calculation 
    usrInput = str(usrInput)

    if usrInput.isdigit():
        return float(usrInput)

    for c in ('-','+','*','/'):
        left, op, right = usrInput.partition(c)
        if op == '*':
            return calculation(left) * calculation(right)
        elif op == '/':
            return calculation(left) / calculation(right)
        elif op == '+':
            return calculation(left) + calculation(right)
        elif op == '-':
            return calculation(left) - calculation(right)

def output():
    global inputWindow, outputWindow 
    outputWindow.configure(state=NORMAL)
    usrInput = inputWindow.get("1.0",END)

    print(calculation(usrInput))

    inputWindow.delete('1.0', 'end-1c')
    outputWindow.insert(END, usrInput)
    outputWindow.configure(state=DISABLED)

кстати, я просто пытаюсь распечатать написанное сообщение в окне ввода "outputWindow.insert (END, usrInput)", я просто пытаюсь заставить "print (вычисление (usrInput))" работать.

Спасибо за помощь! (:

...