Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep the window focus on new Toplevel() window in Tkinter?

I am currently writing a win32gui python27 application (I'm running win7). I am wondering if it is possible to create a new window from my mainloop() and KEEPING the focus on it, possibly by stopping the mainloop and deactivating the root window. In a sort of pseudo code example:

root=Tk()
#put in some widgets, such as statusbars, tkfileDialog widgets etc...
O=Toplevel()
OptionMenu(O) #wait for user to make his choices; btw: OptionMenu is a class...
tkFileDialog.askdirectory(...) #THEN interpret this line

Basically, I'd like to achieve what most of the widgets in tkfiledialog and tksimpledialog do:
To steal the focus from the main window (not just the console focus, the wm focus) and to resume the mainloop until AFTER everything in, for example, OptionMenu has been resolved. I hope I could make my goals clear to y'all, I just started Tkinter programming a couple of weeks ago and may confuse and misinterpret some concepts behind it....
That's all, folks!

like image 666
Sacha Viquerat Avatar asked Apr 11 '13 08:04

Sacha Viquerat


People also ask

What does focus () do tkinter?

Build A Paint Program With TKinter and Python Sometimes, we need to change or modify the focus of any widget in the application which can be achieved by using the focus_set() method. This method sets the default focus for any widget and makes it active till the execution of the program.

How do I handle the window close event in tkinter?

To handle the window close event in Tkinter with Python, we call the root. protocol method with the 'WM_DELETE_WINDOW' event. to call root. protocole with "WM_DELETE_WINDOW" to add a close window handler.

How do I use multiple windows in tkinter?

First, define a class Window that inherits from the Toplevel window. The Window will be closed once the Close button is clicked. Third, in the open_window() method, create a new instance of the Window and call the grab_set() method so that it can receive events.

What does toplevel mean in tkinter?

The Toplevel widget is used to create and display the toplevel windows which are directly managed by the window manager. The toplevel widget may or may not have the parent window on the top of them.


1 Answers

The concept you are looking for is called a "grab". Tkinter supports grabs with several methods. For example, to set a local grab on a toplevel you would do my_window.grab_set(). A local grab is where this window grabs the focus from all other windows in your app, but only for your app.

You can also do a global grab, which effectively freezes your entire display except for your specific window. This is very dangerous since you can easily lock yourself out of your own computer if you have a bug in your code.

Grabs do not deactivate the mainloop function. This must be running for your window to process events. It simply redirects all events to the window that has the grab.

For more information, read about grab_set and other grab commands here: http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.grab_set-method

like image 113
Bryan Oakley Avatar answered Oct 16 '22 07:10

Bryan Oakley