Skip to content

Instantly share code, notes, and snippets.

@israel-dryer
Created March 29, 2021 19:55
Show Gist options
  • Save israel-dryer/1de5859f2f849245855ec0a583e79186 to your computer and use it in GitHub Desktop.
Save israel-dryer/1de5859f2f849245855ec0a583e79186 to your computer and use it in GitHub Desktop.
A tkinter window that fades in and out at specified intervals
"""
Window that fades in an out at a specified time interval and increment
Author: Israel Dryer
Modified: March 29, 2021
"""
from tkinter import ttk
import tkinter
def fade_out(widget, increment):
alpha = widget.attributes('-alpha')
if (alpha - increment) >= 0:
widget.attributes('-alpha', alpha - increment)
widget.after(100, fade_out, widget, increment)
return
widget.after(100, fade_in, widget, increment)
def fade_in(widget, increment):
alpha = widget.attributes('-alpha')
if (alpha + increment) <= 1:
widget.attributes('-alpha', alpha + increment)
widget.after(100, fade_in, widget, increment)
return
widget.after(100, fade_out, widget, increment)
root = tkinter.Tk()
root.geometry('400x400')
root.eval('tk::PlaceWindow . center')
root.option_add('*font', 'Helvetica 24')
ttk.Scale(root, from_=1, to=100, value=25).pack(fill='x', padx=10, pady=10)
ttk.Label(root, text="Hello world!", anchor='center').pack(fill='x', pady=10)
root.after(100, fade_out, root, 0.05)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment