Trying to update a label in tkinter class
Question: Im new to python and this forum. I tried searching a solution for the errors i
get in my code but this only made me try a lot of different stuff, still i
couldn't get my label updated.

Now i think it has something to do with my inability tot properly define a
variable at the class level. But to be honest, i have a hard time grasping the
concept of class, self, global, local etc. So this may well add to my
confusion.

I cut the code that gives me the errors from my larger program to post here. I
hope some of you can enlighten me what i do wrong. Sorry in advance for
breaking python code etiquette, i'm still learning.

Some of the things i tried i commented out in the posted code...The code i
posted here gives the following error:

    
    
    Traceback (most recent call last):
      File "C:/Users/User/Desktop/smaller example.py", line 35, in <module>
        app = cbgui(root)
      File "C:/Users/User/Desktop/smaller example.py", line 8, in __init__
        self.initUI()
      File "C:/Users/User/Desktop/smaller example.py", line 23, in initUI
        labelupdate = Tkinter.Label(frame,  width = 50, textvariable = self.var)
    AttributeError: cbgui instance has no attribute 'var'
    

My code:

    
    
    import sys, Tkinter, tkFileDialog
    
    
    class cbgui(Tkinter.Frame):
        def __init__(self, master):
            Tkinter.Frame.__init__(self,master)
            self.master = master
            self.initUI()
            self.var = Tkinter.StringVar()
            #self.var = Tkinter.StringVar()
            self.var.set = "hello"
    
        def UpdateLabel(self):
            #var.set = "bye"
            self.var.set = "bye"
    
        def initUI(self):
            self.master.title("a small update test")
            frame = Tkinter.Frame(self, relief=Tkinter.RAISED, borderwidth = 1)
            frame.pack(fill = Tkinter.BOTH, expand = 1)
            self.pack(fill = Tkinter.BOTH, expan = 1)
    
            #labelupdate = Tkinter.Label(frame,  width = 50, textvariable = var)
            labelupdate = Tkinter.Label(frame,  width = 50, textvariable = self.var)
            #labelupdate = Tkinter.Label(frame,  width = 50, text = "hello")
            labelupdate.grid(row=3, column=2)
    
            labelspace = Tkinter.Label(frame, width = 1)
            labelspace.grid(row=3, column=3)
    
            UpdateButton = Tkinter.Button(frame, text="Update label",  command = self.UpdateLabel)
            UpdateButton.grid(row=3, column=4)
    
    root = Tkinter.Tk()
    root.geometry("700x300+10+10")
    app = cbgui(root)
    Tkinter.mainloop()
    


Answer: You need to create `self.var` _before_ you call `self.initUI` since `initUI`
uses `self.var`

    
    
    def __init__(self, master):
        Tkinter.Frame.__init__(self,master)
        self.master = master
        self.var = Tkinter.StringVar()
        #self.var = Tkinter.StringVar()
        self.var.set("hello")
        self.initUI()
    

