tk_toastMessageBox : A Tcl/Tk Procedure That Mimics Android's Toast.makeText() Fading Message Behaviour

OK.. I used Arjen Markus code on his wiki page A fading window and I update it to look as much it can be like it is on Android Toast.makeText() message. The feature of showing a string on a window and wait a second or two then fading the message is called on Android a Toast message.

If you made ,at the same run, for example like 20 messages one after the other, it will queue them and fade them all one by one very much like Android's. So here is the updated code. It's a procedure which you can call it as much as you like and its message is centered on the screen.

Usage example:

#1st parameter is the message you want to display
#2nd parameter is the number of milliseconds to keep the message on.

tk_toastMessageBox "my toastmessage" 1000 

Here is the procedure. Just copy it as is and add it to your application:

proc tk_toastMessageBox { message fade_time } {
        
        #check and wait for a previously displayed message to leave the screen
        if { [winfo exists .fading ]==1} {tkwait window .fading}
        toplevel .fading
        
        wm overrideredirect .fading 1

        #put the message toplevel window in the center of the screen
        set window_center_x_coords [ expr {([winfo screenwidth .]-[winfo width .fading])/2} ]
        set window_center_y_coords [ expr {([winfo screenheight .]-[winfo height .fading])/2} ]
        wm geometry .fading "+$window_center_x_coords+$window_center_y_coords"

        raise .fading
        
        pack [label .fading.label -text $message]
        
        tkwait visibility .fading
        
        proc fade {w degree} {
            wm attributes $w -alpha $degree
            if { $degree > 0 } {
                after 200 [list fade $w [expr {$degree-0.05}]]
            } else {
                destroy $w
            }
        }
        after $fade_time {fade .fading 1.0}

}