Note that there are some explanatory texts on larger screens.

plurals
  1. POMake clear button backspace by one in python tkinter calculator
    primarykey
    data
    text
    <p>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.</p> <p>Any help greatly appreciated.</p> <pre><code>#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 &gt;10 and event.x &lt;70 and event.y &gt; 50 and event.y &lt; 110 : pressed = 7 if event.x &gt;10 and event.x &lt;70 and event.y &gt; 120 and event.y &lt; 180 : pressed = 4 if event.x &gt;10 and event.x &lt;70 and event.y &gt; 190 and event.y &lt; 250 : pressed = 1 if event.x &gt;10 and event.x &lt;70 and event.y &gt; 260 and event.y &lt; 320 : pressed = 0 if event.x &gt;80 and event.x &lt;140 and event.y &gt; 50 and event.y &lt; 110 : pressed = 8 if event.x &gt;80 and event.x &lt;140 and event.y &gt; 120 and event.y &lt; 180 : pressed = 5 if event.x &gt;80 and event.x &lt;140 and event.y &gt; 190 and event.y &lt; 250 : pressed = 2 if event.x &gt;150 and event.x &lt;210 and event.y &gt; 50 and event.y &lt; 110 : pressed = 9 if event.x &gt;150 and event.x &lt;210 and event.y &gt; 120 and event.y &lt; 180 : pressed = 6 if event.x &gt;150 and event.x &lt;210 and event.y &gt; 190 and event.y &lt; 250 : pressed = 3 if event.x &gt;80 and event.x &lt;140 and event.y &gt; 260 and event.y &lt; 320 : pressed = "equals" if event.x &gt;150 and event.x &lt;210 and event.y &gt; 260 and event.y &lt; 320 : pressed = "clear" if event.x &gt;220 and event.x &lt;280 and event.y &gt; 50 and event.y &lt; 110 : pressed = "divide" if event.x &gt;220 and event.x &lt;280 and event.y &gt; 120 and event.y &lt; 180 : pressed = "times" if event.x &gt;220 and event.x &lt;280 and event.y &gt; 190 and event.y &lt; 250 : pressed = "minus" if event.x &gt;220 and event.x &lt;280 and event.y &gt; 260 and event.y &lt; 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("&lt;Button-1&gt;", 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() </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload