In the following code I'll get the following error if I right click the window that pops up. Then go down to the very bottom entry widget then delete it's contents. It seems to be giving me a TclError. How do I go about handeling such an error?
The Error
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python26\Lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "C:\Python26\CPUDEMO.py", line 503, in I
TL.sclS.set(S1)
File "C:\Python26\Lib\lib-tk\Tkinter.py", line 2765, in set
self.tk.call(self._w, 'set', value)
TclError: expected floating-point number but got ""
The Code
#F
#PIthon.py
# Import/Setup
import Tkinter
import psutil,time
import re
from PIL import Image, ImageTk
from time import sleep
class simpleapp_tk(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
Widgets
self.menu = Tkinter.Menu(self, tearoff = 0 )
M = [ "Options...", "Exit"]
self.selectedM = Tkinter.StringVar()
self.menu.add_radiobutton( label = 'Hide', variable = self.selectedM, command = self.E )
self.menu.add_radiobutton( label = 'Bump', variable = self.selectedM, command = self.E )
self.menu.add_separator()
self.menu.add_radiobutton( label = 'Options...', variable = self.selectedM, command = self.E )
self.menu.add_separator()
self.menu.add_radiobutton( label = 'Exit', variable = self.selectedM, command = self.E )
self.frame1 = Tkinter.Frame(self,bg='grey15',relief='ridge',borderwidth=4,width=185, height=39)
self.frame1.grid()
self.frame1.grid_propagate(0)
self.frame1.bind( "<Button-3><ButtonRelease-3>", self.D )
self.frame1.bind( "<Button-2><ButtonRelease-2>", self.C )
self.frame1.bind( "<Double-Button-1>", self.C )
self.labelVariable = Tkinter.StringVar()
self.label = Tkinter.Label(self.frame1,textvariable=self.labelVariable,fg="lightgreen",bg="grey15",borderwidth=1,font=('arial', 10, 'bold'))
self.label.grid(column=1,row=0,columnspan=1,sticky='nsew')
self.label.bind( "<Button-3><ButtonRelease-3>", self.D )
self.label.bind( "<Button-2><ButtonRelease-2>", self.C )
self.label.bind( "<Double-Button-1>", self.C )
self.F()
self.overrideredirect(1)
self.wm_attributes("-topmost", 1)
global TL1
TL1 = Tkinter.Toplevel(self)
TL1.wm_geometry("+0+5000")
TL1.overrideredirect(1)
TL1.button = Tkinter.Button(TL1,text="? CPU",fg="lightgreen",bg="grey15",activeforeground="lightgreen", activebackground='grey15',borderwidth=4,font=('Arial', 8, 'bold'),command=self.J)
TL1.button.pack(ipadx=1)
Events
def Reset(self):
self.label.configure(font=('arial', 10, 'bold'),fg='Lightgreen',bg='grey15',borderwidth=0)
self.labela.configure(font=('arial', 8, 'bold'),fg='Lightgreen',bg='grey15',borderwidth=0)
self.frame1.configure(bg='grey15',relief='ridge',borderwidth=4,width=224, height=50)
self.label.pack(ipadx=38)
def helpmenu(self):
t2 = Tkinter.Toplevel(self)
Tkinter.Label(t2, text='This is a help menu',
anchor="w",justify="left",fg="darkgreen",bg="grey90",relief="ridge",borderwidth=5,font=('Arial', 10)).pack(fill='both', expand=1)
t2.resizable(False,False)
t2.title('Help')
menu = Tkinter.Menu(self)
t2.config(menu=menu)
filemenu = Tkinter.Menu(menu)
menu.add_cascade(label="| Exit |", menu=filemenu)
filemenu.add_command(label="Exit", command=t2.destroy)
def aboutmenu(self):
t1 = Tkinter.Toplevel(self)
Tkinter.Label(t1, text=' About:\n\n CPU Usage v1.0\n\n Publisher:
Drew French\n Date: 05/09/10\n Email:
[email protected] \n\n\n\n\n\n\n Written in Python 2.6.4',
anchor="w",justify="left",fg="darkgreen",bg="grey90",relief="sunken",borderwidth=5,font=('Arial', 10)).pack(fill='both', expand=1)
t1.resizable(False,False)
t1.title('About')
menu = Tkinter.Menu(self)
t1.config(menu=menu)
filemenu = Tkinter.Menu(menu)
menu.add_cascade(label="| Exit |", menu=filemenu)
filemenu.add_command(label="Exit", command=t1.destroy)
def A (self,event):
TL.entryVariable1.set(TL.sclY.get())
TL.entryVariable2.set(TL.sclX.get())
Y = TL.sclY.get()
X = TL.sclX.get()
self.wm_geometry("+" + str(X) + "+" + str(Y))
def B(self,event):
Y1 = TL.entryVariable1.get()
X1 = TL.entryVariable2.get()
self.wm_geometry("+" + str(X1) + "+" + str(Y1))
TL.sclY.set(Y1)
TL.sclX.set(X1)
def C(self,event):
s = self.wm_geometry()
geomPatt = re.compile(r"(\d+)?x?(\d+)?([+-])(\d+)([+-])(\d+)")
m = geomPatt.search(s)
X3 = m.group(4)
Y3 = m.group(6)
M = int(Y3) - 150
P = M + 150
while Y3 > M:
sleep(0.0009)
Y3 = int(Y3) - 1
self.update_idletasks()
self.wm_geometry("+" + str(X3) + "+" + str(Y3))
sleep(2.00)
while Y3 < P:
sleep(0.0009)
Y3 = int(Y3) + 1
self.update_idletasks()
self.wm_geometry("+" + str(X3) + "+" + str(Y3))
def D(self, event=None):
self.menu.post( event.x_root, event.y_root )
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'))
TL.label7.grid(column=1,row=3,stick="W",ipady=10)
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.label7 = Tkinter.Label(TL.separator,text="Refresh Rate:",fg="black",bg="grey95",font=('Arial', 8 ,'bold'))
TL.label7.grid(column=1,row=5,stick="W",ipady=10)
TL.sclS = Tkinter.Scale(TL.separator, from_=10, to=2000, orient='horizontal', resolution=10, command=self.H)
TL.sclS.grid(column=1,row=6,ipadx=27, sticky='w')
TL.sclS.set(650)
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=7,ipadx=20, pady=10,sticky='EW')
TL.entryS.bind("<Return>", self.I)
TL.entryVariableS.set(650)
#
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()
if self.selectedM.get() == 'Bump':
s = self.wm_geometry()
geomPatt = re.compile(r"(\d+)?x?(\d+)?([+-])(\d+)([+-])(\d+)")
m = geomPatt.search(s)
X3 = m.group(4)
Y3 = m.group(6)
M = int(Y3) - 150
P = M + 150
while Y3 > M:
sleep(0.0009)
Y3 = int(Y3) - 1
self.update_idletasks()
self.wm_geometry("+" + str(X3) + "+" + str(Y3))
sleep(2.00)
while Y3 < P:
sleep(0.0009)
Y3 = int(Y3) + 1
self.update_idletasks()
self.wm_geometry("+" + str(X3) + "+" + str(Y3))
if self.selectedM.get() == 'Hide':
s = self.wm_geometry()
geomPatt = re.compile(r"(\d+)?x?(\d+)?([+-])(\d+)([+-])(\d+)")
m = geomPatt.search(s)
X3 = m.group(4)
Y3 = m.group(6)
M = int(Y3) + 5000
self.update_idletasks()
self.wm_geometry("+" + str(X3) + "+" + str(M))
TL1.wm_geometry("+0+190")
def F (self):
G = round(psutil.cpu_percent(), 1)
G1 = str(G) + '%'
self.labelVariable.set(G1)
try:
S2 = TL.entryVariableS.get()
except ValueError, e:
S2 = 650
except NameError:
S2 = 650
self.after(int(S2), self.F)
def
G (self,event):
if TL.selectedP.get() =='Normal':
self.label.config( fg = 'lightgreen' )
TL1.button.config( fg = 'lightgreen',activeforeground='lightgreen')
if TL.selectedP.get() =='Red':
self.label.config( fg = 'red' )
TL1.button.config( fg = 'red',activeforeground='red')
if TL.selectedP.get() =='Orange':
self.label.config( fg = 'orange')
TL1.button.config( fg = 'orange',activeforeground='orange')
if TL.selectedP.get() =='Yellow':
self.label.config( fg = 'yellow')
TL1.button.config( fg = 'yellow',activeforeground='yellow')
if TL.selectedP.get() =='Light Green':
self.label.config( fg = 'lightgreen' )
TL1.button.config( fg = 'lightgreen',activeforeground='lightgreen')
if TL.selectedP.get() =='Normal':
self.label.config( fg = 'lightgreen' )
TL1.button.config( fg = 'lightgreen',activeforeground='lightgreen')
if TL.selectedP.get() =='Steel Blue':
self.label.config( fg = 'steelblue1' )
TL1.button.config( fg = 'steelblue1',activeforeground='steelblue1')
if TL.selectedP.get() =='Blue':
self.label.config( fg = 'blue')
TL1.button.config( fg = 'blue',activeforeground='blue')
if TL.selectedP.get() =='Green':
self.label.config( fg = 'darkgreen' )
TL1.button.config( fg = 'darkgreen',activeforeground='darkgreen')
if TL.selectedP.get() =='White':
self.label.config( fg = 'white' )
TL1.button.config( fg = 'white',activeforeground='white')
if TL.selectedP.get() =='Black':
self.label.config( fg = 'black')
TL1.button.config( fg = 'black',activeforeground='black')
def H (self,event):
TL.entryVariableS.set(TL.sclS.get())
S = TL.sclS.get()
def I (self,event):
S1 = TL.entryVariableS.get()
TL.sclS.set(S1)
TL.sclS.set(TL.sclS.get())
S1 = TL.entryVariableS.get()
TL.sclS.set(S1)
def J (self):
s = self.wm_geometry()
geomPatt = re.compile(r"(\d+)?x?(\d+)?([+-])(\d+)([+-])(\d+)")
m = geomPatt.search(s)
X3 = m.group(4)
Y3 = m.group(6)
M = int(Y3) - 5000
self.update_idletasks()
self.wm_geometry("+" + str(X3) + "+" + str(M))
TL1.wm_geometry("+0+5000")
Loop
if name == "main":
app = simpleapp_tk(None)
app.mainloop()