Search code examples
pythontkinterimportresolutionpywinauto

While importing pywinauto in Tkinter window size changes


Here's a snippet of my test code

from tkinter import *

master=Tk()
master.geometry('640x340')
Button(master,text='check').pack()

def new():
    print('done')
    from pywinauto import Desktop, Application

master.after(1000,new)
master.mainloop()

In exactly one second the tkinter window which is created reduces in size, same thing happens for the button as well. This is just test code which I wrote after tracking down this problem in the application I am trying to build. Could somebody tell me why the window changes in size, and what I can do to prevent this?

I have checked all I could, the official documentation only mentions Tkinter once and that is referring to pywinauto’s backend, and I don't think it is relevant. Here on page 5.


Solution

  • This problem occurs from C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\pywinauto **win32functions.py** SetProcessDpiAwareness.

    So the method to solve this problem is like this:

    import ctypes
    ctypes.windll.shcore.SetProcessDpiAwareness(0)
    from tkinter import *
    
    master=Tk()
    master.geometry('640x340')
    Button(master,text='check').pack()
    
    def new():
        print('done')
        from pywinauto import Desktop, Application
    
    master.after(1000,new)
    master.mainloop()