Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter: Window flash when attempting to click away

Tags:

python

tkinter

Ive been trying to do this for a while now, but haven't figured out a way to do it.

I have a tkinter script, that creates a popup window when a button is pressed. However I don't want the user to be able to click away from this window to any previous windows created. I have got this working with root.grab_set(), however there is no indication to the user that they must stay on that 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 attempt to click onto another window the filedialogue window stays on top and has a 'flashing' animation to let the user know they cant click away. Is there a way I can reproduce this effect? Going through the source of filedialogue hasn't been fruitful for me, and neither have Google searches.

like image 450
Titansmasher Avatar asked Feb 16 '15 12:02

Titansmasher


People also ask

How do I make a pop up in Tkinter when Button is clicked?

How do I make a pop-up in Tkinter when a button is clicked? Popup window in Tkinter can be created by defining the Toplevel (win) window. A Toplevel window manages to create a child window along with the parent window. It always opens above all the other windows defined in any application.

How to create a frameless window in Tkinter?

To create a Frameless window, we will use the overrideredirect () method. To create a Frameless Window, we will pass value True or 1 as arguments in over ride redirect () method. Below is the program that creates a normal tkinter window.

What is Tkinter in Python?

Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python.

How to create a frameless window using override in Python?

It is a standard Python interface to the Tk GUI toolkit shipped with Python. To create a Frameless window, we will use the overrideredirect () method. To create a Frameless Window, we will pass value True or 1 as arguments in over ride redirect () method.


1 Answers

The simplest way i can think to do this is to use an event and the focus commands, along with the windows bell command:

#!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()
like image 67
James Kent Avatar answered Sep 23 '22 23:09

James Kent