Tkinter: Flash window when trying to click

I tried to do this for a while, but did not understand how to do it.

I have a tkinter script that creates a popup when a button is clicked. However, I do not want the user to be able to click this window in any of the previously created windows. I have work with root.grab_set (), but the user has no indication that they should remain in this window.

class popup(object):
    def __init__(self, parent):
        self.root=Toplevel(parent)
        self.root.grab_set() #prevents the user clicking on the parent window
                             #But the window doesnt 'flash' when an attempt to click away is made

For example, when you have a window created by the filedialogue module, if you try to click on another window, the filedialogue window remains at the top and has a “blinking” animation so that the user knows that they cannot click. Is there any way to reproduce this effect? Going through the filedialogue source was not good for me, and Google is not looking for Google.

+4
source share
2 answers

The easiest way I can think of is to use the event and focus commands along with the windows command bell:

#!python3

import tkinter as tk

class popup(object):
    def __init__(self, parent):
        self.root=tk.Toplevel(parent)
        self.root.title("Popup")
        self.root.bind("<FocusOut>", self.Alarm)

    def Alarm(self, event):
        self.root.focus_force()
        self.root.bell()

main = tk.Tk()
main.title("Main")
pop = popup(main)
main.mainloop()
+2
source

Here is a windows solution that uses FlashWindowExfrom dll user32. You need to pass the object FLASHWINFO. grab_setensures that the pop-up window remains in focus and disables any widgets in the main window, which makes the transition time of the pop-up window always at the top of the wizard. The event is <Button-1>used to check mouse clicks, and winfo_containingchecks whether another window has clicked, except for the pop-up window. Then I set the focus back to the popup and lock the window in focus (which is always a popup).

pywin32, .

import Tkinter as tk
from ctypes import *
import win32con

class popup(object):
    def __init__(self, parent):
        self.parent = parent
        self.root=tk.Toplevel(self.parent)
        self.root.title("Popup")
        self.root.grab_set()
        self.root.transient(self.parent)
        self.root.bind("<Button-1>", self.flash)

    def flash(self, event):
        if self.root.winfo_containing(event.x_root, event.y_root)!=self.root:
            self.root.focus_set()
            number_of_flashes = 5
            flash_time = 80
            info = FLASHWINFO(0,
                              windll.user32.GetForegroundWindow(),
                              win32con.FLASHW_ALL,
                              number_of_flashes,
                              flash_time)
            info.cbSize = sizeof(info) 
            windll.user32.FlashWindowEx(byref(info))

class FLASHWINFO(Structure): 
    _fields_ = [('cbSize', c_uint), 
                ('hwnd', c_uint), 
                ('dwFlags', c_uint), 
                ('uCount', c_uint), 
                ('dwTimeout', c_uint)]

main = tk.Tk()
main.title("Main")
pop = popup(main)
main.mainloop()

, , . , , , <FocusOut>, , , , , grab_set. , , . , , .

+3

All Articles