Skip to content

Commit 68dbab8

Browse files
authored
More types for tkinter.Treeview (#5584)
1 parent 6159136 commit 68dbab8

File tree

1 file changed

+168
-30
lines changed

1 file changed

+168
-30
lines changed

stdlib/tkinter/ttk.pyi

Lines changed: 168 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import sys
33
import tkinter
44
from tkinter.font import _FontDescription
55
from typing import Any, Callable, Dict, List, Optional, Tuple, Union, overload
6-
from typing_extensions import Literal
6+
from typing_extensions import Literal, TypedDict
77

88
def tclobjs_to_py(adict): ...
99
def setup_master(master: Optional[Any] = ...): ...
@@ -871,6 +871,36 @@ if sys.version_info >= (3, 7):
871871
config = configure # type: ignore
872872
def set(self, value: Any) -> None: ...
873873

874+
class _TreeviewItemDict(TypedDict):
875+
text: str
876+
image: Literal[""] | list[str] # no idea why it's wrapped in list
877+
values: list[Any]
878+
open: bool # actually 0 or 1
879+
tags: list[str]
880+
881+
class _TreeviewTagDict(TypedDict):
882+
text: str
883+
image: Literal[""] | str # not wrapped in list :D
884+
anchor: tkinter._Anchor
885+
background: tkinter._Color
886+
foreground: tkinter._Color
887+
888+
class _TreeviewHeaderDict(TypedDict):
889+
text: str
890+
image: list[str]
891+
anchor: tkinter._Anchor
892+
command: str
893+
state: str # Doesn't seem to appear anywhere else than in these dicts
894+
895+
class _TreeviewColumnDict(TypedDict):
896+
width: int
897+
minwidth: int
898+
stretch: bool # actually 0 or 1
899+
anchor: tkinter._Anchor
900+
id: str
901+
902+
_TreeviewColumnId = Union[int, str] # manual page: "COLUMN IDENTIFIERS"
903+
874904
class Treeview(Widget, tkinter.XView, tkinter.YView):
875905
def __init__(
876906
self,
@@ -914,38 +944,122 @@ class Treeview(Widget, tkinter.XView, tkinter.YView):
914944
@overload
915945
def configure(self, cnf: str) -> Tuple[str, str, str, Any, Any]: ...
916946
config = configure
917-
def bbox(self, item, column: Optional[Any] = ...): ... # type: ignore
918-
def get_children(self, item: Optional[Any] = ...): ...
919-
def set_children(self, item, *newchildren): ...
920-
def column(self, column, option: Optional[Any] = ..., **kw): ...
921-
def delete(self, *items): ...
922-
def detach(self, *items): ...
923-
def exists(self, item): ...
924-
def focus(self, item: Optional[Any] = ...): ...
925-
def heading(self, column, option: Optional[Any] = ..., **kw): ...
947+
def bbox(self, item, column: _TreeviewColumnId | None = ...) -> Tuple[int, int, int, int] | Literal[""]: ... # type: ignore
948+
def get_children(self, item: str | None = ...) -> Tuple[str, ...]: ...
949+
def set_children(self, item: str, *newchildren: str) -> None: ...
950+
@overload
951+
def column(self, column: _TreeviewColumnId, option: Literal["width", "minwidth"]) -> int: ...
952+
@overload
953+
def column(self, column: _TreeviewColumnId, option: Literal["stretch"]) -> bool: ... # actually 0 or 1
954+
@overload
955+
def column(self, column: _TreeviewColumnId, option: Literal["anchor"]) -> _tkinter.Tcl_Obj: ...
956+
@overload
957+
def column(self, column: _TreeviewColumnId, option: Literal["id"]) -> str: ...
958+
@overload
959+
def column(self, column: _TreeviewColumnId, option: str) -> Any: ...
960+
@overload
961+
def column(
962+
self,
963+
column: _TreeviewColumnId,
964+
option: None = ...,
965+
*,
966+
width: int = ...,
967+
minwidth: int = ...,
968+
stretch: bool = ...,
969+
anchor: tkinter._Anchor = ...,
970+
# id is read-only
971+
) -> _TreeviewColumnDict | None: ...
972+
def delete(self, *items: str) -> None: ...
973+
def detach(self, *items: str) -> None: ...
974+
def exists(self, item: str) -> bool: ...
975+
@overload # type: ignore
976+
def focus(self, item: None = ...) -> str: ... # can return empty string
977+
@overload
978+
def focus(self, item: str) -> Literal[""]: ...
979+
@overload
980+
def heading(self, column: _TreeviewColumnId, option: Literal["text"]) -> str: ...
981+
@overload
982+
def heading(self, column: _TreeviewColumnId, option: Literal["image"]) -> Tuple[str]: ...
983+
@overload
984+
def heading(self, column: _TreeviewColumnId, option: Literal["anchor"]) -> _tkinter.Tcl_Obj: ...
985+
@overload
986+
def heading(self, column: _TreeviewColumnId, option: Literal["command"]) -> str: ...
987+
@overload
988+
def heading(self, column: _TreeviewColumnId, option: str) -> Any: ...
989+
@overload
990+
def heading(
991+
self,
992+
column: _TreeviewColumnId,
993+
option: None = ...,
994+
*,
995+
text: str = ...,
996+
image: tkinter._ImageSpec = ...,
997+
anochor: tkinter._Anchor = ...,
998+
command: str | Callable[[], Any] = ...,
999+
) -> _TreeviewHeaderDict | None: ...
9261000
def identify(self, component, x, y): ...
927-
def identify_row(self, y): ...
928-
def identify_column(self, x): ...
929-
def identify_region(self, x, y): ...
930-
def identify_element(self, x, y): ...
931-
def index(self, item): ...
932-
def insert(self, parent, index, iid: Optional[Any] = ..., **kw): ...
933-
def item(self, item, option: Optional[Any] = ..., **kw): ...
934-
def move(self, item, parent, index): ...
935-
reattach: Any
936-
def next(self, item): ...
937-
def parent(self, item): ...
938-
def prev(self, item): ...
939-
def see(self, item): ...
1001+
def identify_row(self, y: int) -> str: ...
1002+
def identify_column(self, x: int) -> str: ...
1003+
def identify_region(self, x: int, y: int) -> Literal["heading", "separator", "tree", "cell", "nothing"]: ...
1004+
def identify_element(self, x: int, y: int) -> str: ... # don't know what possible return values are
1005+
def index(self, item: str) -> int: ...
1006+
def insert(
1007+
self,
1008+
parent: str,
1009+
index: int | Literal["end"],
1010+
iid: str | None = ...,
1011+
*,
1012+
id: str = ..., # same as iid
1013+
text: str = ...,
1014+
image: tkinter._ImageSpec = ...,
1015+
values: tkinter._TkinterSequence[Any] = ...,
1016+
open: bool = ...,
1017+
tags: str | tkinter._TkinterSequence[str] = ...,
1018+
) -> str: ...
1019+
@overload
1020+
def item(self, item: str, option: Literal["text"]) -> str: ...
1021+
@overload
1022+
def item(self, item: str, option: Literal["image"]) -> Literal[""] | Tuple[str]: ...
1023+
@overload
1024+
def item(self, item: str, option: Literal["values"]) -> Literal[""] | Tuple[Any, ...]: ...
1025+
@overload
1026+
def item(self, item: str, option: Literal["open"]) -> bool: ... # actually 0 or 1
1027+
@overload
1028+
def item(self, item: str, option: Literal["tags"]) -> Literal[""] | Tuple[str, ...]: ...
1029+
@overload
1030+
def item(self, item: str, option: str) -> Any: ...
1031+
@overload
1032+
def item(
1033+
self,
1034+
item: str,
1035+
option: None = ...,
1036+
*,
1037+
text: str = ...,
1038+
image: tkinter._ImageSpec = ...,
1039+
values: tkinter._TkinterSequence[Any] = ...,
1040+
open: bool = ...,
1041+
tags: str | tkinter._TkinterSequence[str] = ...,
1042+
) -> _TreeviewItemDict | None: ...
1043+
def move(self, item: str, parent: str, index: int) -> None: ...
1044+
reattach = move
1045+
def next(self, item: str) -> str: ... # returning empty string means last item
1046+
def parent(self, item: str) -> str: ...
1047+
def prev(self, item: str) -> str: ... # returning empty string means first item
1048+
def see(self, item: str) -> None: ...
9401049
if sys.version_info >= (3, 8):
9411050
def selection(self) -> Tuple[str, ...]: ...
9421051
else:
9431052
def selection(self, selop: Optional[Any] = ..., items: Optional[Any] = ...) -> Tuple[str, ...]: ...
944-
def selection_set(self, items): ...
945-
def selection_add(self, items): ...
946-
def selection_remove(self, items): ...
947-
def selection_toggle(self, items): ...
948-
def set(self, item, column: Optional[Any] = ..., value: Optional[Any] = ...): ...
1053+
def selection_set(self, items: str | tkinter._TkinterSequence[str]) -> None: ...
1054+
def selection_add(self, items: str | tkinter._TkinterSequence[str]) -> None: ...
1055+
def selection_remove(self, items: str | tkinter._TkinterSequence[str]) -> None: ...
1056+
def selection_toggle(self, items: str | tkinter._TkinterSequence[str]) -> None: ...
1057+
@overload
1058+
def set(self, item: str, column: None = ..., value: None = ...) -> dict[str, Any]: ...
1059+
@overload
1060+
def set(self, item: str, column: _TreeviewColumnId, value: None = ...) -> Any: ...
1061+
@overload
1062+
def set(self, item: str, column: _TreeviewColumnId, value: Any) -> Literal[""]: ...
9491063
# There's no tag_unbind() or 'add' argument for whatever reason.
9501064
# Also, it's 'callback' instead of 'func' here.
9511065
@overload
@@ -956,8 +1070,32 @@ class Treeview(Widget, tkinter.XView, tkinter.YView):
9561070
def tag_bind(self, tagname: str, sequence: Optional[str], callback: str) -> None: ...
9571071
@overload
9581072
def tag_bind(self, tagname: str, *, callback: str) -> None: ...
959-
def tag_configure(self, tagname, option: Optional[Any] = ..., **kw): ...
960-
def tag_has(self, tagname, item: Optional[Any] = ...): ...
1073+
@overload
1074+
def tag_configure(self, tagname: str, option: Literal["text"]) -> str: ...
1075+
@overload
1076+
def tag_configure(self, tagname: str, option: Literal["image"]) -> str: ...
1077+
@overload
1078+
def tag_configure(self, tagname: str, option: Literal["anchor"]) -> tkinter._Anchor | Literal[""]: ...
1079+
@overload
1080+
def tag_configure(self, tagname: str, option: Literal["foreground", "background"]) -> tkinter._Color: ...
1081+
@overload
1082+
def tag_configure(self, tagname: str, option: str) -> Any: ...
1083+
@overload
1084+
def tag_configure(
1085+
self,
1086+
tagname: str,
1087+
option: None = ...,
1088+
*,
1089+
text: str = ...,
1090+
image: tkinter._ImageSpec = ...,
1091+
anchor: tkinter._Anchor = ...,
1092+
background: tkinter._Color = ...,
1093+
foreground: tkinter._Color = ...,
1094+
) -> _TreeviewTagDict | None: ...
1095+
@overload
1096+
def tag_has(self, tagname: str, item: None = ...) -> Tuple[str, ...]: ...
1097+
@overload
1098+
def tag_has(self, tagname: str, item: str) -> bool: ...
9611099

9621100
class LabeledScale(Frame):
9631101
label: Any

0 commit comments

Comments
 (0)