5

I am trying to make a Toplevel widget that is the active window on the screen (I want it so that if you press Enter, it exits the window. I already have the key bound to the widget, but I can't seem to get the window to be the main window on my computer. I am running my program using Notepad++ (I have a shortcut for this specific program since I will be using it a lot).

Here is my code:

def main():
    root = Tk(className = ' Module Opener')
    app = GetFileName(root)
    root.rowconfigure(0, weight = 1)
    root.columnconfigure(0, weight = 1)
    root.bind('<Return>', (lambda e, b=app.goButton: b.invoke()))
    root.mainloop()
    f, pythonType = app.fileName, app.pythonType
    if f[-3:] != '.py': f += '.py'
    moduleFile = getFilePath(f, pythonType)
    if not moduleFile is None:
        subprocess.call([r"C:\Program Files\Notepad++\notepad++.exe", moduleFile])
    else:
        root.withdraw()
        finalRoot = Toplevel(root)
        finalRoot.grab_set() # I thought this would make it active
        finalApp = FileNotExist(finalRoot, f)
        finalRoot.rowconfigure(0, weight = 1)
        finalRoot.columnconfigure(0, weight = 1)
        finalRoot.bind('<Return>', (lambda e, b=finalApp.okButton: b.invoke()))
        finalRoot.mainloop()

I want it so that when it opens, if I press Enter, it does my command; however, I have to click in the window first so that it becomes active, and then it works.

I tried various things such as finalRoot.lift(), finalRoot.focus_set(), finalRoot.grab_set()/finalRoot.grab_set_global() (I saw these methods from another question), and finalRoot.focus().

The first window Tk() is active when the program starts. However, the Toplevel() is not. I also tried making two Tk()'s (destroying root and then creating finalRoot as a new Tk() instance), but this did not work as well. How can I do this? Thanks!

Community
  • 1
  • 1
Rushy Panchal
  • 16,979
  • 16
  • 61
  • 94

7 Answers7

7

...however, I have to click in the window first so that it becomes active, and then it works.

I just encountered this problem and while I was researching a solution, I found this thread. I'm using Windows 7 Professional. All I did was call both grab_set() and focus() and it solved the problem for me. You already have finalRoot.grab_set(), just add:

finalRoot.focus()

It worked in my case.

sedeh
  • 7,083
  • 6
  • 48
  • 65
3

I tried the above solutions and found that focus_force() alone worked on Windows Vista/Python 3.3. Also it may help to include the takefocus=True method while creating your Toplevel window.

KAG1224
  • 161
  • 1
  • 6
1

I had the same problem and tried everything I could find. Unfortunately, the answer is that it depends on your OS. My window is automatically focused on my old Mac, but not on OSX Lion. Some of the commands you list are OS-dependant, too.

JulienD
  • 7,102
  • 9
  • 50
  • 84
1

None of the above suggestions worked for me on Mac OS El Capitan, but this does:

class Window(Tk.Toplevel):
    ...
    def setActive(self):
        self.lift()
        self.focus_force()
        self.grab_set()
        self.grab_release()
    ...
0
root.call('wm', 'attributes', '.', '-topmost', True)
root.after_idle(root.call, 'wm', 'attributes', '.', '-topmost', False)
root.focus_force()
thinker3
  • 12,771
  • 5
  • 30
  • 36
  • I tested this one, too, but it did not work (OSX 10.9.2). The best I have at the moment - but does not work for everyone -, is ``self.root.wm_attributes("-topmost", 1)`` – JulienD Mar 17 '14 at 15:42
  • @muraveill Probably platform dependent, my os is Ubuntu 12.04. – thinker3 Mar 18 '14 at 01:05
0

Here is the code which worked for me

root= tk.Tk()
root.title("Main Window")
top = tk.Toplevel()
top.title("Topelevel Window")
top.grab_set()  #for disable main window
top.attributes('-topmost',True)  #for focus on toplevel
root.mainloop()
Soham Patel
  • 125
  • 5
0

I'm too late here, but if anyone's wondering a solution for python 3.9, just a focus() will work.

so,

top_window_name.focus()

that's it.

fortyseven
  • 45
  • 1
  • 10