0

Following is the code that by default shows the StartPage (first Frame) when the app runs, as soon as the user clicks on the button, it redirects to another page i.e., Page1 (second Frame). I want to execute self.after(3000, lambda: (canvas.create_image(50,50,image=self.character1, anchor=NW))) after 3 seconds when the user clicks on the button when the second page (frame) opens. But what I'm getting currently is the after() method executes after 3 seconds but on the initiation of the app. i.e., when the StartPage appears. Please help me to achieve this in my code. I'm new in tkinter! Thanks.

from tkinter import *
from PIL import Image, ImageTk
   
class tkinterApp(Tk): 
      
    # __init__ function for class tkinterApp  
    def __init__(self, *args, **kwargs):  
          
        # __init__ function for class Tk 
        Tk.__init__(self, *args, **kwargs) 
          
        # creating a container 
        container = Frame(self)   
        container.pack(side = "top", fill = "both", expand = True)  
   
        # initializing frames to an empty array 
        self.frames = {}
        
        # iterating through a tuple consisting 
        # of the different page layouts 
        for F in (StartPage, Page1): 
   
            frame = F(container, self) 
   
            # initializing frame of that object from 
            # startpage, page1, page2 respectively with  
            # for loop 
            self.frames[F] = frame  
   
            frame.grid(row = 0, column = 0, sticky ="nsew")
        self.update() #edited this
        self.show_frame(StartPage)
   
    # to display the current frame passed as 
    # parameter 
    def show_frame(self, cont): # cont = page_name
        if cont not in self.frames:
            self.frames[cont] = cont(container, self)
        frame = self.frames[cont]
        frame.tkraise()
        frame.event_generate("<<ShowFrame>>")
   
# first window frame startpage 
   
class StartPage(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.bind("<<ShowFrame>>", self.myStartPage)

    def myStartPage(self,controller):
        super(StartPage).__init__()
        print(controller)
        canvas = Canvas(self,width=300, height=300, bd=0, highlightthickness=0, relief='ridge')
        canvas.pack()

        self.background = PhotoImage(file="background.png")
        canvas.create_image(300,300,image=self.background, tags="B")

        self.character1 = PhotoImage(file="bg-e-butd.png")
        obj1 = canvas.create_image(50,50,image=self.character1, anchor=NW)
        canvas.tag_bind(obj1, '<1>', lambda event=None : controller.show_frame(Page1)) 
           
      
# second window frame page1  
class Page1(Frame): 
      
    def __init__(self, parent, controller): 
          
        Frame.__init__(self, parent)

        canvas = Canvas(self,width=300, height=300, bd=0, highlightthickness=0, relief='ridge')
        canvas.pack()

        self.background = PhotoImage(file="background.png")
        canvas.create_image(300,300,image=self.background, tags="B")

        self.character1 = PhotoImage(file="bg-e-butd.png")
        self.after(1000, lambda: (canvas.create_image(50,50,image=self.character1, anchor=NW)))

   
# Driver Code 
app = tkinterApp()
app.title("Title")
app.mainloop() 

sodmzs1
  • 324
  • 1
  • 12
  • Does this answer your question? https://stackoverflow.com/questions/35029188/how-would-i-make-a-method-which-is-run-every-time-a-frame-is-shown-in-tkinter – Bryan Oakley Dec 03 '20 at 18:53
  • I have tried using `self.bind("<>", self.on_show_frame)` but the function doesn't execute. Can you please try to implement this way on my code ? – sodmzs1 Dec 03 '20 at 19:30
  • Did you call `frame.event_generate("<>")` at the end of `show_frame()`? Better update your code with the changes so far. – acw1668 Dec 04 '20 at 06:37
  • Yeah I have called. I have updated my code. kindly check it. – sodmzs1 Dec 05 '20 at 06:20
  • It may be that `mainloop()` does not take control yet, so the binding is not effective. Try adding `self.update()` before `self.show_frame(StartPage)` inside `tkinterApp.__init__()`. – acw1668 Dec 05 '20 at 15:55
  • Hi, by using `self.update()`, my function starts executing but now it's showing this error `AttributeError: 'Event' object has no attribute 'show_frame'`. Can you please check it ? I have again updated my code. Thank you so much!! – sodmzs1 Dec 06 '20 at 07:33

0 Answers0