Python Forum
NameError issue with daughter's newb code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: NameError issue with daughter's newb code (/thread-34931.html)



NameError issue with daughter's newb code - MrGonk - Sep-16-2021

My young daughter has started to learn Python and although I'm from a MF programming background, I'm not much help to her! We have tried moving the colour_name & colour_code before and after the 'def' but that hasn't helped. Thanks in anticipation... Version 3.9.7

from tkinter import *

root = Tk()
root.geometry("200x400")
root.title("Rainbow")

codes = ['#FF0000', '#FFA500', '#FFFF00', '#008800', '#0000FF', '#000080', '#4B0082']
colours = ["red", "orange", "yellow", "green", "light blue", "blue", "violet"]

def chang_text(index):
    global colours, codes
    colour_name.config(text=colours[index])
    colour_code.config(text=codes[index])

colour_name = Label(font='Arial, 14', bg='#ffffff', width=20, height=2, command=chang_text(0))
colour_name.pack(padx=5)
colour_code = Label(font='Arial, 14', bg='#ffffff', width=20, height=2)
colour_code.pack(padx=5)

root.mainloop()
Error:
Traceback (most recent call last): File "C:\Users\mrgonk\OneDrive\Desktop\STEFANYA Питон\Python Stuff\mr gonk.py", line 16, in <module> colour_name = Label(font='Arial, 14', bg='#ffffff', width=20, height=2, command=chang_text(0)) File "C:\Users\mrgonk\OneDrive\Desktop\STEFANYA Питон\Python Stuff\mr gonk.py", line 13, in chang_text colour_name.config(text=colours[index]) NameError: name 'colour_name' is not defined



RE: NameError issue with daughter's newb code - perfringo - Sep-16-2021

There is difference between reference and assignment:

When you reference a variable in an expression, the Python interpreter will traverse the scope to resolve the reference in following order:

- current function’s scope
- any enclosing scopes (like containing functions)
- scope of the module that contains the code (global scope)
- built-in scope (that contains functions like int and abs)

If Python doesn't find defined variable with the referenced name, then a NameError exception is raised.

Assigning a value to a variable works differently. If the variable is already defined in the current scope, then it will just take on the new value. If the variable doesn’t exist in the current scope, then Python treats the assignment as a variable definition. The scope of the newly defined variable is the function that contains the assignment.

As in function scope there is no colour_name defined, assigning value to it's attribute raises NameError.

While at it, I recommend to read What are the “best practices” for using import in a module?


RE: NameError issue with daughter's newb code - BashBedlam - Sep-16-2021

Your problem is coming from line 15. You probably want a button instead of a text widget since you're trying to give it a command to execute. That being said, in tkinter you can't assign a function to a command that requires arguments so you may want to make the variable index a global. Also, you can't include parentheses when assigning a function to a command. Here is the working code but you may want to separate the button and the first text widget.

from tkinter import *
 
root = Tk()
root.geometry("200x400")
root.title("Rainbow")
 
codes = ['#FF0000', '#FFA500', '#FFFF00', '#008800', '#0000FF', '#000080', '#4B0082']
colours = ["red", "orange", "yellow", "green", "light blue", "blue", "violet"]
index = 0

def chang_text():
    colour_name.config(text=colours[index])
    colour_code.config(text=codes[index])
 
colour_name = Button(font='Arial, 14', bg='#ffffff', width=20, height=2, command=chang_text)
colour_name.pack(padx=5)
colour_code = Label(font='Arial, 14', bg='#ffffff', width=20, height=2)
colour_code.pack(padx=5)
 
root.mainloop()