How to minimize a specific window in Python
Asked Answered
C

2

3

Hello I am new to Python and do not know how to minimize a specific window in this case Microsoft Word 2010 all I can minimize is the Python Shell. Here is my code just in case you need it.

import win32gui, win32con
import os
import math
import time

M=6
Minimize = win32gui.GetForegroundWindow()

print("Program Started on "+time.ctime())

while M >0:
time.sleep(1)
print(M," more seconds until Word is opened")
M -=1

time.sleep(1)    
os.startfile("C:\Documents and Settings\All Users\Start Menu\Programs\MicrosoftOffice\Microsoft Word 2010")
print("Microsoft Word 2010 opened "+time.ctime())

time.sleep(2)
win32gui.ShowWindow(Minimize, win32con.SW_MINIMIZE)
Caballero answered 23/8, 2014 at 22:6 Comment(1)
It seems that you forgot to ask a question. Please take a tour.Muncey
L
10

Well - the error in this code is that at the time you run win32gui.GetForegroundWindow(), there is no MS Word Window, and the current foreground window is probably the Python shell.

Try :

time.sleep(2)
Minimize = win32gui.GetForegroundWindow()
win32gui.ShowWindow(Minimize, win32con.SW_MINIMIZE)

I think it would be better if you used some other function to select the right window - not depending on timeout. Not sure, but FindWindow looks like what you need.

Lichenin answered 24/8, 2014 at 0:36 Comment(1)
The hyperlink is not openingXhosa
O
0

Not saying it's the best way to solve this but if you put:

Minimize = win32gui.GetForegroundWindow()

at the bottom, it should work the way it was intended.

Obligor answered 19/6 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.