Python Forum
name 'lblstatus' is not defined when referencing a label
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
name 'lblstatus' is not defined when referencing a label
#1
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()
Larz60+ write Apr-19-2022, 07:23 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags on future posts.
Reply
#2
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.
Reply
#3
(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!
Reply
#4
Sorry, I had a typo. Should be:
    def dwcmd3(self):
        self.statusVar.set("OK!")  # Not self.status which doesn't exist
Reply
#5
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dictionary Referencing nickdavis2017 1 1,613 Nov-20-2021, 06:24 PM
Last Post: deanhystad
  Referencing string names in df to outside variables illmattic 1 1,366 Nov-16-2021, 12:47 PM
Last Post: jefsummers
  Referencing a fixed cell Mark17 2 2,072 Dec-17-2020, 07:14 PM
Last Post: Mark17
Sad need help in referencing a list n00bdev 2 1,855 Nov-01-2020, 12:06 PM
Last Post: buran
  Issue referencing new instance from other class nanok66 3 2,239 Jul-31-2020, 02:07 AM
Last Post: nanok66
  referencing another method in a class Skaperen 6 2,669 Jul-02-2020, 04:30 AM
Last Post: Skaperen
  python library not defined in user defined function johnEmScott 2 3,885 May-30-2020, 04:14 AM
Last Post: DT2000
  Theory behind referencing a dictionary rather than copying it to a list sShadowSerpent 2 2,091 Mar-24-2020, 07:18 PM
Last Post: sShadowSerpent
  "not defined" error in function referencing a class Exsul 2 3,686 Mar-27-2019, 11:59 PM
Last Post: Exsul
  Prevent Variable Referencing Th3Eye 4 2,891 Oct-03-2018, 07:01 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020