Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close window

Status
Not open for further replies.

newjim

Programmer
Sep 8, 2001
21
CA
Is it possible to close a window or program in Python? I use Python to programmatically open a CHM in its own window by giving it the path and filename. Later I want to close that CHM. I would like to do that by somehow accessing it from Python and closing it. Is that possible?

Thanks.
 
I do believe that you can accomplish what you are asking with the win32com module. I have only done it with excel applications so far. The code will be something like this.

To Open:
file_to_open=win32com.client.Dispatch("Excel.Application")#"Excel.Application" would be changed to what kind of application you are starting.
file_Opened=file_to_open.Workbooks.Open(name of file)
visible=1 #To make it to where it opens up in a window. Left out and the program will open in the background only.

To Close:
file_to_open.Workbooks.Close()


 
BBelt is right if the application exposes a COM interface. Depends on the app.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Code:
PythonWin 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on win32.
Portions Copyright 1994-2004 Mark Hammond (mhammond@skippinet.com.au) - see 'Help/About PythonWin' for further copyright information.
>>> import win32gui
>>> import win32help
>>> helpfile = r'E:\WINDOWS\Help\ntcmds.chm'
>>> hwnd = win32gui.GetActiveWindow()
>>> a = win32help.HtmlHelp(hwnd, helpfile, win32help.HH_DISPLAY_TOC, None)
>>> # help file should now be displayed on top of active window
>>> win32help.HtmlHelp(hwnd, '', win32help.HH_CLOSE_ALL, None)
0
>>> # help window should now be closed
>>>
 
The code by JustinEzequiel causes a CHM to briefly display on my screen but not stay open (I commented out the last line, that close it). I've looked at the win32help module on ActiveState, and the code looks correct. It should bring up the CHM, but it should not close. Am I missing something here?

Thanks
 
Actually, it does work from the interactive shell. So with some refining, it should work from a Python program run on the command line. Thanks JustinEzequiel.
 
The problem is that the CHM that you launch from the Python program closes when the Python program ends.

Here's a solution: cause the Python program to pause, then call the Python program again.

For example, in a program called close_chm.py...

import win32gui
import win32help
import os
helpfile = r'C:\python\test.chm'
hwnd = win32gui.GetActiveWindow()

a = win32help.HtmlHelp(hwnd, helpfile, win32help.HH_DISPLAY_TOC, None)
# help file should now be displayed on top of active window
# Cause execution to pause while you read the CHM
os.system("pause")
# Close the program...
win32help.HtmlHelp(hwnd, '', win32help.HH_CLOSE_ALL, None)
# ...and start it again.
os.system(r"C:\python\close_chm.py")
'''
If Windows caches the CHM, give it a distinct name, perhaps
by appending a timestamp to it, and pass the file name in
as a command line argument.
'''
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top