Reputation: 55
When I import pyautogui as pag on my gui code, the window size change. This becomes an issue when I use two different monitors which have different resolutions. I wanted to know if there is a way to just disable pyautogui from doing any form of resizing? The example bellow only works when I start it when my second monitor is not connected so I am only using my laptop monitor which has a resolution 1920 x 1200. The second monitor has a higher resolution, and for it there is not issue, even when I drag the tkinter window to the laptop monitor, there is still no issue. This issue only seems to happen when I start the program without having my second monitor connected. The button in the example is meant to show how the window changes when pyautogui is imported.
from tkinter import *
win = Tk()
win.geometry("1270x725+0+0")
win.minsize(width=1270, height=600)
win.maxsize(width=1270, height=600)
def test():
import pyautogui as pad
button = Button(win, command=test)
button.place(relx=.5, rely=.5)
win.mainloop()
Upvotes: 2
Views: 1939
Reputation: 36
This problem occurs from C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyautogui _pyautogui_win.py SetProcessDpiAwareness
.
So the method to solve this problem is like this:
import ctypes
ctypes.windll.shcore.SetProcessDpiAwareness(0)
from tkinter import *
win = Tk()
win.geometry("1270x725+0+0")
win.minsize(width=1270, height=600)
win.maxsize(width=1270, height=600)
def test():
import pyautogui as pad
button = Button(win, command=test)
button.place(relx=.5, rely=.5)
win.mainloop()
Upvotes: 0
Reputation: 54668
This is a complicated issue. I'm assuming you are running on Windows, because I don't think the issue would occur anywhere else.
The problem here has to do with high DPI monitors. Windows knows that most people don't like reading teeny tiny pixels and little tiny fonts on high-DPI monitors, so it lies to applications about the actual dot resolution. That produces the best results in most cases, but some applications want to know the exact DPI so they can take their own appropriate action. Windows calls this a "high DPI aware application".
On Windows, the first thing pyautogui
does is call the SetProcessDPIAware()
API, which tells Windows you don't want any remapping. That changes the meaning of the pixel coordinates you get on your high DPI monitor.
I can see two practical workarounds. One, you could tweak _pyautogui_win.py
in the pyautogui module to delete that call. That's going to have some effect on the internals of pyautogui, but it's not clear what. Two, you could call SetProcessDPIAware()
early in your own code. That would put the burden on YOU to decide how to handle windows that exist on monitors of different resolution.
Upvotes: 3