Home > Enterprise >  tkinter - make a frame hide to the right with smooth "fading" animation
tkinter - make a frame hide to the right with smooth "fading" animation

Time:12-21

I made a simple program where the main window has a frame hideout which will slowly move out of the screen

But it doesn't work

import tkinter as tk
from tkinter import ttk
import time

root = tk.Tk()
root.geometry('360x640')

hideout = tk.Frame(root, background= '#fff', width = 360, height= 640)
def initial(*args):
    for i in range(361):
        time.sleep(0.01)
        about.place(x=i,y=0)

root.after(0,initial)
root.mainloop()

What is wrong here?

CodePudding user response:

You are using undefined variables: when calling place() on about, you probably mean hideout.

The "fading" can be controlled directly with root.after callbacks; use of time.sleep will block your GUI during an animation; this is undesirable.

Here is a working example:

import tkinter as tk

def initial(t=0):
    hideout.place(x=t, y=0)
    if t > 360:
        return
    root.after(10, initial, t 1)

if __name__ == '__main__':

    root = tk.Tk()
    root.geometry('360x640')
    hideout = tk.Frame(root, background='#fff', width=360, height=640)

    initial()
    root.mainloop()
  • Related