2016-03-29 48 views
-1

這是我的代碼中,我試圖鏈接

兩頁
from Tkinter import * 

class Example(Frame): 

    def __init__(self , parent, controller): 
     Frame.__init__(self, parent)  
     self.controller=controller 
     self.parent = parent 
     self.parent.title("f2") 
     self.parent.configure(background="royalblue4") 
     self.pack(fill=BOTH, expand=1) 

     w = 800 
     h = 500 

     sw = self.parent.winfo_screenwidth() 
     sh = self.parent.winfo_screenheight() 

     x = (sw - w)/2 
     y = (sh - h)/2 
     self.parent.geometry('%dx%d+%d+%d' % (w, h, x, y)) 

     self.logbtn1 = Button(self,text="SIGN UP",font=("Copperplate Gothic Bold",16),bg=("dark green"),activebackground=("green"),command=lambda: controller.show_frame("D:\java prgms\minor\signup")) 
     self.logbtn1.place(x=325,y=175) 

     self.logbtn2 = Button(self, text="LOGIN",font=("Copperplate Gothic Bold",16),bg=("cyan"),activebackground=("yellow"),command=lambda: controller.show_frame("D:\java prgms\minor\log1")) 
     self.logbtn2.place(x=335,y=220) 

     self.pack() 

def main():  
    root = Tk() 
    ex = Example(root,Frame) 
    root.mainloop() 


if __name__ == '__main__': 
    main() 

但是在這裏我得到這個錯誤信息:「AttributeError錯誤:類框架有沒有屬性‘show_frame’」

AttributeError: class Frame has no attribute 'show_frame' 
how to remove this error 
+0

我試過你的代碼它工作 –

回答

1

首先,您不能像您在command=lambda: controller.show_frame(...)中那樣調用lambda。

假設你做了必要的導入操作,但是在當前程序中沒有看到,只需將這兩個語句(代碼的兩行代碼)替換爲:command=controller.show_frame(...)

請閱讀有關how to use lambda

其次,你的代碼包含圍繞這一行的其他錯誤:

if name == 'main': 
    main() 

將其更改爲:

if __name__ == '__main__': 
    main() 

我定你的錯誤後,我跑您的程序成功完成:

enter image description here

P.S.可能是你會對這個職位感興趣:What does if __name__ == "__main__": do?

相關問題