Advertisement
Loren_Meehan

Calculator

Jul 14th, 2020
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.11 KB | None | 0 0
  1. #define variables
  2. error=''
  3. accumulator = 0
  4. mem = 0
  5. op = ''
  6.  
  7. #import GUI
  8. from appJar import gui
  9. app=gui("Grid Demo", "500x600")
  10. app.setSticky("news")
  11. app.setExpand("both")
  12. app.setFont(20)
  13.  
  14.        
  15. # Do the math will take the accumulator and memory registers and perform the prevailing operation on them.
  16. # It returns the result, as an integer
  17. def doTheMath(a,b,o):
  18.     if o == "add":
  19.         return a + b
  20.     if o == "sub":
  21.         return b - a
  22.     if o == "mul":
  23.         return a * b
  24.     if o == "div":
  25.         return b/a
  26.  
  27.     #press function defines the output of each button.
  28. def press(button):
  29.     global accumulator
  30.     global mem
  31.     global op
  32.  
  33.     print ("A button was pressed: " + button)
  34.     if button == "C":
  35.         accumulator = 0
  36.         op = ""
  37.         mem = 0
  38.     elif button == "=":
  39.         accumulator = doTheMath(accumulator,mem,str(op))
  40.         mem = 0
  41.         op = ""
  42.     elif button == "+":
  43.         op = "add"
  44.         mem = accumulator
  45.         accumulator = 0
  46.     elif button == "-":
  47.         op = "sub"
  48.         mem = accumulator
  49.         accumulator = 0
  50.     elif button == "x":
  51.         op = "mul"
  52.         mem = accumulator
  53.         accumulator = 0
  54.     elif button == "รท":
  55.         op = "div"
  56.         mem = accumulator
  57.         accumulator = 0
  58.     else:
  59.         accumulator = accumulator * 10 + int(button)
  60.     print ("Acc: " + str(accumulator) + ", op: " + op + ", mem: " + str(mem))
  61.     app.go()
  62.    
  63.        
  64.     #define widgets in GUI
  65. app.addLabel("bar", error+str(accumulator), 0, 0, 3)
  66. app.addButtons(["1"], press, 3, 0)
  67. app.addButtons(["2"], press, 3, 1)
  68. app.addButtons(["3"], press, 3, 2)
  69. app.addButtons(["4"], press, 2, 0)
  70. app.addButtons(["5"], press, 2, 1)
  71. app.addButtons(["6"], press, 2, 2)
  72. app.addButtons(["7"], press, 1, 0)
  73. app.addButtons(["8"], press, 1, 1)
  74. app.addButtons(["9"], press, 1, 2)
  75. app.addButtons(["0"], press, 4, 1)
  76. app.addButtons(["+"], press, 3, 3)
  77. app.addButtons(["-"], press, 4, 3)
  78. app.addButtons(["x"], press, 2, 3)
  79. app.addButtons(["รท"], press, 1, 3)
  80. app.addButtons(["="], press, 4, 2)
  81. app.addButtons(["C"], press, 4, 0)
  82. app.go()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement