Matplotlib/tkinter geometry management

Hello… it’s perfectly possible that I’m being stupid here, since I’m a relative novice at tkinter, but I’m having difficulty getting geometry management to work as expected. Here’s some example code (cut down from a much more complicated GUI I’m trying to create)

import tkinter
import matplotlib.pyplot as plt
import matplotlib.backends.backend_tkagg as tkagg


hamlet = '''To be, or not to be, that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles
And by opposing end them. To die—to sleep,
No more; and by a sleep to say we end
The heart-ache and the thousand natural shocks
That flesh is heir to: 'tis a consummation
Devoutly to be wish'd. To die, to sleep;
To sleep, perchance to dream—ay, there's the rub:
For in that sleep of death what dreams may come,
When we have shuffled off this mortal coil,
Must give us pause—there's the respect
That makes calamity of so long life.
For who would bear the whips and scorns of time,
Th'oppressor's wrong, the proud man's contumely,
 The pangs of dispriz'd love, the law's delay,
The insolence of office, and the spurns
That patient merit of th'unworthy takes,
When he himself might his quietus make
With a bare bodkin? Who would fardels bear,
To grunt and sweat under a weary life,
But that the dread of something after death,
The undiscovere'd country, from whose bourn
No traveller returns, puzzles the will,
And makes us rather bear those ills we have
Than fly to others that we know not of?
Thus conscience doth make cowards of us all,
And thus the native hue of resolution
Is sicklied o'er with the pale cast of thought,
And enterprises of great pith and moment
With this regard their currents turn awry
And lose the name of action.'''


def main():

    root = tkinter.Tk()

    label = tkinter.Label(root, text=hamlet)
    label.grid(row=0, column=0, sticky='nsew')

    fig = plt.figure()
    canvas = tkagg.FigureCanvasTkAgg(fig, master=root)
    canvas.get_tk_widget().grid(row=0, column=1, sticky='nsew')

    root.mainloop()


if __name__ == '__main__':
    main()

When I run this the window comes out taller than it does if I create either the Label widget or the Canvas alone, resulting in spurious space being left above and below the Label.

Any help would be appreciated!

Cheers,
John

What actually happens…


What I’d expected…

Ah!

This problem only occurs on Windows with a custom scaling factor set (right-click desktop, Display settings, Advanced scaling settings, Custom scaling, 120 (for example)).

I think it’s a matplotlib issue, rather than a tkinter one, because the problem doesn’t occur unless there’s a FigureCanvasTkAgg in the grid.

You don’t want to be using pyplot as that initializes Matplotlib to run Tk itself. See Embedding in Tk — Matplotlib 3.8.2 documentation

Yes, on Windows, Matplotlib enables HiDPI scaling if it is managing Tk windows.

Thank you! I read the link you gave and fixed that issue, both in my real application and my simplified example which now reads…

import tkinter
import matplotlib.figure
import matplotlib.backends.backend_tkagg as tkagg


hamlet = '''To be, or not to be, that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles
And by opposing end them. To die—to sleep,
No more; and by a sleep to say we end
The heart-ache and the thousand natural shocks
That flesh is heir to: 'tis a consummation
Devoutly to be wish'd. To die, to sleep;
To sleep, perchance to dream—ay, there's the rub:
For in that sleep of death what dreams may come,
When we have shuffled off this mortal coil,
Must give us pause—there's the respect
That makes calamity of so long life.
For who would bear the whips and scorns of time,
Th'oppressor's wrong, the proud man's contumely,
 The pangs of dispriz'd love, the law's delay,
The insolence of office, and the spurns
That patient merit of th'unworthy takes,
When he himself might his quietus make
With a bare bodkin? Who would fardels bear,
To grunt and sweat under a weary life,
But that the dread of something after death,
The undiscovere'd country, from whose bourn
No traveller returns, puzzles the will,
And makes us rather bear those ills we have
Than fly to others that we know not of?
Thus conscience doth make cowards of us all,
And thus the native hue of resolution
Is sicklied o'er with the pale cast of thought,
And enterprises of great pith and moment
With this regard their currents turn awry
And lose the name of action.'''


def main():

    root = tkinter.Tk()

    label = tkinter.Label(root, text=hamlet)
    label.grid(row=0, column=0, sticky='nsew')

    fig = matplotlib.figure.Figure(figsize=(5, 4), dpi=100)
    canvas = tkagg.FigureCanvasTkAgg(fig, master=root)
    canvas.draw()
    canvas.get_tk_widget().grid(row=0, column=1, sticky='nsew')

    root.mainloop()


if __name__ == '__main__':
    main()

But his hasn’t changed the behaviour.

If I set a 125% scaling directly in the Display settings dialog box (screenshot 1), I get correct geometry management but blurry text (screenshot 2).

If, however, I instead press “Advanced scaling settings” and set a 125% scaling in that sub-dialog box (screenshot 3) I get clear text but wrong geometry management.




Weird!