Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture the "Save your changes" dialog when using win32gui and closing an embedded application within a QApplication?

I am embedding an application within a tab in a pyqt QApplication. When I close the tab this application is embedded in how do I allow it to display the "Save your changes" dialog?

I use this on tab_close:

win32gui.PostMessage(int(wdg.process._handle),win32con.WM_CLOSE,0,0)

When I do this though, I lose this dialog box, if the application would normally throw one up.

Missing Prompt

The code looks similar to this:

class MainWindow(QTabWidget):
    def __init__(self, parent=None):
        QTabWidget.__init__(self, parent)
        self.setTabsClosable(1)
        self.tabCloseRequested.connect(self.close_tab)

    ...

    def close_tab(self,ind):
        wdg = self.widget(ind)
        win32gui.PostMessage(int(wdg.process._handle),win32con.WM_CLOSE,0,0)
        self.removeTab(ind)
        del wdg

    ...

This produces a UI like this (with Window's notepad.exe embedded). Clicking the "X" on the tab will close Notepad without prompting the user to save any input.

Embedded Notepad in a tab

How can I close the tab and allow the embedded application to prompt a user to save their changes?

like image 892
Andy Avatar asked Sep 14 '15 18:09

Andy


1 Answers

You are on a path that will only bring you pain and disillusions.

Notepad is only one of the few Windows applications that has its source code available for free.
Recompile it and modify it to fit your purposes.

You will find-out that there are also other popup dialogs that you may need to get rid of.

Now, if you really want to continue with automation for Windows you will probably need something like https://pywinauto.github.io which happens to have an example for notepad.exe.

like image 150
sorin Avatar answered Nov 10 '22 07:11

sorin