|
| 1 | +--- |
| 2 | +sidebar_position: 2 |
| 3 | +--- |
| 4 | + |
| 5 | +# API Reference |
| 6 | + |
| 7 | +## `Tk` Class |
| 8 | + |
| 9 | +The `Tk` class is a drop-in replacement for `tkinter.Tk` with DPI awareness capabilities. |
| 10 | + |
| 11 | +```python |
| 12 | +from tkinter_unblur import Tk |
| 13 | +``` |
| 14 | + |
| 15 | +### Attributes |
| 16 | + |
| 17 | +| Attribute | Type | Description | |
| 18 | +|-----------|------|-------------| |
| 19 | +| `dpi_x` | `int` \| `None` | Horizontal DPI (96 = 100% scaling). `None` on non-Windows platforms. | |
| 20 | +| `dpi_y` | `int` \| `None` | Vertical DPI (96 = 100% scaling). `None` on non-Windows platforms. | |
| 21 | +| `dpi_scaling` | `float` | Scaling factor (1.0 = 100%, 1.5 = 150%, etc.). | |
| 22 | + |
| 23 | +**Example:** |
| 24 | + |
| 25 | +```python |
| 26 | +root = Tk() |
| 27 | +print(f"DPI: {root.dpi_x}x{root.dpi_y}") |
| 28 | +print(f"Scaling: {root.dpi_scaling:.0%}") |
| 29 | +# Output: Scaling: 150% |
| 30 | +``` |
| 31 | + |
| 32 | +### Methods |
| 33 | + |
| 34 | +#### `scale_value` |
| 35 | + |
| 36 | +Scale a numeric value according to the current DPI scaling factor. |
| 37 | + |
| 38 | +```python |
| 39 | +def scale_value(self, value: float | str) -> int |
| 40 | +``` |
| 41 | + |
| 42 | +**Arguments:** |
| 43 | +- `value`: The value to scale (can be int, float, or numeric string). |
| 44 | + |
| 45 | +**Returns:** |
| 46 | +- The scaled value as an integer. |
| 47 | + |
| 48 | +**Example:** |
| 49 | + |
| 50 | +```python |
| 51 | +from tkinter import Label |
| 52 | + |
| 53 | +root = Tk() |
| 54 | +# Scale font size to maintain physical size across different DPI settings |
| 55 | +label = Label(root, text="Hello", font=("Arial", root.scale_value(12))) |
| 56 | +``` |
| 57 | + |
| 58 | +#### `scale_geometry` |
| 59 | + |
| 60 | +Scale a geometry string according to the current DPI scaling factor. |
| 61 | + |
| 62 | +```python |
| 63 | +def scale_geometry(self, geometry: str) -> str |
| 64 | +``` |
| 65 | + |
| 66 | +**Arguments:** |
| 67 | +- `geometry`: A Tkinter geometry string in format `"WxH+X+Y"`. |
| 68 | + |
| 69 | +**Returns:** |
| 70 | +- The scaled geometry string. |
| 71 | + |
| 72 | +**Example:** |
| 73 | + |
| 74 | +```python |
| 75 | +root = Tk() |
| 76 | +# Scale window geometry (width x height + x + y) |
| 77 | +root.geometry(root.scale_geometry("800x600+100+50")) |
| 78 | +``` |
| 79 | + |
| 80 | +## Exceptions |
| 81 | + |
| 82 | +The library defines the following exceptions in `tkinter_unblur.exceptions`: |
| 83 | + |
| 84 | +### `TkinterUnblurError` |
| 85 | +Base exception for all errors raised by this library. |
| 86 | + |
| 87 | +### `UnsupportedPlatformError` |
| 88 | +Raised when attempting to use Windows-specific features on a non-Windows platform (though the main `Tk` class handles this gracefully by disabling DPI features). |
| 89 | + |
| 90 | +### `DPIDetectionError` |
| 91 | +Raised when DPI detection fails on Windows. |
0 commit comments