Python Forum
Tkinter fading bubble text
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter fading bubble text
#1
Was just playing around with popup bubble text. Cant seem to get more than one at a time to popup but, all will popup after the one before fades.
All thoughts are welcome.


Added a move up effect while bubble fades

import tkinter as tk
from random import randrange, uniform, choice
from time import time

class Bubble(tk.Tk):
    ''' Class creates popup window '''
    def __init__(self, *args, text='Default Text', **kwargs):
        super().__init__(*args, **kwargs)

        # Instance variables removes titlebar and grabs focus of window
        self.wait_visibility(self)
        self.wm_attributes('-topmost', True)
        self.wm_attributes('-alpha', 0.9)
        self.wm_attributes('-type', 'splash')
        self.focus_force()

        # Get time window opens, set duration for window, and set opacity of window
        self.start = time()
        self.duration = 5
        self.opacity = float(0.9)

        self.label = tk.Label(self, text=text, padx=10, pady=10, bg='#fffdd0')
        self.label.pack(fill='x', expand=True)
        self.label.configure(
            wraplength = 300,
            justify = 'left',
            font = (None, 14, 'normal')
        )

        # get window info for positioning of window
        self.update()

        # Calculations for window geometry
        x = self.winfo_screenwidth()//2, self.winfo_screenwidth()-int(self.winfo_screenwidth())
        self.xpos = randrange(x[0], self.winfo_screenwidth()-int(self.label.winfo_width()*0.9))
        
        self.ypos = int(self.winfo_screenheight()*uniform(0.2, 0.4))
        
        # Set window geometry
        self.geometry(f'{self.label.winfo_width()}x{self.label.winfo_height()}+{self.xpos}+{self.ypos}')

        # Start timer and mainloop
        self.timer()
        self.mainloop()
        


    def timer(self):
        ''' Method for countdown timer '''
        counter = self.duration + self.start - time()
        seconds = int(divmod(counter, 60)[1])

        # Call timer every one second
        self._timer = self.after(1000, self.timer)

        if seconds == 1:
            self.fade()

    def move(self):
        ''' Method for moving bubble up '''
        self.ypos -= 20

    def fade(self):
        ''' Method for creating fade and move up effect '''
        self.opacity -= float(0.1)
        self.wm_attributes('-alpha', self.opacity)
        disolve = self.after(100, self.fade)
        up = self.after(0, self.move)
        self.geometry(f'{self.label.winfo_width()}x{self.label.winfo_height()}+{self.xpos}+{self.ypos}')

        if self.opacity <= float(0.0):
            self.after_cancel(self._timer)
            self.after_cancel(disolve)
            self.after_cancel(up)
            self.destroy()

messages = [
    'Hello World! This is a random messages.',
    'The skys are blue and the clouds are white and fluffy.',
    'I look at you and my blood boils hot, I feel my temperature rise.',
    'Who\'s that knocking at the door? Is it you again? You can love me tonight, \
if you want. In the moring make sure your gone.'
]

t = Bubble(text=choice(messages))
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#2
Thought I would add a video. Used a loop to do five popups

I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020