Search code examples
pythonuser-interfaceanimationtkintergrid-layout

Is it possible to animate frames that are layed out with the grid manager in tkinter?


I made this desktop application with tkinter and finished the main functionalities. But now I want to make it look better and even animate the menus is possible. The thing is the buttons and widgets that I used are contained in frames and all of them were put by using the grid layout manageR. So I was wondering if i could maybe animate elements ( mainly buttons and frames ) w

For an example if I hover my mouse over a button it'll increase its size a bit, but I want of course to see the increase of that size in time. O r maybe when I click one button and I need to change to a new frame, then the old one will slide to the right for example


Solution

  • This is example which use after to move Frame with Label and Button. I used place() to use relative position so Frame leave window even if you change window size.

    import tkinter as tk
    
    # --- functions ---
    
    def move(steps=10, distance=0.1):
        if steps > 0:
            # get current position
            relx = float(frame.place_info()['relx'])
    
            # set new position
            frame.place_configure(relx=relx+distance)
    
            # repeate it after 10ms
            root.after(10, move, steps-1, distance)
    
    def on_click():
        print('on_click')
        # start move
        move(50, 0.02) # 50*0.02 = 1
    
    # --- main --
    
    root = tk.Tk()
    
    frame = tk.Frame(root, background='red')
    frame.place(relx=0, rely=0, relwidth=1, relheight=1)
    
    # to center label and button
    #frame.grid_columnconfigure(0, weight=1)
    #frame.grid_rowconfigure(0, weight=1)
    #frame.grid_rowconfigure(3, weight=1)
    
    label = tk.Label(frame, text='Frame with Label and Button')
    label.grid(row=1, column=0)
    
    button = tk.Button(frame, text='MOVE', command=on_click)
    button.grid(row=2, column=0)
    
    root.mainloop()