2015-06-20 57 views
1

我對tkinter比較陌生。我創建了這個應用程序,它運行得很好。然後我添加了HelpPage類,它給了我這個錯誤「KeyError:」我刪除了HelpPage類,同樣的錯誤顯示爲main.PageTwo。我似乎無法讓它運行了。Python tkinter「KeyError:<class __main__。」錯誤

這裏是代碼(該功能都縮進正確,即使它不顯示):

## This program calculates the number of pixels which need to be removed from each edge of collected images based on camera paramters and measured distances 

## horizontal_field_of_view = camera_distance*(sensor_width/focal_length) where sensor_width is the horizontal width. Sensor_width and focal_length are measured in mm and camera distance is measure in m 
## resolution = horizontal_field_of_view/number_of_pixels where horizontal_field_of_view is measured in m. The results is the width of a single pixel measured in m. 


import Tkinter as 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={} 

    for 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 the file all the data will be 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 for 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 the number of pixels to be removed from each direction and writes it to a 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 and resets entry windows to default then 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 and 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 the image needs to be removed from the top") 
    Label1.pack() 
    entry1=tk.Entry(self) 
    entry1.insert(0,"0.0") 
    entry1.pack() 

    Label2=tk.Label(self,text="How many m of the image needs to be removed from the left") 
    Label2.pack() 
    entry2=tk.Entry(self) 
    entry2.insert(0,"0.0") 
    entry2.pack() 

    Label3=tk.Label(self,text="How many m of the image needs to be removed from the bottm") 
    Label3.pack() 
    entry3=tk.Entry(self) 
    entry3.insert(0,"0.0") 
    entry3.pack() 

    Label4=tk.Label(self,text="How many m of the image needs to be removed from the 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 a set").pack() 
    entry1=tk.Entry(self) 
    entry1.insert(0,"0.0") 
    entry1.pack() 

    label2=tk.Label(self,text="How many pixels in the X direction").pack() 
    entry2=tk.Entry(self) 
    entry2.insert(0,"0.0") 
    entry2.pack() 

    label3=tk.Label(self,text="How many pixels in the 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() 

任何幫助是極大的讚賞

+1

請將您的代碼編輯到[MCVE](http://stackoverflow.com/help/mcve)中,以便我們不必篩選大約250行代碼。 – TigerhawkT3

回答

1

簡單的解決方法是使用lambda設置時StartPage中幫助按鈕的命令屬性。

您目前在撥打電話tk.Button時呼叫功能controller.show_frame(HelpPage),而您可能打算在按下按鈕時調用它。

所以不是

button1 = tk.Button(self, text="Help", command=controller.show_frame(HelpPage)) 

使用

button1 = tk.Button(self, text="Help", command=lambda: controller.show_frame(HelpPage)) 

就像你在代碼中的其他地方。

+0

如果它解決了您的問題,您可以點擊打勾和向上投票。你也得到了代表。 –