Opening a window on top of other windows

Question:

How to open a window on top of other windows when calling a function?

import wx
def openFile(wildcard="*"):
    app = wx.App(None)
    style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST
    dialog = wx.FileDialog(None, 'Open', wildcard=wildcard, style=style)
    if dialog.ShowModal() == wx.ID_OK:
        path = dialog.GetPath()
    else:
        dialog.Destroy()
        path = 'No file'
        return f'<div class="notification error">{path}</div>'
    dialog.Destroy()
    return f'<div id="pathToFile" class="notification">{path}</div>'
Asked By: Ms.kitty

||

Answers:

To show a dialog on top of some other top level window you need to specify that window as the dialog parent (instead of using None as you do).

There is no support for showing a native dialog, such as the "Open file" one, on top of all windows, this can only be done for custom windows using wx.STAY_ON_TOP flag.

Answered By: VZ.

The Accepted answer from @VZ is spot on for all normal usage but strictly speaking, your code can be tweaked to work, even if it serves no real purpose but you’ll notice, despite passing wx.STAY_ON_TOP, it will not honour it.

Like so:

import wx
def openFile(wildcard="*"):
    app = wx.App(None)
    style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST | wx.STAY_ON_TOP
    dialog = wx.FileDialog(None, 'Open', wildcard=wildcard, style=style)
    if dialog.ShowModal() == wx.ID_OK:
        path = dialog.GetPath()
    else:
        path = 'No file'
    dialog.Destroy()
    return f'<div class="notification error">{path}</div>'

print(openFile())
Answered By: Rolf of Saxony
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.