If you enter the following code in Python 3, you get the error "btn_click
is not defined", and btn2_click
is not defined in the same way.
Where should I change to work without errors?
import tkinter
# screen creation
tki=tkinter.Tk()
tki.geometry('400x200')# Screen Size Settings
tki.title('Gacha')# Setting the Screen Title
# Create Buttons
btn=tkinter.Button(tki, text='single shot', height=2, width=20, command=btn_click)# Button Settings
btn.place(x=30,y=100)# Button Location
btn2=tkinter.Button(tki, text='10', height=2, width=20, command=btn2_click)# Button Settings
btn2.place(x=230,y=100)# Button Location
defbtn_click():
import tkinter
tki2 = tkinter.Tk()
tki2.geometry ('400x200')
tki2.title('1')
btn3 = tkinter.Button(tki2, text='yes', height=2, width=20)# Button Settings
btn4 = tkinter.Button(tki2, text='no', height=2, width=20)
btn3.place(x=30,y=100)
btn4.place(x=230,y=100)
defbtn2_click():
import tkinter
tki3 = tkinter.Tk()
tki3.geometry ('400x200')
tki3.title('10')
btn5 = tkinter.Button(tki2, text='yes', height=2, width=20)# Button Settings
btn6=tkinter.Button(tki2, text='no', height=2, width=20)
btn5.place(x=30,y=100)
btn6.place(x=230,y=100)
# Display screen as it
tki.mainloop()
It's not limited to Python, it's usually an error when referenced before using variables (especially in dynamic typing languages).
Defining it right there, but don't look backward when you run it
result=hed+v*3#👈Error because it deals with undefined variables
had = 1
v = 2
print(result)
Also, it is the same even if it is not a variable but not a variable.Don't browse before defining a function
The question btn_click
is undefined at the time of "Create Button" execution
result=fn#👈Error handling function fn before definition
However, the situation is slightly different in the following cases
v
is not an errorhed
, v
, so if undefined at that time, errorIn this case, the order is fn
definition -->hed
, v
definition --> subsequently calling fn()
, so no errors occur
本来 Originally hed
, v
should be given by argument, but this structure for reference description
deffn():
result=hed+v*3
return result
had = 'Ah'
v=' is '
print(fn())
had = 'Yes'
v='To'
print(fn())
As noted, calling btn_click
on a line prior to defining btn_click
results in NameError
as shown below.
Exception has occurred:NameError
name 'btn_click' is not defined
By the way, btn5=tkinter.Button(tki2,text='yes',height=2,width=20)
also generates NameError
.
This is because tki2
is not defined within the scope of the btn2_click
function.
Make sure that tki3
is typed incorrectly.
We verified normal operation by rewriting it like a sample code.
sample code
import tkinter
# screen creation
tki=tkinter.Tk()
tki.geometry('400x200')# Screen Size Settings
tki.title('Gacha')# Setting the Screen Title
defbtn_click():
import tkinter
tki2 = tkinter.Tk()
tki2.geometry ('400x200')
tki2.title('1')
btn3 = tkinter.Button(tki2, text='yes', height=2, width=20)# Button Settings
btn4 = tkinter.Button(tki2, text='no', height=2, width=20)
btn3.place(x=30,y=100)
btn4.place(x=230,y=100)
defbtn2_click():
import tkinter
tki3 = tkinter.Tk()
tki3.geometry ('400x200')
tki3.title('10')
btn5=tkinter.Button(tki3,text='yes',height=2,width=20)# Button Settings
btn6=tkinter.Button(tki3, text='no', height=2, width=20)
btn5.place(x=30,y=100)
btn6.place(x=230,y=100)
# Create Buttons
btn=tkinter.Button(tki, text='single shot', height=2, width=20, command=btn_click)# Button Settings
btn.place(x=30,y=100)# Button Location
btn2=tkinter.Button(tki, text='10', height=2, width=20, command=btn2_click)# Button Settings
btn2.place(x=230,y=100)# Button Location
# Display screen as it
tki.mainloop()
© 2025 OneMinuteCode. All rights reserved.