Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Playing around with tkinter and bubble popups

menator01

Gold Coder
Can't seem to get more than one at a time to popup but, all do popup after the one before fades.
Any thoughts welcome.

Python:
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))

To view this content we will need your consent to set third party cookies.
For more detailed information, see our cookies page.
 
Last edited:

Latest posts

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom