Make clear button backspace by one in python tkinter calculator
Question: I am making a calculator in python using tkinter. The calculator works very
well apart from one thing. At the moment I have set the calculator to clear
the display. I want to make it so that it clears the last number on the
display. For example 564 would become 56.

Any help greatly appreciated.

    
    
    #Import tkinter
    from Tkinter import *
    #Setup quit button
    def quit():
        window.destroy()
    #What happens when you click the button
    def buttonclick(event):
        global calcvalue
        global savedvalue
        global operator
        pressed = ""
        if event.x >10 and event.x <70 and event.y > 50 and event.y < 110 : pressed = 7
        if event.x >10 and event.x <70 and event.y > 120 and event.y < 180 : pressed = 4
        if event.x >10 and event.x <70 and event.y > 190 and event.y < 250 : pressed = 1
        if event.x >10 and event.x <70 and event.y > 260 and event.y < 320 : pressed = 0
        if event.x >80 and event.x <140 and event.y > 50 and event.y < 110 : pressed = 8
        if event.x >80 and event.x <140 and event.y > 120 and event.y < 180 : pressed = 5
        if event.x >80 and event.x <140 and event.y > 190 and event.y < 250 : pressed = 2
        if event.x >150 and event.x <210 and event.y > 50 and event.y < 110 : pressed = 9
        if event.x >150 and event.x <210 and event.y > 120 and event.y < 180 : pressed = 6
        if event.x >150 and event.x <210 and event.y > 190 and event.y < 250 : pressed = 3
        if event.x >80 and event.x <140 and event.y > 260 and event.y < 320 : pressed = "equals"
        if event.x >150 and event.x <210 and event.y > 260 and event.y < 320 : pressed = "clear"
        if event.x >220 and event.x <280 and event.y > 50 and event.y < 110 : pressed = "divide"
        if event.x >220 and event.x <280 and event.y > 120 and event.y < 180 : pressed = "times"
        if event.x >220 and event.x <280 and event.y > 190 and event.y < 250 : pressed = "minus"
        if event.x >220 and event.x <280 and event.y > 260 and event.y < 320 : pressed = "plus"
    
    
        if pressed == 0 or pressed == 1 or pressed == 2 or pressed == 3 or pressed == 4 or pressed == 5 or pressed == 6 or pressed == 7 or pressed == 8 or pressed == 9 :
            calcvalue = calcvalue * 10 + pressed
    
    
        if pressed == "divide" or pressed == "times" or pressed == "minus" or pressed == "plus" :
            operator = pressed
            savedvalue = calcvalue
            calcvalue = 0
    
        if pressed == "equals":
            if operator == "divide": calcvalue =  savedvalue /calcvalue
            if operator == "times": calcvalue =  savedvalue * calcvalue
            if operator == "minus": calcvalue =  savedvalue - calcvalue
            if operator == "plus": calcvalue =  savedvalue + calcvalue
    
        if pressed == "clear":
            calcvalue = 0
    
        displayupdate()
        canvas.update()
    
    #Setup the display
    def displayupdate():
        canvas.create_rectangle(10, 10, 370, 40, fill="white", outline="black")
        canvas.create_text(350, 25,  text=calcvalue,font="Times 20  bold",anchor=E)
    
    #Setup the canvas/window
    def main():
        global window
        global tkinter
        global canvas
        window = Tk()
        window.title("BIG Calculator")
        Button(window, text="Quit",  width=5, command=quit).pack()
        canvas = Canvas(window, width= 380, height=330, bg = 'black')
        canvas.bind("<Button-1>", buttonclick)
    
    #Add the 1 2 3 4 5 6 7 8 9 0 buttons
        canvas.create_rectangle(10, 50, 100, 110, fill="white", outline="black")
        canvas.create_text(45, 80,  text="7",font="Times 30  bold")
    
        canvas.create_rectangle(10, 120, 100, 180, fill="white", outline="black")
        canvas.create_text(45, 150,  text="4",font="Times 30  bold")
    
        canvas.create_rectangle(10, 190, 100, 250, fill="white", outline="black")
        canvas.create_text(45, 220,  text="1",font="Times 30  bold")
    
        canvas.create_rectangle(10, 260, 100, 320, fill="white", outline="black")
        canvas.create_text(45, 290,  text="0",font="Times 30  bold")
    
        canvas.create_rectangle(80, 50, 170, 110, fill="white", outline="black")
        canvas.create_text(115, 80,  text="8",font="Times 30  bold")
    
        canvas.create_rectangle(80, 120, 170, 180, fill="white", outline="black")
        canvas.create_text(115, 150,  text="5",font="Times 30  bold")
    
        canvas.create_rectangle(80, 190, 170, 250, fill="white", outline="black")
        canvas.create_text(115, 220,  text="2",font="Times 30  bold")
    
        canvas.create_rectangle(150, 50, 240, 110, fill="white", outline="black")
        canvas.create_text(185, 80,  text="9",font="Times 30  bold")
    
        canvas.create_rectangle(150, 120, 240, 180, fill="white", outline="black")
        canvas.create_text(185, 150,  text="6",font="Times 30  bold")
    
        canvas.create_rectangle(150, 190, 240, 250, fill="white", outline="black")
        canvas.create_text(185, 220,  text="3",font="Times 30  bold")
    
    #SHow the = c + - x / buttons
        canvas.create_rectangle(80, 260, 170, 320, fill="white", outline="black")
        canvas.create_text(115, 290,  text="=",font="Times 30  bold")
    
        canvas.create_rectangle(150, 260, 240, 320, fill="white", outline="black")
        canvas.create_text(185, 290,  text="C",font="Times 30  bold")
    
        canvas.create_rectangle(220, 50, 370, 110, fill="white", outline="black")
        canvas.create_text(295, 80,  text="Divide",font="Times 30  bold")
    
        canvas.create_rectangle(220, 120, 370, 180, fill="white", outline="black")
        canvas.create_text(295, 150,  text="Times",font="Times 30  bold")
    
        canvas.create_rectangle(220, 190, 370, 250, fill="white", outline="black")
        canvas.create_text(295, 220,  text="Minus",font="Times 30  bold")
    
        canvas.create_rectangle(220, 260, 370, 320, fill="white", outline="black")
        canvas.create_text(295, 290,  text="Plus",font="Times 30  bold")
    
    #Make the whole calculator work
        canvas.create_rectangle(10, 10, 280, 40, fill="white", outline="black")
        global calcvalue
        calcvalue = 0
        displayupdate()
    
        canvas.pack()
        window.mainloop()
    
    
    
    
    
    
    
    main()
    


Answer: Briefly: you get ValueError because you try to do `int("clear")` in

    
    
    if pressed == "clear": calcvalue = calcvalue - int(pressed) / 10
    

You can do this:

    
    
    if pressed == "clear": calcvalue = int(calcvalue/10.0)
    

Because you work only with integers and you use Python 2.x you can do even
this:

    
    
    if pressed == "clear": calcvalue = calcvalue/10
    

In Python 2.x:

  * `integer/integer` gives always `integer`
  * `float/integer` and `integer/float` gives `float`

In Python 3.x:

  * `integer/integer` gives always `float`
  * `integer//integer` gives always `integer`

* * *

By the way:

You can this:

    
    
    if event.x >10 and event.x <70 ...
    

replace with this:

    
    
    if 10< event.x <70 ...
    

And this:

    
    
    if pressed == 0 or pressed == 1 or pressed == 2 or pressed == 3 or pressed == 4 or pressed == 5 or pressed == 6 or pressed == 7 or pressed == 8 or pressed == 9 :
    
    if pressed == "divide" or pressed == "times" or pressed == "minus" or pressed == "plus" :
    

with this:

    
    
    if pressed in (0, 1, 2, 3, 4, 5, 6, 7, 8, 9):
    
    if pressed in ("divide", "times", "minus", "plus"):
    

It is more readable.

