robertlugg / easygui

easygui for Python
http://easygui.readthedocs.org/en/master/
BSD 3-Clause "New" or "Revised" License
457 stars 116 forks source link

Windows 7 - dialog boxes spawn behind other windows. #76

Open LiaungYip opened 9 years ago

LiaungYip commented 9 years ago

(Cross posting from http://stackoverflow.com/questions/31223583/easygui-dialogs-are-not-given-focus-when-created .)

I am using Python 2.7.9, easygui 0.97.4, and Windows 7.

I am trying to pop up a dialog box using the following:

import easygui
easygui.msgbox()

I would expect a dialog box to pop up and come to the foreground, taking focus. Sometimes, this happens. Other times, the dialog box does not come to the front, and spawns behind all other windows.

Whether the dialog box comes to the front, or hides behind other windows, seems to depend on something unpredictable. Giving different arguments to easygui.msgbox() doesn't seem to help. Restarting the Python interpreter sometimes makes it work, and sometimes doesn't.

The issue is not so bad for simple dialogs, which at least give a flashing button in the task bar to get your attention.

enter image description here

It is worse for diropenbox() which does not spawn a flashing button in the taskbar. The dialog spawns behind a window and gives no evidence it's there, until you minimise all other windows.

enter image description here

enter image description here

Is this normal and expected behaviour? I would like easygui dialogs to always come to the front and always take focus, instead of hiding behind other windows.

JuliaScythe commented 9 years ago

Hmm. As a temporary fix you could have a message in the console (or some prominent place in your GUI if you have one) saying a file box has opened... But my windows 10 box could not replicate it...

LiaungYip commented 9 years ago

@Mrnumbers9 ; Yes, it's an intermittent issue. For me, it went away after playing with some things and rebooting.

I was also in email correspondence with a person named Stav Bar-Sheshet who was also having the same issue; and another anonymous poster on Stack Overflow, also having the issue.

JuliaScythe commented 9 years ago

Good

brombeerberg commented 8 years ago

I am still struggling with the same problem of easygui-dialogs opening in the background using Anaconda with Spyder on Windows 7 (32-bit). In my Programme, I first open a fileopenbox which is always in the background whereas the subsequent multenterbox is always in the foreground. Of course I would like to have the fileopenbox already in the foreground. As long as there is no keyword argument for the desired behaviour within easygui, one workaround could be to apply a raise_window-function as proposed on http://physicalmodelingwithpython.blogspot.de/2015/07/raising-figure-window-to-foreground.html . But what is the figure name of the easygui-dialog that I have to pass to this function?

godfoder commented 7 years ago

I have this problem in my application as well: https://github.com/idigbio/idigbio-media-appliance . In my case this is behavior is complicated by the fact that the dialog is spawned by a call to an internal web-service (the UI is an HTML based, not TK). The good news here is that diropenbox always spawns behind the browser window when called, so at least its reproducible. Filesavebox seems to always be in front of the browser though, so its not consistent across box types.

Perhaps adding something like this: http://stackoverflow.com/questions/1892339/how-to-make-a-window-jump-to-the-front

to the root window before spawning the dialog would work?

blipk commented 2 years ago

@godfoder can we add this fix to fileopenbox as well? Or perhaps theres a way to expose the localRoot so it can be done by the user?

blipk commented 2 years ago

This is still happening on windows 10.

A hackish workaround if anyone happens to be looking for this:

import ctypes
import win32gui
import win32con
from pywinauto.findwindows    import find_window

def show_window(title):
    if os.name != 'nt': return
    time.sleep(0.25)
    window = None
    done = False
    attempts = 0
    print("Searching for window with title:", title)
    while not done:
        attempts += 1
        if attempts > 50: break
        try:
            window = find_window(best_match=title)
        except:
            window = None
            continue
        if window:
            print("Found window", window)
            try:
                win32gui.SetWindowPos(window, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_SHOWWINDOW | win32con.SWP_NOMOVE | win32con.SWP_NOREPOSITION | win32con.SWP_NOSIZE)
                win32gui.SetForegroundWindow(window)
                done = True
                print("Window set to foreground")
                return True
            except Exception as e:
                print("Error bringing window to front", e)
                continue
    print("Search done with no result....")
    return False
t = threading.Thread(target=show_window,name='WindowFocus',args=['Select a video'])
t.daemon = True
t.start()
chosen = easygui.fileopenbox(default=target+'\\*.webm', title="Select a video", filetypes=[['*.mp4', '*.webm', 'Videos']])
LiaungYip commented 2 years ago

See also https://github.com/robertlugg/easygui/pull/163 .

ProsperousHeart commented 2 years ago

This is still an issue. Windows 10, python 3.10