API Reference
Tk Class
The Tk class is a drop-in replacement for tkinter.Tk with DPI awareness capabilities.
from tkinter_unblur import TkAttributes
| Attribute | Type | Description |
|---|---|---|
dpi_x | int | None | Horizontal DPI (96 = 100% scaling). None on non-Windows platforms. |
dpi_y | int | None | Vertical DPI (96 = 100% scaling). None on non-Windows platforms. |
dpi_scaling | float | Scaling factor (1.0 = 100%, 1.5 = 150%, etc.). |
Example:
root = Tk()
print(f"DPI: {root.dpi_x}x{root.dpi_y}")
print(f"Scaling: {root.dpi_scaling:.0%}")
# Output: Scaling: 150%Methods
scale_value
Scale a numeric value according to the current DPI scaling factor.
def scale_value(self, value: float | str) -> intArguments:
value: The value to scale (can be int, float, or numeric string).
Returns:
- The scaled value as an integer.
Example:
from tkinter import Label
root = Tk()
# Tk does not scale pixel values, such as padding, so scale them yourself
label = Label(root, text="Hello", font=("Arial", 12))
label.pack(padx=root.scale_value(10), pady=root.scale_value(10))Font sizes in points, such as ("Arial", 12), need no scaling: Tk already sizes them for the display's DPI. Scaling them again with scale_value() makes the text too large.
scale_geometry
Scale a geometry string according to the current DPI scaling factor.
def scale_geometry(self, geometry: str) -> strArguments:
geometry: A Tkinter geometry string in any formroot.geometry()accepts:
| Form | Example |
|---|---|
"WxH" | "800x600" |
"WxH+X+Y", each sign + or - | "800x600+100+50", "800x600-100-50" |
"+X+Y", each sign + or - | "+100+50" |
Each form can start with "=", as in "=800x600+100+50".
Returns:
- The scaled geometry string, in the same form as the input. Signs and a leading
"="are kept.
Raises:
ValueError: If the string is not one of these forms, for example"800x600+100". The message starts withInvalid geometry string format:.
Example:
root = Tk()
root.geometry(root.scale_geometry("800x600")) # "1200x900" at 150%
root.geometry(root.scale_geometry("800x600+100+50")) # "1200x900+150+75" at 150%Exceptions
The library defines the following exceptions in tkinter_unblur.exceptions.
UnsupportedPlatformError and DPIDetectionError are reserved for future use. The library does not currently raise them; it falls back to default values instead (see below).
TkinterUnblurError
Base exception for this library's custom exceptions.
UnsupportedPlatformError
Reserved. Not currently raised.
On non-Windows platforms, Tk skips DPI detection and sets dpi_x = None, dpi_y = None and dpi_scaling = 1.0.
DPIDetectionError
Reserved. Not currently raised.
On Windows, if DPI detection fails, the library logs a warning through the logging module (logger tkinter_unblur._windows) and sets dpi_x = 96, dpi_y = 96 and dpi_scaling = 1.0 (100% scaling). This happens when:
shcore.dlloruser32.dllcannot be loaded- The monitor for the window cannot be determined
GetDpiForMonitorreturns an error code or fails