asweigart / pyautogui

A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
BSD 3-Clause "New" or "Revised" License
10.22k stars 1.24k forks source link

importing pyautogui breaks tkinter Checkbutton #635

Closed GabeMillikan closed 2 years ago

GabeMillikan commented 2 years ago

Code:

import tkinter as tk
from tkinter import ttk
import pyautogui

w = tk.Tk()
ttk.Checkbutton(w, text = "Checkbox").grid()
w.mainloop()

Result: KwUqCTWl58

New code after removing pyautogui import:

import tkinter as tk
from tkinter import ttk
#import pyautogui

w = tk.Tk()
ttk.Checkbutton(w, text = "Checkbox").grid()
w.mainloop()

Result: apXWoyEXIc

I am on 64bit Windows 11 and using Python 3.8.10, but I'm not sure that this is platform specific. Would love for people on other systems to check if the same issue occurs.

GabeMillikan commented 2 years ago

P.S. If I use .pack() instead of .grid(), then the checkbox starts out small, whereas with .gird() you have to hover over it for it to become small.

GabeMillikan commented 2 years ago

I actually located the problem. pyautogui imports pyscreeze, which executes this code:

import ctypes
ctypes.windll.user32.SetProcessDPIAware()

Which is the cause of this bug. I now believe that this is actually a Tkinter bug, and not pyscreeze nor pyautogui's problem (since the ctypes code is required for them to take accurate screenshots).

macdeport commented 2 years ago

The size of window seems the cause of this "bug": superposition of checkbox area and resize handle... This code works perfectly (Mac OS X / Python 3.9.6):

import tkinter as tk
from tkinter import ttk
import pyautogui

w = tk.Tk()
f = tk.Frame()
w.title('Better')
w.geometry('150x40') # sufficient window size
ttk.Checkbutton(f, text = "Checkbox").pack()
f.grid()
w.mainloop()