Make a Tkinter Toplevel active

前端 未结 6 1346
灰色年华
灰色年华 2020-12-10 08:57

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 h

相关标签:
6条回答
  • 2020-12-10 09:18

    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.

    0 讨论(0)
  • 2020-12-10 09:20

    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.

    0 讨论(0)
  • 2020-12-10 09:21

    ...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.

    0 讨论(0)
  • 2020-12-10 09:30
    root.call('wm', 'attributes', '.', '-topmost', True)
    root.after_idle(root.call, 'wm', 'attributes', '.', '-topmost', False)
    root.focus_force()
    
    0 讨论(0)
  • 2020-12-10 09:35

    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()
    
    0 讨论(0)
  • 2020-12-10 09:43

    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 讨论(0)
提交回复
热议问题