Python Forum
NameError issue with daughter's newb code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError issue with daughter's newb code
#1
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
Reply
#2
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?
MrGonk likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
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()
MrGonk likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IBKR Purchasing code issue compuman145 0 1,454 Sep-23-2025, 09:08 AM
Last Post: compuman145
  Python newb need help Fictile 1 1,310 Apr-02-2024, 03:28 AM
Last Post: buran
  Updating Code And Having Issue With Keys Xileron 8 5,086 May-25-2023, 11:14 PM
Last Post: DigiGod
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 4,369 Jul-26-2021, 04:36 PM
Last Post: snippsat
  Calculator code issue using list kirt6405 4 4,004 Jun-11-2021, 10:13 PM
Last Post: topfox
  code not working, NameError: name 's' is not defined ridgerunnersjw 4 7,125 Oct-05-2020, 07:03 PM
Last Post: buran
  Issue with code for auto checkout nqk28703 2 3,592 Nov-01-2019, 09:33 AM
Last Post: nqk28703
  NameError: NameError: global name 'BPLInstruction' is not defined colt 7 7,056 Oct-27-2019, 07:49 AM
Last Post: Larz60+
  Simple newb string question Involute 2 3,538 Sep-08-2019, 12:50 AM
Last Post: Involute
  NameError: name 'display' is not defined when running code on power bi beginner1 2 23,868 Jul-24-2019, 11:03 AM
Last Post: beginner1

Forum Jump:

User Panel Messages

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