Python Forum
[variable] is not defined error arises despite variable being defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[variable] is not defined error arises despite variable being defined
#1
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.

        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())
Reply


Messages In This Thread
[variable] is not defined error arises despite variable being defined - by TheTypicalDoge - Apr-05-2022, 02:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to get variable Azdaghost 3 501 Apr-23-2025, 07:43 PM
Last Post: deanhystad
  I trying to automate the Variable Logon button using the python code but I couldn't surendrasamudrala 0 347 Mar-07-2025, 05:02 AM
Last Post: surendrasamudrala
  not able to call the variable inside the if/elif function mareeswaran 3 674 Feb-09-2025, 04:27 PM
Last Post: mareeswaran
  Regarding Defined Functions Hudjefa 1 787 Nov-05-2024, 04:59 AM
Last Post: Gribouillis
  creating arbitrary local variable names Skaperen 9 2,035 Sep-07-2024, 12:12 AM
Last Post: Skaperen
  Variable Substitution call keys Bobbee 15 3,060 Aug-28-2024, 01:52 PM
Last Post: Bobbee
  NameError: name 'pi' is not defined katebishop 2 2,815 Jul-15-2024, 05:48 AM
Last Post: Pedroski55
  how solve: local variable referenced before assignment ? trix 5 1,899 Jun-15-2024, 07:15 PM
Last Post: trix
  Variable being erased inside of if statement deusablutum 8 2,277 Jun-15-2024, 07:00 PM
Last Post: ndc85430
  Cant contain variable in regex robertkwild 3 1,218 Jun-12-2024, 11:50 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020