In my app, I have a callback that simply opens a file. The problem is that once python opens that file, my application loses focus. This behavior is going to really slow down my process. Here is an example of my issue:
import os
from tkinter import *
class App(Frame):
def __init__(self, *args, **kwargs):
Frame.__init__(self, *args, **kwargs)
Button(self, text='Open File', bg=self['bg'],
command=lambda: self.open_file('path_to_movie_or_PDF_or_anything_that_launches_application'),
relief=GROOVE).pack(padx=10, pady=10)
self.entry_field = Entry(self, bg=self['bg'], width=20)
self.entry_field.pack(padx=10, pady=10)
self.entry_field.focus()
def open_file(self, some_path):
os.startfile(some_path)
self.handle_focus()
def handle_focus(self):
# ATTEMPT 1
# self.master.after(1, lambda: self.master.focus_force())
# self.entry_field.focus()
# ATTEMPT 2
# self.master.attributes("-topmost", True)
# self.master.lift()
# self.entry_field.focus()
# ATTEMPT 3
# self.master.focus_set()
# self.entry_field.focus_set()
# ATTEMPT 4
# self.master.focus_force()
# self.master.lift()
# self.master.update()
# self.entry_field.focus()
pass
if __name__ == '__main__':
root = Tk()
root.config(bg='white')
App(root, bg='white').pack()
root.mainloop()
I am trying to open the file with the push button, then immediately turn the focus to the entry bar. How can this be achieved?
I have already looked to Tkinter main window focus for the answer, but focus_force() is not fixing the issue although it's function is exactly what I need. The documentation for focus_force() states that "Direct input focus to this widget even if the application does not have the focus. Use with caution!" For some reason, this does not work.