Python Forum
name 'lblstatus' is not defined when referencing a label - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: name 'lblstatus' is not defined when referencing a label (/thread-36982.html)



name 'lblstatus' is not defined when referencing a label - KatManDEW - Apr-19-2022

I'm trying to create an app that opens a window with a calendar and then after the calendar window is close it will countdown until the selected date and time, while displaying the countdown timer. When I try to change the text of a label so I can display the countdown in the label I get the error "name 'lblstatus' is not defined".

Here's the code;
-------------------------------
import tkinter as tk
from tkinter import ttk
from tkinter import *
from tkcalendar import *
import datetime
import time

class Window(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)        
    
        self.geometry('600x400+550+450')
        self.title('Select Date for sample insertion')
          
# Add the Calendar module
        now = datetime.datetime.now()
        cal = Calendar(self, selectmode = 'day',
               year = int(now.strftime("%Y")), month = int(now.strftime("%m")), day = int(now.strftime("%d")))
        cal.place(x=1, y=111) 
        cal.pack(pady = 20, fill="both", expand=True)
        
        def dwcmd():
            global status
            status = "Status=OK"
            caldate = cal.get_date()# + "18:45:00.000"
            a,b,c = caldate.split('/')
            m = int(min.get())
            h = int(hr.get())
            mytime =datetime.datetime(int(c)+2000, int(a), int(b), h, m, 0)
            timenow = datetime.datetime.now()
            print ("Time now: ", timenow)
            print ("mytime: ", mytime)
            
            while datetime.datetime.now() < mytime:
                print ("Waiting...", datetime.datetime.now())    
                time.sleep(10)

            print ("Time's up!")
            print (status)

            
        def dw2():
            print ("ok")
            
        lblhour = tk.Label(self, text='Hour')
        lblhour.place(x=228,y=237)
        
        # Spinbox
        current_value = tk.StringVar(value=0)
        hr = tk.Spinbox(
            self,
            from_=0,
            to=23,
            width=5,
            #height=10,
            font =50,
            textvariable=current_value,
            state="readonly",
            wrap=True)
        
        lblmin = tk.Label(self, text='Minute')
        lblmin.place(x=214,y=260)
#       #label2.pack(ipadx=10, ipady=10)
        hr.pack()
        
        current_value = tk.StringVar(value=0)
        min = tk.Spinbox(
            self,
            from_=0,
            to=60,
            width=5,
            font =50,
            textvariable=current_value,
            state="readonly",
            wrap=True)        
        min.pack()
       
        ttk.Button(self, text='Countdown', command=dwcmd).pack(expand=True),
       
        buttonclose = ttk.Button(self, text='Close', command=self.destroy).pack(expand=True)

class App(tk.Tk):

    def __init__(self):
        super().__init__()

        self.geometry('600x400+550+450')
        self.title('MCL Water Boil')
               
        myvar = tk.StringVar()
        lblstatus = tk.Label(self, textvariable=myvar)
        lblstatus.place(x=277,y=238)
        myvar.set("not set")
        lblstatus.pack(pady=80)

       # place a button on the root window
        ttk.Button(self,text='Open Calendar', command=self.open_window).pack(expand=True)

        ttk.Button(self,text='Show status', command=self.dwcmd3).pack(expand=True)

    def dwcmd3(App):
        lblstatus.config(text="OK!")

    def open_window(self):
        window = Window(self)
        window.grab_set()
        
if __name__ == "__main__":
    app = App()
    app.mainloop()



RE: name 'lblstatus' is not defined when referencing a label - deanhystad - Apr-19-2022

Please wrap posted code in Python tags to retain indenting.

If you want something to use lblstatus you should make it an instance variable of the class.
class App(tk.Tk):
    def __init__(self):
        super().__init__()
        myvar = tk.StringVar(self, "not set)
        self.lblstatus = tk.Label(self, textvariable=myvar)  # Make it an instance variable
        self.lblstatus.pack(pady=80)
Then you can use it later
    def dwcmd3(App):
        self.lblstatus.config(text="OK!")
But I don't think you need to make lblstatus an instance variable. I think you want to make myvar an instance variable (after you give it a better name).
class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.statusVar = tk.StringVar(self, "not set")
        tk.Label(self, textvariable=self.statusVar).pack(pady=80)
        ttk.Button(self,text='Open Calendar', command=self.open_window).pack(expand=True)
        ttk.Button(self,text='Show status', command=self.dwcmd3).pack(expand=True)

    def dwcmd3(self):
        self.status.set("OK!")

    def open_window(self):
        Window(self).grab_set()
You need to go through all your code and decide what you need to keep and convert those from local variables to instance variables. If you use it in two methods it has to be an instance variable.


RE: name 'lblstatus' is not defined when referencing a label - KatManDEW - Apr-20-2022

(Apr-19-2022, 05:53 PM)deanhystad Wrote: Please wrap posted code in Python tags to retain indenting.

If you want something to use lblstatus you should make it an instance variable of the class.
class App(tk.Tk):
    def __init__(self):
        super().__init__()
        myvar = tk.StringVar(self, "not set)
        self.lblstatus = tk.Label(self, textvariable=myvar)  # Make it an instance variable
        self.lblstatus.pack(pady=80)
Then you can use it later
    def dwcmd3(App):
        self.lblstatus.config(text="OK!")
But I don't think you need to make lblstatus an instance variable. I think you want to make myvar an instance variable (after you give it a better name).
class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.statusVar = tk.StringVar(self, "not set")
        tk.Label(self, textvariable=self.statusVar).pack(pady=80)
        ttk.Button(self,text='Open Calendar', command=self.open_window).pack(expand=True)
        ttk.Button(self,text='Show status', command=self.dwcmd3).pack(expand=True)

    def dwcmd3(self):
        self.status.set("OK!")

    def open_window(self):
        Window(self).grab_set()
You need to go through all your code and decide what you need to keep and convert those from local variables to instance variables. If you use it in two methods it has to be an instance variable.

Wow, thank you so very much for the reply! Very appreciated!

I tried the first two steps and it eliminated the not defined error, but dwcmd3 did nothing. I added a "Print OK" to make sure it was running the command and it was.

I will try the third step, making myvar an instance variable (after you giving it a better name) next.

I am ashamed to say that I don't know what Python tags are, or instance variables. I will research that.

Thank you again!


RE: name 'lblstatus' is not defined when referencing a label - deanhystad - Apr-20-2022

Sorry, I had a typo. Should be:
    def dwcmd3(self):
        self.statusVar.set("OK!")  # Not self.status which doesn't exist



RE: name 'lblstatus' is not defined when referencing a label - KatManDEW - Apr-21-2022

(Apr-20-2022, 01:53 PM)deanhystad Wrote: Sorry, I had a typo. Should be:
    def dwcmd3(self):
        self.statusVar.set("OK!")  # Not self.status which doesn't exist

That works! Thank you! I very much appreciate it.