Python tkinter "KeyError: <class __main__." error -
i relatively new tkinter. created application , ran fine. added helppage class , gave me error "keyerror: " removed helppage class , same error showed main.pagetwo. cannot seem run anymore.
here code (the functions indented though doesn't show up):
## program calculates number of pixels need removed each edge of collected images based on camera paramters , measured distances ## horizontal_field_of_view = camera_distance*(sensor_width/focal_length) sensor_width horizontal width. sensor_width , focal_length measured in mm , camera distance measure in m ## resolution = horizontal_field_of_view/number_of_pixels horizontal_field_of_view measured in m. results width of single pixel measured in m. import tkinter tk import tkfiledialog import os class application(tk.tk): pixsize=0 cnt=1 height=0 width=0 numpixx=0 numpixy=0 numimages=0 file='none' def __init__(self,*args,**kwargs): tk.tk.__init__(self,*args,**kwargs) container = tk.frame(self) container.pack(side="top",fill="both",expand = true) container.grid_rowconfigure(0,weight=1) container.grid_columnconfigure(0,weight=1) self.frames={} f in (startpage,pageone,pagetwo,pagethree,finalpage,helppage): frame = f(container,self) self.frames[f]= frame frame.grid(row=0,column=0,sticky="nsew") self.show_frame(startpage) ## opens file data stored in def selectfolder(self): folder=tkfiledialog.asksaveasfile() self.file=folder self.show_frame(pageone) def show_frame(self,controller): frame=self.frames[controller] frame.tkraise() def rescalc(self,argcamdist,argsenwidth,argfoclen,argnumpix,cont): ## runs both equations calculating spatial resolution ## part one: calculating hfov hfov = float(argcamdist.get())*(float(argsenwidth.get())/float(argfoclen.get())) ## part two: calculating pixsize self.pixsize = float(argnumpix.get())/hfov cont.show_frame(pagethree) def getnumbers(self,argnumimages,argnumpixx,argnumpixy,cont): self.numpixx=int(argnumpixx.get()) self.numpixy=int(argnumpixy.get()) self.numimages=int(argnumimages.get()) cont.file.write(str(self.numimages)) cont.show_frame(pagetwo) def clipcalculator(self,arglabel,argtop,argleft,argbottom,argright,argnumpixx,argnumpixy,argnumimages,arglabel3,arglabel4,cont): ## calculates number of pixels removed each direction , writes file upper=int(self.pixsize*float(argtop.get()))+1 left=int(self.pixsize*float(argleft.get()))+1 if cont.cnt==1: bottom=int(argnumpixy)-(int(self.pixsize*float(argbottom.get()))+1) right=int(argnumpixx)-(int(self.pixsize*float(argright.get()))+1) cont.width=right-left cont.height=upper-bottom else: bottom=upper+cont.height right=left+cont.width cont.file.write('upper left: rows:{0}:columns:{1} \n'.format(upper,left)) cont.file.write('upper right: rows:{0}:columns:{1} \n'.format(upper,right)) cont.file.write('lower left: rows:{0}:columns:{1} \n'.format(bottom,left)) cont.file.write('lower right: rows:{0}:columns:{1} \n'.format(bottom,right)) cont.cnt+=1 ##updates label , resets entry windows default refreshes page arglabel['text']="image number {0}".format(cont.cnt) argtop.delete(0,tk.end) argtop.insert(0,"0.0") argleft.delete(0,tk.end) argleft.insert(0,"0.0") argbottom.delete(0,tk.end) argbottom.insert(0,"0.0") argright.delete(0,tk.end) argright.insert(0,"0.0") cont.refresh(argbottom,argright,arglabel3,arglabel4,cont) def refresh(self,label3,label4,entry3,entry4,cont): if cont.cnt==1: cont.show_frame(pagetwo) if (cont.cnt > 1 , cont.cnt <= cont.numimages): label3.forget() label4.forget() entry3.forget() entry4.forget() cont.show_frame(pagetwo) else: cont.file.close() cont.show_frame(finalpage) class startpage(tk.frame): def __init__(self,parent,controller): tk.frame.__init__(self,parent) label=tk.label(self,text="image overlap program v 1.0") label.pack(pady=10,padx=10) button1 = tk.button(self, text="help", command=controller.show_frame(helppage)) button1.pack() button2 = tk.button(self, text="select folder", command=lambda: controller.selectfolder()) button2.pack() class pageone(tk.frame): def __init__(self,parent,controller): tk.frame.__init__(self,parent) label3=tk.label(self,text="camera parameters") label3.pack(pady=10,padx=10) label1=tk.label(self,text="camera distance").pack() entry1=tk.entry(self) entry1.insert(0,"0.0") entry1.pack() label2=tk.label(self,text="sensor width").pack() entry2=tk.entry(self) entry2.insert(0,"0.0") entry2.pack() label3=tk.label(self,text="focal length").pack() entry3=tk.entry(self) entry3.insert(0,"0.0") entry3.pack() label4=tk.label(self,text="number of pixels").pack() entry4=tk.entry(self) entry4.insert(0,"0.0") entry4.pack() button1=tk.button(self,text='run',command = lambda: controller.rescalc(entry1,entry2,entry3,entry4,controller)) button1.pack() button3 = tk.button(self, text="home", command=lambda: controller.show_frame(startpage)) button3.pack() class pagetwo(tk.frame): def __init__(self,parent,controller): tk.frame.__init__(self,parent) cont=controller argnumpixx= cont.numpixx argnumpixy= cont.numpixy argnumimages= cont.numimages title=tk.label(self,text="distance parameters") title.pack(pady=10,padx=10) label5=tk.label(self,text="image number {0}".format(cont.cnt)) label5.pack() label1=tk.label(self,text="how many m of image needs removed top") label1.pack() entry1=tk.entry(self) entry1.insert(0,"0.0") entry1.pack() label2=tk.label(self,text="how many m of image needs removed left") label2.pack() entry2=tk.entry(self) entry2.insert(0,"0.0") entry2.pack() label3=tk.label(self,text="how many m of image needs removed bottm") label3.pack() entry3=tk.entry(self) entry3.insert(0,"0.0") entry3.pack() label4=tk.label(self,text="how many m of image needs removed right") label4.pack() entry4=tk.entry(self) entry4.insert(0,"0.0") entry4.pack() button1=tk.button(self,text="run1",command= lambda: controller.clipcalculator(label5,entry1,entry2,entry3,entry4,argnumpixx,argnumpixy,argnumimages,label3,label4,cont)) button1.pack() button3 = tk.button(self, text="home", command=lambda: controller.show_frame(startpage)) button3.pack() class pagethree(tk.frame): def __init__(self,parent,controller): tk.frame.__init__(self,parent) title=tk.label(self,text="image parameters") title.pack(pady=10,padx=10) label1=tk.label(self,text="how many images in set").pack() entry1=tk.entry(self) entry1.insert(0,"0.0") entry1.pack() label2=tk.label(self,text="how many pixels in x direction").pack() entry2=tk.entry(self) entry2.insert(0,"0.0") entry2.pack() label3=tk.label(self,text="how many pixels in y direction").pack() entry3=tk.entry(self) entry3.insert(0,"0.0") entry3.pack() button2 = tk.button(self, text="run", command=lambda: controller.getnumbers(entry1,entry2,entry3,controller)) button2.pack() button1 = tk.button(self, text="home", command=lambda: controller.show_frame(startpage)) button1.pack() class finalpage(tk.frame): def __init__(self,parent,controller): tk.frame.__init__(self,parent) label2=tk.label(self,text="done") label2.pack(pady=10,padx=10) button2 = tk.button(self, text="home", command=lambda: controller.show_frame(startpage)) button2.pack() class helppage(tk.frame): def __init__(self,parent,controller): tk.frame.__init__(self,parent) label2=tk.label(self,text="help page") label2.pack(pady=10,padx=10) button2 = tk.button(self, text="home", command=lambda: controller.show_frame(startpage)) button2.pack() app=application() app.mainloop()
any appreciated
simple fix use lambda when setting command property of button in startpage
.
you calling function controller.show_frame(helppage)
@ point call tk.button
, whereas intended call @ point button pressed.
so instead of
button1 = tk.button(self, text="help", command=controller.show_frame(helppage))
use
button1 = tk.button(self, text="help", command=lambda: controller.show_frame(helppage))
like have in other places in code.
Comments
Post a Comment