-1

this is my code for in which I am trying to link two pages

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() 

But here I am getting this error message:

AttributeError: class Frame has no attribute 'show_frame'
how to remove this error 
Community
  • 1
  • 1
Nancy
  • 9

1 Answers1

1

First of all, you can not call lambda the way you did in command=lambda: controller.show_frame(...).

Suppose you did the necessary imports that I do not see in your current program, just replace those 2 statements (in 2 lines of your code) by : command=controller.show_frame(...).

Please read about how to use lambda

Second, your code contains an other error around this line:

if name == 'main':
   main()

Change it to:

if __name__ == '__main__': 
   main() 

After I fixed your error, I run your program successfully:

enter image description here

P.S. May be you would be interested in this post: What does if __name__ == "__main__": do?

Community
  • 1
  • 1
Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130