2016-03-29 14 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?

相关问题