Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert Python Code to Use Buttons instead
    primarykey
    data
    text
    <p>I have tried but I am not sure how to make this code work using buttons instead of the canvas. Its for a calculator using tkinter. I need to make this work using Buttons but everything i have tried has failed. If someone could tell me how to do it or even do the whole thing that would be much appreciated. I am new to this language and its confusing me. Thanks</p> <pre><code>from Tkinter import * def quit(): window.destroy() 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() def displayupdate(): canvas.create_rectangle(10, 10, 280, 40, fill="white", outline="black") canvas.create_text(260, 25, text=calcvalue,font="Times 20 bold",anchor=E) def main(): global window global tkinter global canvas window = Tk() window.title("Simple Calculator") Button(window, text="Quit", width=5, command=quit).pack() canvas = Canvas(window, width= 290, height=330, bg = 'beige') canvas.bind("&lt;Button-1&gt;", buttonclick) #Add the numbers canvas.create_rectangle(10, 50, 50, 110, fill="yellow", outline="black") canvas.create_text(40, 80, text="7",font="Times 30 bold") canvas.create_rectangle(10, 120, 70, 180, fill="yellow", outline="black") canvas.create_text(40, 150, text="4",font="Times 30 bold") canvas.create_rectangle(10, 190, 70, 250, fill="yellow", outline="black") canvas.create_text(40, 220, text="1",font="Times 30 bold") canvas.create_rectangle(10, 260, 70, 320, fill="yellow", outline="black") canvas.create_text(40, 290, text="0",font="Times 30 bold") canvas.create_rectangle(80, 50, 140, 110, fill="yellow", outline="black") canvas.create_text(110, 80, text="8",font="Times 30 bold") canvas.create_rectangle(80, 120, 140, 180, fill="yellow", outline="black") canvas.create_text(110, 150, text="5",font="Times 30 bold") canvas.create_rectangle(80, 190, 140, 250, fill="yellow", outline="black") canvas.create_text(110, 220, text="2",font="Times 30 bold") canvas.create_rectangle(150, 50, 210, 110, fill="yellow", outline="black") canvas.create_text(180, 80, text="9",font="Times 30 bold") canvas.create_rectangle(150, 120, 210, 180, fill="yellow", outline="black") canvas.create_text(180, 150, text="6",font="Times 30 bold") canvas.create_rectangle(150, 190, 210, 250, fill="yellow", outline="black") canvas.create_text(180, 220, text="3",font="Times 30 bold") #Add the operators canvas.create_rectangle(80, 260, 140, 320, fill="green", outline="black") canvas.create_text(110, 290, text="=",font="Times 30 bold") canvas.create_rectangle(150, 260, 210, 320, fill="green", outline="black") canvas.create_text(180, 290, text="C",font="Times 30 bold") canvas.create_rectangle(220, 50, 280, 110, fill="pink", outline="black") canvas.create_text(250, 80, text="/",font="Times 30 bold") canvas.create_rectangle(220, 120, 280, 180, fill="pink", outline="black") canvas.create_text(250, 150, text="*",font="Times 30 bold") canvas.create_rectangle(220, 190, 280, 250, fill="pink", outline="black") canvas.create_text(250, 220, text="-",font="Times 30 bold") canvas.create_rectangle(220, 260, 280, 320, fill="pink", outline="black") canvas.create_text(250, 290, text="+",font="Times 30 bold") #Setup the display 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.
 

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