|
| 1 | +import tkinter |
| 2 | +from typing import List, Optional, Tuple, TypeVar, Union, overload |
| 3 | +from typing_extensions import Literal, TypedDict |
| 4 | + |
| 5 | +NORMAL: Literal["normal"] |
| 6 | +ROMAN: Literal["roman"] |
| 7 | +BOLD: Literal["bold"] |
| 8 | +ITALIC: Literal["italic"] |
| 9 | + |
| 10 | +def nametofont(name: str) -> Font: ... |
| 11 | + |
| 12 | +# _TkinterSequence[T] represents a sequence that tkinter understands. It |
| 13 | +# differs from typing.Sequence[T]. For example, collections.deque a valid |
| 14 | +# Sequence but not a valid _TkinterSequence: |
| 15 | +# |
| 16 | +# >>> tkinter.Label(font=('Helvetica', 12, collections.deque(['bold']))) |
| 17 | +# Traceback (most recent call last): |
| 18 | +# ... |
| 19 | +# _tkinter.TclError: unknown font style "deque(['bold'])" |
| 20 | +_T = TypeVar("_T") |
| 21 | +_TkinterSequence = Union[List[_T], Tuple[_T, ...]] |
| 22 | + |
| 23 | +# See 'FONT DESCRIPTIONS' in font man page. This uses str because Literal |
| 24 | +# inside Tuple doesn't work. |
| 25 | +_FontDescription = Union[ |
| 26 | + str, Font, Tuple[str, int], Tuple[str, int, _TkinterSequence[str]], |
| 27 | +] |
| 28 | + |
| 29 | +class _FontDict(TypedDict): |
| 30 | + family: str |
| 31 | + size: int |
| 32 | + weight: Literal["normal", "bold"] |
| 33 | + slant: Literal["roman", "italic"] |
| 34 | + underline: Literal[0, 1] |
| 35 | + overstrike: Literal[0, 1] |
| 36 | + |
| 37 | +class _MetricsDict(TypedDict): |
| 38 | + ascent: int |
| 39 | + descent: int |
| 40 | + linespace: int |
| 41 | + fixed: Literal[0, 1] |
| 42 | + |
| 43 | +class Font: |
| 44 | + name: str |
| 45 | + delete_font: bool |
| 46 | + def __init__( |
| 47 | + self, |
| 48 | + # In tkinter, 'root' refers to tkinter.Tk by convention, but the code |
| 49 | + # actually works with any tkinter widget so we use tkinter.Misc. |
| 50 | + root: Optional[tkinter.Misc] = ..., |
| 51 | + font: Optional[_FontDescription] = ..., |
| 52 | + name: Optional[str] = ..., |
| 53 | + exists: bool = ..., |
| 54 | + *, |
| 55 | + family: str = ..., |
| 56 | + size: int = ..., |
| 57 | + weight: Literal["normal", "bold"] = ..., |
| 58 | + slant: Literal["roman", "italic"] = ..., |
| 59 | + underline: bool = ..., |
| 60 | + overstrike: bool = ..., |
| 61 | + ) -> None: ... |
| 62 | + @overload |
| 63 | + def cget(self, option: Literal["family"]) -> str: ... |
| 64 | + @overload |
| 65 | + def cget(self, option: Literal["size"]) -> int: ... |
| 66 | + @overload |
| 67 | + def cget(self, option: Literal["weight"]) -> Literal["normal", "bold"]: ... |
| 68 | + @overload |
| 69 | + def cget(self, option: Literal["slant"]) -> Literal["roman", "italic"]: ... |
| 70 | + @overload |
| 71 | + def cget(self, option: Literal["underline", "overstrike"]) -> Literal[0, 1]: ... |
| 72 | + __getitem__ = cget |
| 73 | + @overload |
| 74 | + def actual(self, option: Literal["family"], displayof: Optional[tkinter.Misc] = ...) -> str: ... |
| 75 | + @overload |
| 76 | + def actual(self, option: Literal["size"], displayof: Optional[tkinter.Misc] = ...) -> int: ... |
| 77 | + @overload |
| 78 | + def actual(self, option: Literal["weight"], displayof: Optional[tkinter.Misc] = ...) -> Literal["normal", "bold"]: ... |
| 79 | + @overload |
| 80 | + def actual(self, option: Literal["slant"], displayof: Optional[tkinter.Misc] = ...) -> Literal["roman", "italic"]: ... |
| 81 | + @overload |
| 82 | + def actual(self, option: Literal["underline", "overstrike"], displayof: Optional[tkinter.Misc] = ...) -> Literal[0, 1]: ... |
| 83 | + @overload |
| 84 | + def actual(self, option: None, displayof: Optional[tkinter.Misc] = ...) -> _FontDict: ... |
| 85 | + @overload |
| 86 | + def actual(self, *, displayof: Optional[tkinter.Misc] = ...) -> _FontDict: ... |
| 87 | + @overload |
| 88 | + def __setitem__(self, key: Literal["family"], value: str) -> None: ... |
| 89 | + @overload |
| 90 | + def __setitem__(self, key: Literal["size"], value: int) -> None: ... |
| 91 | + @overload |
| 92 | + def __setitem__(self, key: Literal["weight"], value: Literal["normal", "bold"]) -> None: ... |
| 93 | + @overload |
| 94 | + def __setitem__(self, key: Literal["slant"], value: Literal["roman", "italic"]) -> None: ... |
| 95 | + @overload |
| 96 | + def __setitem__(self, key: Literal["underline"], value: bool) -> None: ... |
| 97 | + @overload |
| 98 | + def __setitem__(self, key: Literal["overstrike"], value: bool) -> None: ... |
| 99 | + def config( |
| 100 | + self, |
| 101 | + *, |
| 102 | + family: str = ..., |
| 103 | + size: int = ..., |
| 104 | + weight: Literal["normal", "bold"] = ..., |
| 105 | + slant: Literal["roman", "italic"] = ..., |
| 106 | + underline: bool = ..., |
| 107 | + overstrike: bool = ..., |
| 108 | + ) -> Optional[_FontDict]: ... |
| 109 | + configure = config |
| 110 | + def copy(self) -> Font: ... |
| 111 | + @overload |
| 112 | + def metrics(self, __option: Literal["ascent", "descent", "linespace"], *, displayof: Optional[tkinter.Misc] = ...) -> int: ... |
| 113 | + @overload |
| 114 | + def metrics(self, __option: Literal["fixed"], *, displayof: Optional[tkinter.Misc] = ...) -> Literal[0, 1]: ... |
| 115 | + @overload |
| 116 | + def metrics(self, *, displayof: Optional[tkinter.Misc] = ...) -> _MetricsDict: ... |
| 117 | + def measure(self, text: str, displayof: Optional[tkinter.Misc] = ...) -> int: ... |
| 118 | + |
| 119 | +def families(root: Optional[tkinter.Misc] = ..., displayof: Optional[tkinter.Misc] = ...) -> Tuple[str, ...]: ... |
| 120 | +def names(root: Optional[tkinter.Misc] = ...) -> Tuple[str, ...]: ... |
0 commit comments