The error occurs in the btn.configure(text="stop")
part of the code below.
I would appreciate it if you could tell me how to deal with it.
Thank you for your cooperation.
error messages:
An exception has occurred: NameError
name 'btn' is not defined
File "I hid the path", in start and stop
btn.configure(text="stop")
File "Hidden Path", line 44, in<module>
btn=tk.Button(text="start", font=("MS Gothic Classic", "150", "normal"), command=startandstop())
source code:
import tkinter ask
from time import sleep
h = 0
m = 0
s = 0
mode = 2
def time():
if mode == 1:
ifs == 59:
sleep(1)
s = 0
if m == 59:
sleep(1)
m = 0
if h == 24:
sleep(1)
h = 1
else:
h = h+1
else:
m = m+1
else:
s = s+1
timelabel.configure(text=h-m-s)
root.after(100, time)
def startandstop():
global mode
if mode == 1:
mode = 2
btn.configure(text="start")
else:
mode = 1
btn.configure(text="stop")
root=tk.Tk()
root.geometry ("300x300")
timelabel=tk.Label(text=h-m-s, font=("MS Gothic Classic", "150", "normal"))
timelabel.place (x=280, y=400)
btn=tk.Button(text="start", font=("MS Gothic Classic", "150", "normal"), command=startandstop())
btn.place (x=280, y=400)
root.after(100, time)
root.mainloop()
command=startandstop
instead of command=startandstop()
.
name 'btn' is not defined
They say btn doesn't exist
As a countermeasure, @merino's method is correct, but the causes and reasons are as follows:
The following lines of this function are causing the error:
def startandstop():
global mode
if mode == 1:
mode = 2
btn.configure(text="start")
else:
mode = 1
btn.configure(text="stop")#### This line shows the error message
This line is responsible for this.
btn=tk.Button(text="start", font=("MS Gothic Classic", "150", "normal"), command=startandstop())
The reason is that the parameter command=startandstop()
has a ()
at the end of the command=startandstop
and like @merino's answer, command ststartandstop
will solve the problem.
(However, the size of the parts to be displayed and the setting of the stopwatch display are strange, so it doesn't seem to work properly.)
btn=tk.Button(text="start", font=("MS Gothic Classic", "150", "normal"), command=startandstop)
If you run Python at the command prompt, the call stack will also be displayed when an error occurs, so it will be easy to understand where and what is going.
C:\Develop\Python>py soqa0813.py
Traceback (most recent call last):
File "C:\Develop\Python\soqa0813.py", line 44, in<module>
btn=tk.Button(text="start", font=("MS Gothic Classic", "150", "normal"), command=startandstop())
File "C:\Develop\Python\soqa0813.py", line 36, in startandstop
btn.configure(text="stop")
NameError: name 'btn' is not defined. Did you mean: 'bin'?
The reason is that when In other words, we call Therefore, when The structure and function of the program as a stopwatch are slightly different, but this article may be helpful.command=startandstop
, the command
parameter is set to the function object defined in def startandstop():
by mistake, but command=startandstop()
is set to parameter.
def startandstop():
to set the parameters for this line btn=tk.Button(text="start", font=("MS Gothic Classic", "150", "normal"), command=startandstop())
in which we call the btn.configure(text>
"I'm not here.
def startandstop():
is called to set the command
parameter of tk.Button(...)
to define and initialize btn
, the error is undefined.
Python 3 Tkinter Mini Stopwatch Timer Alarm Clock Script GUI Desktop App
© 2024 OneMinuteCode. All rights reserved.