I'm an amateur coder and I'm trying to resize the chrome application by pressing a key on my keyboard by using python. I want my code to be able to resize the window to (300, 1080) and (1920, 300), as well as another key to return to fullscreen. I started researching online and most sources said to use selenium to do this, but using their exact code would just open a new chrome application rather than resize the current one. Other sources told me to use pywinauto, win32gui, or pygetwindow but again all of the code they provided either did nothing or opened a new window.
I tried this but it said module 'selenium.webdriver' has no attribute 'set_window_size'
from selenium import webdriver
webdriver.set_window_size(300, 1080)
size = webdriver.get_window_size()
print("Window size: width = {}px, height = {}px.".format(size["width"], size["height"]))
I also tried this but it opened a new window rather than resize the current one
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.google.com/chrome/")
driver.set_window_size(300, 1080)
Out of desperation I asked chatGPT but it said that its pretty hard to do this but I thought I would at least reach out to some ppl who actually know what their doing before I give up. If anyone could help it would be much appreciated
Took a minute to piece this together but the results are satisfying. The following code uses the keyboard library to bind CTRL+ALT+W to the resize_window function. The function locates the active window and alternates resizing between fullscreen and a smaller size.
import keyboard
import win32con # part of pywin32
import win32gui # part of pywin32
def resize_window():
"""
Locates the active window. If the window is 1080x300,
makes the window fullscreen. Otherwise, resizes to 1080x300
"""
hwnd = win32gui.GetForegroundWindow()
if not hwnd:
return
rect = win32gui.GetWindowRect(hwnd)
w = rect[2] - rect[0]
h = rect[3] - rect[1]
small_width = 1080
small_height = 300
if not (w==small_width and h==small_height):
win32gui.ShowWindow(hwnd, win32con.SW_NORMAL)
win32gui.MoveWindow(hwnd, -7, 0, small_width, small_height, True)
else: # make fullscreen
win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
def main():
resize_hotkey = 'ctrl+alt+w' # any hotkey you want
keyboard.add_hotkey(resize_hotkey, resize_window, suppress=True)
keyboard.wait('ctrl+alt+q') # hotkey to quit the script
if __name__ == "__main__":
main()
I wasn't able to find convenience functions for these operations in the pywinauto library, so I used pywin32 (which includes the win32gui module). This library provides direct access to many Windows API calls; you should be able to do almost anything if you refer to the Windows API documentation.
I'll leave it up to you to implement toggling between 3 sizes through use of an additional hotkey. You can also target only Chrome by finding the process named "chrome.exe".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With