I have a lot more code than I'm going to post here, but I am posting the relevant parts.
What I have is a Calculator program. Here's the code:
num1 = None
num2 = None
oper = None
def calculate(num1, num2, oper):
print("calculate() called")
if(oper=="+"):
answ = Decimal(num1) + Decimal(num2)
return answ
elif(oper=="-"):
pass
elif(oper=="*"):
pass
elif(oper=="/"):
pass
elif(oper=="^"):
pass
elif(oper=="%"):
pass
else:
pass
def addPressedF():
print("addPressedF() called")
num1 = dfSV.get()
print(num1)
oper = "+"
root.title("Calculator - '+'")
dfSV.set("")
def equPressedF(num1, num2):
print("equPressedF() called")
num2 = dfSV.get()
print(num1)
print(num2)
dfSV.set(calculate(num1, num2, oper))
When someone presses a number button (say, '1') it takes the numbers in the Entry and
adds the number to the end of it (so, entry holds '432', after button pressed, '4321').
After I type three numbers (125) and press '+' it says num1 = 125.
After a couple more numbers (75) and press '=' it says that num1 = None, num2 = 75.
And the Calculate function displays 'None' in the Entry.
I've defined the 'oper' variable in functions the same way as these work.
If I remove the 'var = None' lines then it says that "global name 'num1' is not defined."
So, what am I doing wrong? I looked into functions (online, daniweb) and haven't found
it yet.
- WolfShield