1

im getting this name error for my button. I cant figure out why. Im using tkinter and I think Ive imported all the ne

#this the sound shart 


    start_game = Button (text_box,command=start_game_pressed, fg='black',text = """                                               
    丂七闩尺七
    """)

start_game.place(x=450, y=400)


def start_game_pressed (self):
    print("sus")


#subprocess.call(["afplay", "soundscrate-not-done2.wav"]) 
#text and textbox configuration 

text_box.pack(expand=True)
text_box.insert('end', titletext,start_game )
text_box.config(state = 'disabled')
text_box.config(bg='black')
text_box.config(fg='white')
text_box.place(x=150, y=125)

gui.mainloop()

this is the error message

 File "/Users/odinschaefer/Desktop/torture my/main.py", line 43, in <module>
    start_game = Button (text_box,command=start_game_pressed, fg='black',text = """                                                 
NameError: name 'start_game_pressed' is not defined

help pls.

1
  • 3
    start_game_pressed hadn't been defined yet at the time you tried to use it. Move the function definition up above the creation of the Button. Commented Oct 27, 2021 at 20:44

2 Answers 2

2

You need to define the function before you try to reference it.

def start_game_pressed (self):
    print("sus")

start_game = Button (text_box,command=start_game_pressed, fg='black',text = """                                               
丂七闩尺七
""")
Sign up to request clarification or add additional context in comments.

1 Comment

It looks like the function is inside a class, so it may be command=self.start_game_pressed instead.
0

You need to write your function before referencing it in the button. Something like this:

# Notice that here, the function is 
# defined before the button is.

def start_game_pressed(self):
    print(“sus”)
start_game = Button(text_box,command=start_game_pressed, 
    fg='black',text = "丂七闩尺七")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.