The behavior of the if statement in the function is strange in the tkinter of python.

Asked 2 years ago, Updated 2 years ago, 112 views

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()

python tkinter

2022-09-30 19:47

3 Answers

command=startandstop instead of command=startandstop().


2022-09-30 19:47

name 'btn' is not defined

They say btn doesn't exist


2022-09-30 19:47

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 command=startandstop, the command parameter is set to the function object defined in def startandstop(): by mistake, but command=startandstop() is set to parameter.

In other words, we call 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.

Therefore, when def startandstop(): is called to set the command parameter of tk.Button(...) to define and initialize btn, the error is undefined.

The structure and function of the program as a stopwatch are slightly different, but this article may be helpful.
Python 3 Tkinter Mini Stopwatch Timer Alarm Clock Script GUI Desktop App


2022-09-30 19:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.