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
#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


Messages In This Thread
RE: NameError issue with daughter's newb code - by BashBedlam - Sep-16-2021, 01:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python newb need help Fictile 1 343 Apr-02-2024, 03:28 AM
Last Post: buran
  Updating Code And Having Issue With Keys Xileron 8 1,368 May-25-2023, 11:14 PM
Last Post: DigiGod
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 2,960 Jul-26-2021, 04:36 PM
Last Post: snippsat
  Calculator code issue using list kirt6405 4 2,400 Jun-11-2021, 10:13 PM
Last Post: topfox
  code not working, NameError: name 's' is not defined ridgerunnersjw 4 3,956 Oct-05-2020, 07:03 PM
Last Post: buran
  Issue with code for auto checkout nqk28703 2 2,237 Nov-01-2019, 09:33 AM
Last Post: nqk28703
  NameError: NameError: global name 'BPLInstruction' is not defined colt 7 4,570 Oct-27-2019, 07:49 AM
Last Post: Larz60+
  Simple newb string question Involute 2 2,314 Sep-08-2019, 12:50 AM
Last Post: Involute
  NameError: name 'display' is not defined when running code on power bi beginner1 2 19,413 Jul-24-2019, 11:03 AM
Last Post: beginner1
  please help this newb install pygame iofhua 7 6,168 May-15-2019, 01:09 PM
Last Post: buran

Forum Jump:

User Panel Messages

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