Python: How does one use variables defined in main(), and/or use a lists with two classes without errors?
Question: Objective: I have tried using variables in main() and creating a random list
of messages in my code, which have partially worked, but are still coming up
with multiple errors that I don't know how to fix.

Question: Could someone fix my code so the errors are gone, and the program at
least works as intended?

In the main() section of my code I define several buttons, which I think
messes up the GUI of the timer section of my program after the first break
timer. The objective box is supposed to reappear in the window, but it
doesn't. This also may have something to do with the .destroy() command. How
would I fix it so the label and entry widget reappear in the initial window?

How this question is different: I am aware that there is a large number of
questions regarding some of these common errors, but my code is unique, and
thus requires a different post.

Code: (This should be all formatted correctly, pull it into your favorite IDE
and run it to debug)

    
    
    from Tkinter import *
    import time
    import random
    
    
    class PopUp():
        def __init__(self):
            top = self.top = Toplevel()
            self.inp = StringVar()
            self.nth = 0
            top.geometry("240x135+25+300")
            Label(top, text="Suggestion:").pack(side=TOP)
            self.message()
            Label(top, textvariable=self.inp)
    
        def message(self):
            self.inp.set(random.choice(self.mess))
    
        mess = ['Nice Job! Go take a walk outside!',
                'Nice Job! Wiggle your toes, and get back on it!',
                'Nice Job! Get up and walk around!',
                'Nice Job! Go get a drink of water!',
                'Good Work! Go take a walk outside!',
                'Good Work! Wiggle your toes, and get back on it!',
                'Good Work! Get up and walk around!',
                'Good Work! Go get a drink of water!']
    
    
    class Timer(Frame):
        def __init__(self, stb, st, rb, qb, parent=None, **kw):
            Frame.__init__(self, parent, background="white")
            self.parent = parent
            self.initUI()
            self.inp = None
            self.stb = stb
            self.st = st
            self.rb = rb
            self.qb = qb
            self.timer1 = 9
            self.checkTimer1 = 1
            self.timer1Run = 0
            self.tempObj = 0
            self.entdat = StringVar()
            self.timestr = StringVar()
            self.makeTimer()
            self.makeWidgets()
    
       def initUI(self):
            self.parent.title("Timer")
    
        def makeWidgets(self):
            minutes = int(self.timer1/60)
            seconds = int(self.timer1 - minutes*60.0)
            self.timestr.set('%02d:%02d' % (minutes, seconds))
            self.ol = Label(text="Objective:")
            self.ol.pack(fill=X, expand=NO, pady=2, padx=2, side=TOP)
            self.ew = Entry(textvariable=self.entdat)
            self.ew.pack(fill=X, expand=NO, pady=2, padx=2, side=TOP)
            self.b = Button(text="OK", command=self.clicked)
            self.b.pack(side=TOP)
    
    
        def makeTimer(self):
            self.l = Label(self, textvariable=self.timestr)
            self.l.pack(fill=X, expand=NO, pady=2, padx=2)
    
        def makeButtons(self, st, stb, rb, qb):
            self.destroy()
    
        def updateTime(self):
            self.elapsed = time.time() - self.startTime
            self.curtim1 = self.timer1 - self.elapsed
            if self.curtim1 <= 0:
                self.stop()
                self.checkValue()
    
            else:
                minutes = int(self.curtim1/60)
                seconds = int(self.curtim1 - minutes*60.0)
                self.timestr.set('%02d:%02d' % (minutes, seconds))
                self.timer = self.after(1, self.updateTime)
    
        def checkValue(self):
            if self.checkTimer1 == 1:
                self.distroy_Widget()
                self.timer1 = 3
                self.checkTimer1 -= 1
                self.start()
    
            else:
                self.dat.destroy()
                self.makeWidgets()
                self.timer1 = 9
                self.checkTimer1 += 1
                pu = PopUp()
                pu.message()
                self.start()
    
        def start(self):
            if not self.timer1Run:
                self.startTime = time.time()
                self.updateTime()
                self.timer1Run = 1
    
        def stop(self):
             if self.timer1Run:
                 self.after_cancel(self.timer)
                 self.timer1Run = 0
                 self.timer1 = self.curtim1
    
        def reset(self):
            self.timer1 = 900
            minutes = int(self.timer1/60)
            seconds = int(self.timer1 - minutes*60.0)
    
            self.timestr.set('%02d:%02d' % (minutes, seconds))
    
        def clicked(self):
            self.distroy_Widget()
            self.dat = Label(self, textvariable=self.entdat)
            self.dat.pack(side=TOP)
    
        def distroy_Widget(self):
            self.ew.destroy()
            self.ol.destroy()
            self.b.destroy()
    
    
    def main():
        root = Tk()
        root.geometry("240x235+25+50")
        tm = Timer(root, stb, sb, rb, qb)
        tm.pack(side=TOP)
    
        sb = Button(root, text='Start', command=tm.start).pack(side=LEFT)
        stb = Button(root, text='Pause', command=tm.stop).pack(side=LEFT)
        rb = Button(root, text='Reset', command=tm.reset).pack(side=LEFT)
        qb = Button(root, text='Quit', command=root.quit).pack(side=LEFT)
    
        mainloop()
    
    if __name__ == '__main__':
        main()
    


