0

link to code in pastebin I'm new to Python, and I attempted to create a calculator application. I'm using the Appjar library for my calculator GUI. The calculator works fine, but the appjar label "bar" will not update, despite changes in the variable accumulator. I have attempted to add loops, appjar's .after() function, and messing with appjar's automatic updating system, but I cannot seem to fix the problem.

#define variables
error=''
accumulator = 0
mem = 0
op = ''
 
#import GUI
from appJar import gui
app=gui("Grid Demo", "500x600")
app.setSticky("news")
app.setExpand("both")
app.setFont(20)
 
       
# Do the math will take the accumulator and memory registers and perform the prevailing operation on them.
# It returns the result, as an integer
def doTheMath(a,b,o):
    if o == "add":
        return a + b
    if o == "sub":
        return b - a
    if o == "mul":
        return a * b
    if o == "div":
        return b/a
 
    #press function defines the output of each button.
def press(button):
    global accumulator
    global mem
    global op
 
    print ("A button was pressed: " + button)
    if button == "C":
        accumulator = 0
        op = ""
        mem = 0
    elif button == "=":
        accumulator = doTheMath(accumulator,mem,str(op))
        mem = 0
        op = ""
    elif button == "+":
        op = "add"
        mem = accumulator
        accumulator = 0
    elif button == "-":
        op = "sub"
        mem = accumulator
        accumulator = 0
    elif button == "x":
        op = "mul"
        mem = accumulator
        accumulator = 0
    elif button == "÷":
        op = "div"
        mem = accumulator
        accumulator = 0
    else:
        accumulator = accumulator * 10 + int(button)
    print ("Acc: " + str(accumulator) + ", op: " + op + ", mem: " + str(mem))
    app.go()
   
       
    #define widgets in GUI
app.addLabel("bar", error+str(accumulator), 0, 0, 3)
app.addButtons(["1"], press, 3, 0)
app.addButtons(["2"], press, 3, 1)
app.addButtons(["3"], press, 3, 2)
app.addButtons(["4"], press, 2, 0)
app.addButtons(["5"], press, 2, 1)
app.addButtons(["6"], press, 2, 2)
app.addButtons(["7"], press, 1, 0)
app.addButtons(["8"], press, 1, 1)
app.addButtons(["9"], press, 1, 2)
app.addButtons(["0"], press, 4, 1)
app.addButtons(["+"], press, 3, 3)
app.addButtons(["-"], press, 4, 3)
app.addButtons(["x"], press, 2, 3)
app.addButtons(["÷"], press, 1, 3)
app.addButtons(["="], press, 4, 2)
app.addButtons(["C"], press, 4, 0)
app.go()
  • 1
    Assigning a new value to a Python variable doesn't magically cause all code that used a previous value of that variable to be reevaluated. Now, appJar is built on top of Tkinter, which does have Var objects that can trigger widget changes when their value is updated - but it doesn't look like appJar exposes that ability. So you will need to explicitly call `.setLabel()` every time `accumulator` (or `error`) changes. – jasonharper Jul 15 '20 at 03:49
  • I made a function that runs `.setlable()`: `def changeLabel(btn): app.setLabel("bar", error+str(accumulator)) `, but I cant figure out how to call the function, `else: accumulator = accumulator * 10 + int(button) changeLabel` – Loren Meehan Jul 15 '20 at 04:08
  • 1
    `changeLabel` merely references the function. `changeLabel()` would actually call it. You should remove the `btn` parameter from the definition, as this function doesn't need any further information. – jasonharper Jul 15 '20 at 04:16
  • Thanks! I'll put an answer and close the question. – Loren Meehan Jul 15 '20 at 05:10

1 Answers1

0

Appjar needs .setlable() to be referenced anytime any variable within "bar" changes, I made a function called changeLable(),

def changeLabel():  
    app.setLabel("bar", error+str(accumulator))

That when referenced at the end of the set of if statements, changes the label "bar", to error+str(accumulator.
Credit to jasonharper.