Sep-16-2021, 12:35 PM
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