Apr-05-2022, 02:02 AM
(This post was last modified: Apr-05-2022, 03:24 AM by TheTypicalDoge.)
I've come across an error that I'm not sure how to fix. I keep getting an error that 'currentCalcInput' isn't defined even though it's defined before it's used. I'm not sure what's happening here.
To confirm, currentCalcInput is only ever used in that function and no where else. I've also copied this snippet into another file and it works just fine, so I'm assuming it's the context in which its used in. I'm not really sure, so I'm open to help.
To confirm, currentCalcInput is only ever used in that function and no where else. I've also copied this snippet into another file and it works just fine, so I'm assuming it's the context in which its used in. I'm not really sure, so I'm open to help.
currentCalcInput = "" def buttonPress(key): global currentCalcInput currentCalcInput += key
Output:NameError: name 'currentCalcInput' is not defined
[Edit] Here is the full code that doesn't work:from tkinter import * class Gui: def __init__(self, root): currentCalcInput = "" def buttonPress(key): global currentCalcInput currentCalcInput += key def generateButtons(): posOffset = (0, 0) buttonSizes = (6, 3) buttonNames = ["1", "4", "7", "Clear", "2", "5", "8", "0", "3", "6", "9", "=", "+", "-", "*", "/"] buttonNamesIndex = 0 for x in range(4): for y in range(4): Button(root, text=buttonNames[buttonNamesIndex], width=buttonSizes[0], height=buttonSizes[1], command=lambda buttonId=buttonNames[buttonNamesIndex]: buttonPress(buttonId)). \ grid(column=x + posOffset[0], row=y + posOffset[1]) buttonNamesIndex += 1 generateButtons() root.mainloop() guiRoot = Gui(Tk())