# DPI-aware Tkinter + Matplotlib (Consistent Across IDEs and Standalone)
I tried the suggested one, did not work in some cases. Below is slightly heavy but works in every situation and has fine control over every element.
Get DPI and Screen Info
def get_display_context():
import tkinter as tk
root = tk.Tk()
root.withdraw()
root.update_idletasks()
root.state('zoomed')
root.update()
dpi = root.winfo_fpixels("1i")
width, height = root.winfo_width(), root.winfo_height()
root.destroy()
return {
"dpi": dpi,
"screen_width_pixel": width,
"screen_height_pixel": height
}
Define Font Helpers
**Tkinter:**
def font_ui_tk(size_pt, ctx, font="Segoe UI", bold=False):
scale = ctx["dpi"] / 96
return (font, int(size_pt * scale), "bold" if bold else "normal")
**Matplotlib:**
def font_ui_mpl(size_pt, ctx, font="Segoe UI", bold=False):
scale = ctx["dpi"] / 96
return {
"fontsize": int(size_pt * scale),
"fontweight": "bold" if bold else "normal",
"family": font
}
### Step 3: Apply in Your App
**Tkinter Example:**
ctx = get_display_context()
label = tk.Label(root, text="Run", font=font_ui_tk(11, ctx))
**Matplotlib Example:**
ax.set_title("Plot Title", **font_ui_mpl(12, ctx))
**Legend/Ticks:**
ax.legend(fontsize=font_ui_mpl(9, ctx)["fontsize"])
ax.tick_params(labelsize=font_ui_mpl(9, ctx)["fontsize"])
This method removes the need for `tk scaling` or `ctypes.windll.shcore.SetProcessDpiAwareness`.