Hi folks, I’ve been writing a program using both Tkinter and matplotlib in a Jupyter Notebook in VS Code, and noticed strange (possibly unintended) behavior when switching the matplotlib backend to Qt via %matplotlib qt.
With the following code, I see that without using %matplotlib qt, I get some incorrect results about monitor resolution and DPI (the monitor is 28"@4K, should have a DPI of 157).
import ctypes
from tkinter import Tk
root = Tk()
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
print(width, height)
print(root.winfo_fpixels('1i'))
Output:
1920 1080
96.0
If I then run this code:
import ctypes
from tkinter import Tk
%matplotlib qt
root = Tk()
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
print(width, height)
print(root.winfo_fpixels('1i'))
Output:
3840 2160
192.0
I don’t even need to import matplotlib at all into my code, simply writing that line changes the output.
I ran the following code in a terminal:
from tkinter import Tk
import matplotlib
matplotlib.use('qtagg')
root = Tk()
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
print(width, height)
print(root.winfo_fpixels('1i'))
Output:
1920 1080
96.0
and as you can see, I got a different result. Something tells me that there is a discrepancy between using %matplotlib qt and using matplotlib.use('qtagg'), even though the documentation does not indicate that there should be any difference.
Does anyone know what is happening here?