0

I'd like to use the tab key in both showing of the following code:

from tkinter import *
main = Tk() 
def pressButton():
        main.destroy()
End=Button(main,text='Finished',width=15,command=pressButton).grid()
main.mainloop()

from tkinter import *
main = Tk() 
def pressButton():
        main.destroy()
End=Button(main,text='Finished',width=15,command=pressButton).grid()
main.mainloop()

first window works: i can press tab and space and it opens the second window; there i'm not able to "press button" by using tab and space, because the cursor is in Python Shell. How can I get the cursor in the second window?

  • Window focus is handled by your window manager, I'd be surprised if tkinter (or you) could influence where the keyboard focus goes. – Aran-Fey Feb 25 '17 at 14:59
  • 1
    You shouldn't be creating one tkinter program that creates two instances of `Tk`, so the question doesn't make much sense. If you want more than one window you should create instances of `Toplevel` to create additional windows. – Bryan Oakley Feb 25 '17 at 15:24

1 Answers1

0

As mention in that post "Tkinter main window focus", it is possible to force the focus to the main window.

Solution - add a call to the after() to focus_force().

from tkinter import *
main = Tk() 
def pressButton():
    main.destroy()
End=Button(main,text='Finished',width=15,command=pressButton).grid()
# call the focus_force() after the window is displayed
main.after(1, lambda: main.focus_force())
main.mainloop()
Community
  • 1
  • 1
J. Piquard
  • 1,665
  • 2
  • 12
  • 17