0

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.

Gabe Morris
  • 804
  • 4
  • 21
  • 1
    How about focus_force and grab_set –  Jun 15 '21 at 12:26
  • The window still doesn't focus with those additional methods. @Sujay – Gabe Morris Jun 15 '21 at 12:30
  • 1
    try increasing the delay to maybe 3 second (3000) in your first attempt or sth more than 1 ms although now looking at it, that is probably not the issue – Matiiss Jun 15 '21 at 12:56
  • 1
    have a look at [this](https://stackoverflow.com/q/2090464/15993687) and [this](https://pypi.org/project/PyGetWindow/). – Art Jun 15 '21 at 12:59
  • I have tried that before too and still no luck @Matiiss – Gabe Morris Jun 15 '21 at 13:32
  • Those would probably work, but I'm limited to the standard library at the moment. Also, I have strong feelings for downloading a huge package such as pywin32 for one line of code. @Art – Gabe Morris Jun 15 '21 at 13:33
  • 1
    I don't think that would be possible, read [1](https://stackoverflow.com/q/26323767/15993687) also have you tried [this](https://stackoverflow.com/a/47161643/15993687)? – Art Jun 15 '21 at 13:36
  • I think you're right in how it's not possible with the standard library. That second link is the same one that I referenced in my post. Thanks! @Art – Gabe Morris Jun 15 '21 at 14:10
  • 1
    Have you tried the lift() method? Jk – NTSwizzle Jun 15 '21 at 14:53
  • Yes. See my attempts in my snippet. That method lifts the window to the front, but only when the main application window is on the foreground. The issue here is that when I open the file, my app loses focus. @NTSwizzle – Gabe Morris Jun 15 '21 at 16:03

1 Answers1

0

As you stated you wanted to set focus on the input field

inputfield.focus_set()

thats all

MrHola21
  • 301
  • 2
  • 8
  • This does not fix the issue. See attempt number 3 where I used that method. You still have to navigate back to the app through the windows manager after opening the file. – Gabe Morris Jun 15 '21 at 12:13