17

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?

Andy
  • 49,085
  • 60
  • 166
  • 233
  • Is there any difference if you change `WM_CLOSE` to `WM_QUIT`? – Steven Rumbalski Sep 14 '15 at 19:06
  • @StevenRumbalski, according to [this](http://stackoverflow.com/questions/3155782/what-is-the-difference-between-wm-quit-wm-close-and-wm-destroy-in-a-windows-pr): Yes. But, in my case, both eat the "Save?" dialog – Andy Sep 14 '15 at 19:09
  • 1
    is there a way you can get the return code from the application? my guess is that you post the close message (asynchronously), you then close the tab (which is the parent for the other application, and therefore the 'save' dialog as well), which forces both windows to close. I couldn't say for sure, but that's what i would look at first. – Corley Brigman Sep 24 '15 at 13:46

1 Answers1

1

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.

Ulf Gjerdingen
  • 1,414
  • 3
  • 16
  • 20
sorin
  • 161,544
  • 178
  • 535
  • 806
  • This may work for Notepad, but what about other applications? That is the ultimate goal here and Notepad provides a simple test case – Andy May 29 '16 at 14:04