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
#5
There is no currentCalcInput in the global scope. There is an is Gui.__init__(), but global looks for variables in the global (module) scope, not the enclosed scope. You should either make currentCalcInput a mutable type, or pass it as an argument to the function. Here I make it mutable by putting the string in a list. buttonPress() changes the value in the list. It does not assign a new value to currentCalcInput.
from tkinter import *
 
class Gui(Tk):
 
    def __init__(self):
        super().__init__()
        currentCalcInput = [""]
 
        def buttonPress(key):
            currentCalcInput[0] += key
            print(currentCalcInput)
 
        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(self, 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()
        self.mainloop()

Gui()
I don't like this design pattern at all. You are using an obscure and confusing (to me) enclosing scope behavior where you should be using an instance variable and method.
import tkinter as tk
 
class Gui(tk.Tk):
    def __init__(self):
        super().__init__()
        self.currentCalcInput = ""  # <- Instance variable
        buttons = ("1", "4", "7", "Clear", "2", "5", "8", "0", "3", "6", "9", "=", "+", "-", "*", "/")
        for index, btn in enumerate(buttons):
            tk.Button(self, text=btn, width=6, height=3, command=lambda arg=btn: self.buttonPress(arg)). \
                grid(row=index % 4, column=index // 4)

    def buttonPress(self, key):
        self.currentCalcInput += key  # <- Instance variable
        print(self.currentCalcInput)

Gui().mainloop()
TheTypicalDoge likes this post
Reply


Messages In This Thread
RE: [variable] is not defined error arises despite variable being defined - by deanhystad - Apr-05-2022, 04:55 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 338 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  Commas issue in variable ddahlman 6 503 Apr-05-2024, 03:45 PM
Last Post: deanhystad
  Variable Explorer in spyder driesdep 1 279 Apr-02-2024, 06:50 AM
Last Post: paul18fr
  Mediapipe. Not picking up second variable stevolution2024 1 232 Mar-31-2024, 05:56 PM
Last Post: stevolution2024
Question Variable not defined even though it is CoderMerv 3 348 Mar-28-2024, 02:13 PM
Last Post: Larz60+
  I'm getting a NameError: ...not defined. vonArre 2 355 Mar-24-2024, 10:25 PM
Last Post: vonArre
  optimum chess endgame with D=3 pieces doesn't give an exact moves_to_mate variable max22 1 305 Mar-21-2024, 09:31 PM
Last Post: max22
  unbounded variable akbarza 3 521 Feb-07-2024, 03:51 PM
Last Post: deanhystad
  Variable for the value element in the index function?? Learner1 8 710 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable definitions inside loop / could be better? gugarciap 2 477 Jan-09-2024, 11:11 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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