Global name not defined
- by anteater7171
I wrote a CPU monitoring program in Python. For some reason sometimes the the program will run without any problem. Then other times the program won't even start because of the following error.
Traceback (most recent call last):
File "", line 244, in run_nodebug
File "C:\Python26\CPUR1.7.pyw", line 601, in
app = simpleapp_tk(None)
File "C:\Python26\CPUR1.7.pyw", line 26, in init
self.initialize()
File "C:\Python26\CPUR1.7.pyw", line 107, in initialize
self.F()
File "C:\Python26\CPUR1.7.pyw", line 517, in F
S2 = TL.entryVariableS.get()
NameError: global name 'TL' is not defined
I can't seem to find the problem, maybe someone more experienced may assist me?
Here is a snippet of the part giving me trouble:
(The second to last line in the snippet is what's giving me trouble)
def E(self):
if self.selectedM.get() =='Options...':
Setup
global TL
TL = Tkinter.Toplevel(self)
menu = Tkinter.Menu(TL)
TL.config(menu=menu)
filemenu = Tkinter.Menu(menu)
menu.add_cascade(label="| Menu |", menu=filemenu)
filemenu.add_command(label="Instruction Manual...", command=self.helpmenu)
filemenu.add_command(label="About...", command=self.aboutmenu)
filemenu.add_separator()
filemenu.add_command(label="Exit Options", command=TL.destroy)
filemenu.add_command(label="Exit", command=self.destroy)
helpmenu = Tkinter.Menu(menu)
menu.add_cascade(label="| Help |", menu=helpmenu)
helpmenu.add_command(label="Instruction Manual...", command=self.helpmenu)
helpmenu.add_separator()
helpmenu.add_command(label="Quick Help...", command=self.helpmenu)
Title
TL.label5 = Tkinter.Label(TL,text="CPU Usage: Options",anchor="center",fg="black",bg="lightgreen",relief="ridge",borderwidth=5,font=('Arial', 18, 'bold'))
TL.label5.pack(padx=15,ipadx=5)
X Y scale
TL.separator = Tkinter.Frame(TL,height=7, bd=1, relief='ridge', bg='grey95')
TL.separator.pack(pady=5,padx=5)
#
TL.sclX = Tkinter.Scale(TL.separator, from_=0, to=1500, orient='horizontal', resolution=1, command=self.A)
TL.sclX.grid(column=1,row=0,ipadx=27, sticky='w')
TL.label1 = Tkinter.Label(TL.separator,text="X",anchor="s",fg="black",bg="grey95",font=('Arial', 8 ,'bold'))
TL.label1.grid(column=0,row=0, pady=1, sticky='S')
TL.sclY = Tkinter.Scale(TL.separator, from_=0, to=1500, resolution=1, command=self.A)
TL.sclY.grid(column=2,row=1,rowspan=2,sticky='e', padx=4)
TL.label3 = Tkinter.Label(TL.separator,text="Y",fg="black",bg="grey95",font=('Arial', 8 ,'bold'))
TL.label3.grid(column=2,row=0, padx=10, sticky='e')
TL.entryVariable2 = Tkinter.StringVar()
TL.entry2 = Tkinter.Entry(TL.separator,textvariable=TL.entryVariable2,
fg="grey15",bg="grey90",relief="sunken",insertbackground="black",borderwidth=5,font=('Arial', 10))
TL.entry2.grid(column=1,row=1,ipadx=20, pady=10,sticky='EW')
TL.entry2.bind("<Return>", self.B)
TL.label2 = Tkinter.Label(TL.separator,text="X:",fg="black",bg="grey95",font=('Arial', 8 ,'bold'))
TL.label2.grid(column=0,row=1, ipadx=4, sticky='W')
TL.entryVariable1 = Tkinter.StringVar()
TL.entry1 = Tkinter.Entry(TL.separator,textvariable=TL.entryVariable1,
fg="grey15",bg="grey90",relief="sunken",insertbackground="black",borderwidth=5,font=('Arial', 10))
TL.entry1.grid(column=1,row=2,sticky='EW')
TL.entry1.bind("<Return>", self.B)
TL.label4 = Tkinter.Label(TL.separator,text="Y:", anchor="center",fg="black",bg="grey95",font=('Arial', 8 ,'bold'))
TL.label4.grid(column=0,row=2, ipadx=4, sticky='W')
TL.label7 = Tkinter.Label(TL.separator,text="Text Colour:",fg="black",bg="grey95",font=('Arial', 8 ,'bold'),justify='left')
TL.label7.grid(column=1,row=3, sticky='W',padx=10,ipady=10,ipadx=30)
TL.selectedP = Tkinter.StringVar()
TL.opt1 = Tkinter.OptionMenu(TL.separator, TL.selectedP,'Normal', 'White','Black', 'Blue', 'Steel Blue','Green','Light Green','Yellow','Orange' ,'Red',command=self.G)
TL.opt1.config(fg="black",bg="grey90",activebackground="grey90",activeforeground="black",
anchor="center",relief="raised",direction='right',font=('Arial', 10))
TL.opt1.grid(column=1,row=4,sticky='EW',padx=20,ipadx=20)
TL.selectedP.set('Normal')
TL.sclS = Tkinter.Scale(TL.separator, from_=10, to=2000, orient='horizontal', resolution=10, command=self.H)
TL.sclS.grid(column=1,row=5,ipadx=27, sticky='w')
TL.sclS.set(600)
TL.entryVariableS = Tkinter.StringVar()
TL.entryS = Tkinter.Entry(TL.separator,textvariable=TL.entryVariableS,
fg="grey15",bg="grey90",relief="sunken",insertbackground="black",borderwidth=5,font=('Arial', 10))
TL.entryS.grid(column=1,row=6,ipadx=20, pady=10,sticky='EW')
TL.entryS.bind("<Return>", self.I)
TL.entryVariableS.set(600)
#
TL.resizable(False,False)
TL.title('Options')
geomPatt = re.compile(r"(\d+)?x?(\d+)?([+-])(\d+)([+-])(\d+)")
s = self.wm_geometry()
m = geomPatt.search(s)
X = m.group(4)
Y = m.group(6)
TL.sclY.set(Y)
TL.sclX.set(X)
if self.selectedM.get() == 'Exit':
self.destroy()
def F (self):
G = round(psutil.cpu_percent(), 1)
G1 = str(G) + '%'
self.labelVariable.set(G1)
if G < 5:
self.imageLabel.configure(image=self.image0)
if G >= 5:
self.imageLabel.configure(image=self.image5)
if G >= 10:
self.imageLabel.configure(image=self.image10)
if G >= 15:
self.imageLabel.configure(image=self.image15)
if G >= 20:
self.imageLabel.configure(image=self.image20)
if G >= 25:
self.imageLabel.configure(image=self.image25)
if G >= 30:
self.imageLabel.configure(image=self.image30)
if G >= 35:
self.imageLabel.configure(image=self.image35)
if G >= 40:
self.imageLabel.configure(image=self.image40)
if G >= 45:
self.imageLabel.configure(image=self.image45)
if G >= 50:
self.imageLabel.configure(image=self.image50)
if G >= 55:
self.imageLabel.configure(image=self.image55)
if G >= 60:
self.imageLabel.configure(image=self.image60)
if G >= 65:
self.imageLabel.configure(image=self.image65)
if G >= 70:
self.imageLabel.configure(image=self.image70)
if G >= 75:
self.imageLabel.configure(image=self.image75)
if G >= 80:
self.imageLabel.configure(image=self.image80)
if G >= 85:
self.imageLabel.configure(image=self.image85)
if G >= 90:
self.imageLabel.configure(image=self.image90)
if 100> G >= 95:
self.imageLabel.configure(image=self.image95)
if G == 100:
self.imageLabel.configure(image=self.image100)
S2 = TL.entryVariableS.get()
self.after(int(S2), self.F)