It seems like the window title might not be accurately reflected in the script. Windows applications often have dynamic or partial titles, or sometimes additional text in the title bar. You can use tools like pygetwindow to list all current window titles to find the correct one.
Here is an enhanced version of the script which will list all the window titles currently open on your system. This should help in identifying the exact window title of your application.
First, list all current windows:
List Windows Script:
import pygetwindow as gw
windows = gw.getAllWindows()
for window in windows:
print(window.title)
This script will print out all the window titles. Run this script to locate the exact title of the CodeTantra application window.
Once you have identified the correct window title, modify your force_window_mode function to use the accurate title.
Example after identifying the correct title:
import time
from pywinauto import Application
import pygetwindow as gw
# Function to resize and move the application window
def force_window_mode(app_title):
time.sleep(5) # Give some time for the application to fully load
try:
# Find the application window by title
windows = gw.getWindowsWithTitle(app_title)
if windows:
win = windows[0] # Assuming only one window with this title exists.
# Use pywinauto to manipulate the window
app = Application().connect(handle=win._hWnd)
window = app.window(handle=win._hWnd)
# Restore the window if it is minimized or maximized
if window.is_minimized():
window.restore()
elif window.is_maximized():
window.restore()
# Resize to a specific width and height
window.move_window(x=100, y=100, width=800, height=600)
print("Window resized and moved successfully.")
else:
print(f"No window found with the title: {app_title}")
except Exception as e:
print(f"An error occurred: {e}")
# Replace "Exact Window Title" with the actual title of your application window identified in the previous script run
app_title = "Exact Window Title"
force_window_mode(app_title)
Steps:
- Run the List Windows Script to identify the correct title of the CodeTantra window.
- Update the
app_title in the window manipulation script with the exact title you found.
- Run the updated script to resize and move the window.
By accurately identifying the window title, you should be able to manipulate it successfully.