Answer: Sorry, but the code indenting in your question isn't quite right. However--
ignoring that relatively minor issue and the fact that the following modified
version of it doesn't address all your questions--you may still find it useful
because it does get rid of the errors about using variables in`main()`by
making them all attributes of the`Timer`class, the only place they're
referenced, and initializing them in its`__init__()` method--essentialy what
@Mike Housky suggested in [his
comment](http://stackoverflow.com/questions/27872410/python-how-does-one-use-
variables-defined-in-main-and-or-use-a-lists-with-
tw#comment44147355_27872410). I also renamed a few things to be more
consistent.

This should get the GUI running well enough to allow you can deal with the
remaining problems yourself.

    
    
    from Tkinter import *
    import time
    import random
    
    class PopUp():
        def __init__(self):
            top = self.top = Toplevel()
            self.inp = StringVar()
            self.nth = 0
            top.geometry("240x135+25+300")
            Label(top, text="Suggestion:").pack(side=TOP)
            self.message()
            Label(top, textvariable=self.inp)
    
        def message(self):
            self.inp.set(random.choice(self.mess))
    
        mess = ['Nice Job! Go take a walk outside!',
                'Nice Job! Wiggle your toes, and get back on it!',
                'Nice Job! Get up and walk around!',
                'Nice Job! Go get a drink of water!',
                'Good Work! Go take a walk outside!',
                'Good Work! Wiggle your toes, and get back on it!',
                'Good Work! Get up and walk around!',
                'Good Work! Go get a drink of water!']
    
    class Timer(Frame):
        def __init__(self, parent=None, **kw):
            Frame.__init__(self, parent, background="white")
            self.initUI(parent)
            self.inp = None
            self.timer1 = 9
            self.checkTimer1 = 1
            self.timer1Run = 0
            self.tempObj = 0
            self.entdat = StringVar()
            self.timestr = StringVar()
            self.makeTimer()
            self.makeWidgets()
    
        def initUI(self, parent):
            self.parent = parent
            self.parent.title("Timer")
            self.sb = Button(self.parent, text='Start',
                             command=self.start).pack(side=LEFT)
            self.stb = Button(self.parent, text='Pause',
                              command=self.stop).pack(side=LEFT)
            self.rb = Button(self.parent, text='Reset',
                             command=self.reset).pack(side=LEFT)
            self.qb = Button(self.parent, text='Quit',
                             command=parent.quit).pack(side=LEFT)
    
        def makeWidgets(self):
            minutes = int(self.timer1/60)
            seconds = int(self.timer1 - minutes*60.0)
            self.timestr.set('%02d:%02d' % (minutes, seconds))
            self.ol = Label(text="Objective:")
            self.ol.pack(fill=X, expand=NO, pady=2, padx=2, side=TOP)
            self.ew = Entry(textvariable=self.entdat)
            self.ew.pack(fill=X, expand=NO, pady=2, padx=2, side=TOP)
            self.b = Button(text="OK", command=self.clicked)
            self.b.pack(side=TOP)
    
        def makeTimer(self):
            self.l = Label(self, textvariable=self.timestr)
            self.l.pack(fill=X, expand=NO, pady=2, padx=2)
    
        def updateTime(self):
            self.elapsed = time.time() - self.startTime
            self.curtim1 = self.timer1 - self.elapsed
            if self.curtim1 <= 0:
                self.stop()
                self.checkValue()
            else:
                minutes = int(self.curtim1/60)
                seconds = int(self.curtim1 - minutes*60.0)
                self.timestr.set('%02d:%02d' % (minutes, seconds))
                self.timer = self.after(1, self.updateTime)
    
        def checkValue(self):
            if self.checkTimer1 == 1:
                self.destroyWidgets()
                self.timer1 = 3
                self.checkTimer1 -= 1
                self.start()
            else:
                self.dat.destroy()
                self.makeWidgets()
                self.timer1 = 9
                self.checkTimer1 += 1
                pu = PopUp()
                pu.message()
                self.start()
    
        def start(self):
            if not self.timer1Run:
                self.startTime = time.time()
                self.updateTime()
                self.timer1Run = 1
    
        def stop(self):
             if self.timer1Run:
                 self.after_cancel(self.timer)
                 self.timer1Run = 0
                 self.timer1 = self.curtim1
    
        def reset(self):
            self.timer1 = 900
            minutes = int(self.timer1/60)
            seconds = int(self.timer1 - minutes*60.0)
            self.timestr.set('%02d:%02d' % (minutes, seconds))
    
        def clicked(self):
            self.destroyWidgets()
            self.dat = Label(self, textvariable=self.entdat)
            self.dat.pack(side=TOP)
    
        def destroyWidgets(self):
            self.ew.destroy()
            self.ol.destroy()
            self.b.destroy()
    
    def main():
        root = Tk()
        root.geometry("240x235+25+50")
        tm = Timer(root)
        tm.pack(side=TOP)
        mainloop()
    
    if __name__ == '__main__':
        main()
    

