| 1 | # **About tksheet** |
| 2 | |
| 3 | - `tksheet` is a Python tkinter table and treeview widget written in pure python. |
| 4 | - It is licensed under the [MIT license](https://github.com/ragardner/tksheet/blob/master/LICENSE.txt). |
| 5 | - It works by using tkinter canvases and moving lines, text and rectangles around for only the visible portion of the table. |
| 6 | - If you are using a version of tksheet that is older than `7.0.0` then you will need the documentation [here](https://github.com/ragardner/tksheet/wiki/Version-6) instead. |
| 7 | - In tksheet versions >= `7.0.2` the current version will be at the top of the file `__init__.py`. |
| 8 | |
| 9 | ### **Limitations** |
| 10 | |
| 11 | Some examples of things that are not possible with tksheet: |
| 12 | |
| 13 | - Due to the limitations of the Tkinter Canvas right-to-left (RTL) languages are not supported. |
| 14 | - Cell merging. |
| 15 | - Changing font for individual cells. |
| 16 | - Mouse drag copy cells. |
| 17 | - Cell highlight borders. |
| 18 | - At the present time the type hinting in tksheet is only meant to serve as a guide and not to be used with type checkers. |
| 19 | |
| 20 | --- |
| 21 | # **Installation and Requirements** |
| 22 | |
| 23 | `tksheet` is available through PyPi (Python package index) and can be installed by using Pip through the command line `pip install tksheet` |
| 24 | |
| 25 | ```python |
| 26 | #To install using pip |
| 27 | pip install tksheet |
| 28 | |
| 29 | #To update using pip |
| 30 | pip install tksheet --upgrade |
| 31 | ``` |
| 32 | |
| 33 | Alternatively you can download the source code and inside the tksheet directory where the `pyproject.toml` file is located use the command line `pip install -e .` |
| 34 | |
| 35 | - Versions **<** `7.0.0` require a Python version of **`3.7`** or higher. |
| 36 | - Versions **>=** `7.0.0` require a Python version of **`3.8`** or higher. |
| 37 | |
| 38 | --- |
| 39 | # **Basic Initialization** |
| 40 | |
| 41 | Like other tkinter widgets you need only the `Sheet()`s parent as an argument to initialize a `Sheet()` e.g. |
| 42 | ```python |
| 43 | sheet = Sheet(my_frame_widget) |
| 44 | ``` |
| 45 | - `my_frame_widget` would be replaced by whatever widget is your `Sheet()`s parent. |
| 46 | |
| 47 | ___ |
| 48 | |
| 49 | As an example, this is a tkinter program involving a `Sheet()` widget |
| 50 | ```python |
| 51 | from tksheet import Sheet |
| 52 | import tkinter as tk |
| 53 | |
| 54 | |
| 55 | class demo(tk.Tk): |
| 56 | def __init__(self): |
| 57 | tk.Tk.__init__(self) |
| 58 | self.grid_columnconfigure(0, weight = 1) |
| 59 | self.grid_rowconfigure(0, weight = 1) |
| 60 | self.frame = tk.Frame(self) |
| 61 | self.frame.grid_columnconfigure(0, weight = 1) |
| 62 | self.frame.grid_rowconfigure(0, weight = 1) |
| 63 | self.sheet = Sheet(self.frame, |
| 64 | data = [[f"Row {r}, Column {c}\nnewline1\nnewline2" for c in range(50)] for r in range(500)]) |
| 65 | self.sheet.enable_bindings() |
| 66 | self.frame.grid(row = 0, column = 0, sticky = "nswe") |
| 67 | self.sheet.grid(row = 0, column = 0, sticky = "nswe") |
| 68 | |
| 69 | |
| 70 | app = demo() |
| 71 | app.mainloop() |
| 72 | ``` |
| 73 | |
| 74 | --- |
| 75 | # **Usage Examples** |
| 76 | |
| 77 | This is to demonstrate some of tksheets functionality: |
| 78 | |
| 79 | - The functions which return the Sheet itself (have `-> Sheet`) can be chained with other Sheet functions. |
| 80 | - The functions which return a Span (have `-> Span`) can be chained with other Span functions. |
| 81 | |
| 82 | ```python |
| 83 | from tksheet import Sheet, num2alpha |
| 84 | import tkinter as tk |
| 85 | |
| 86 | |
| 87 | class demo(tk.Tk): |
| 88 | def __init__(self): |
| 89 | tk.Tk.__init__(self) |
| 90 | self.grid_columnconfigure(0, weight=1) |
| 91 | self.grid_rowconfigure(0, weight=1) |
| 92 | self.frame = tk.Frame(self) |
| 93 | self.frame.grid_columnconfigure(0, weight=1) |
| 94 | self.frame.grid_rowconfigure(0, weight=1) |
| 95 | |
| 96 | # create an instance of Sheet() |
| 97 | self.sheet = Sheet( |
| 98 | # set the Sheets parent widget |
| 99 | self.frame, |
| 100 | # optional: set the Sheets data at initialization |
| 101 | data=[[f"Row {r}, Column {c}\nnewline1\nnewline2" for c in range(20)] for r in range(100)], |
| 102 | theme="light green", |
| 103 | height=520, |
| 104 | width=1000, |
| 105 | ) |
| 106 | # enable various bindings |
| 107 | self.sheet.enable_bindings("all", "edit_index", "edit_header") |
| 108 | |
| 109 | # set a user edit validation function |
| 110 | # AND bind all sheet modification events to a function |
| 111 | # chained as two functions |
| 112 | # more information at: |
| 113 | # #validate-user-cell-edits |
| 114 | self.sheet.edit_validation(self.validate_edits).bind("<<SheetModified>>", self.sheet_modified) |
| 115 | |
| 116 | # add some new commands to the in-built right click menu |
| 117 | # setting data |
| 118 | self.sheet.popup_menu_add_command( |
| 119 | "Say Hello", |
| 120 | self.say_hello, |
| 121 | index_menu=False, |
| 122 | header_menu=False, |
| 123 | empty_space_menu=False, |
| 124 | ) |
| 125 | # getting data |
| 126 | self.sheet.popup_menu_add_command( |
| 127 | "Print some data", |
| 128 | self.print_data, |
| 129 | empty_space_menu=False, |
| 130 | ) |
| 131 | # overwrite Sheet data |
| 132 | self.sheet.popup_menu_add_command("Reset Sheet data", self.reset) |
| 133 | # set the header |
| 134 | self.sheet.popup_menu_add_command( |
| 135 | "Set header data", |
| 136 | self.set_header, |
| 137 | table_menu=False, |
| 138 | index_menu=False, |
| 139 | empty_space_menu=False, |
| 140 | ) |
| 141 | # set the index |
| 142 | self.sheet.popup_menu_add_command( |
| 143 | "Set index data", |
| 144 | self.set_index, |
| 145 | table_menu=False, |
| 146 | header_menu=False, |
| 147 | empty_space_menu=False, |
| 148 | ) |
| 149 | |
| 150 | self.frame.grid(row=0, column=0, sticky="nswe") |
| 151 | self.sheet.grid(row=0, column=0, sticky="nswe") |
| 152 | |
| 153 | def validate_edits(self, event): |
| 154 | # print (event) |
| 155 | if event.eventname.endswith("header"): |
| 156 | return event.value + " edited header" |
| 157 | elif event.eventname.endswith("index"): |
| 158 | return event.value + " edited index" |
| 159 | else: |
| 160 | if not event.value: |
| 161 | return "EMPTY" |
| 162 | return event.value[:3] |
| 163 | |
| 164 | def say_hello(self): |
| 165 | current_selection = self.sheet.get_currently_selected() |
| 166 | if current_selection: |
| 167 | box = (current_selection.row, current_selection.column) |
| 168 | # set cell data, end user Undo enabled |
| 169 | # more information at: |
| 170 | # #setting-sheet-data |
| 171 | self.sheet[box].options(undo=True).data = "Hello World!" |
| 172 | # highlight the cell for 2 seconds |
| 173 | self.highlight_area(box) |
| 174 | |
| 175 | def print_data(self): |
| 176 | for box in self.sheet.get_all_selection_boxes(): |
| 177 | # get user selected area sheet data |
| 178 | # more information at: |
| 179 | # #getting-sheet-data |
| 180 | data = self.sheet[box].data |
| 181 | for row in data: |
| 182 | print(row) |
| 183 | |
| 184 | def reset(self): |
| 185 | # overwrites sheet data, more information at: |
| 186 | # #setting-sheet-data |
| 187 | self.sheet.set_sheet_data([[f"Row {r}, Column {c}\nnewline1\nnewline2" for c in range(20)] for r in range(100)]) |
| 188 | # reset header and index |
| 189 | self.sheet.headers([]) |
| 190 | self.sheet.index([]) |
| 191 | |
| 192 | def set_header(self): |
| 193 | self.sheet.headers( |
| 194 | [f"Header {(letter := num2alpha(i))} - {i + 1}\nHeader {letter} 2nd line!" for i in range(20)] |
| 195 | ) |
| 196 | |
| 197 | def set_index(self): |
| 198 | self.sheet.set_index_width() |
| 199 | self.sheet.row_index( |
| 200 | [f"Index {(letter := num2alpha(i))} - {i + 1}\nIndex {letter} 2nd line!" for i in range(100)] |
| 201 | ) |
| 202 | |
| 203 | def sheet_modified(self, event): |
| 204 | # uncomment below if you want to take a look at the event object |
| 205 | # print ("The sheet was modified! Event object:") |
| 206 | # for k, v in event.items(): |
| 207 | # print (k, ":", v) |
| 208 | # print ("\n") |
| 209 | |
| 210 | # otherwise more information at: |
| 211 | # #event-data |
| 212 | |
| 213 | # highlight the modified cells briefly |
| 214 | if event.eventname.startswith("move"): |
| 215 | for box in self.sheet.get_all_selection_boxes(): |
| 216 | self.highlight_area(box) |
| 217 | else: |
| 218 | for box in event.selection_boxes: |
| 219 | self.highlight_area(box) |
| 220 | |
| 221 | def highlight_area(self, box, time=800): |
| 222 | # highlighting an area of the sheet |
| 223 | # more information at: |
| 224 | # #highlighting-cells |
| 225 | self.sheet[box].bg = "indianred1" |
| 226 | self.after(time, lambda: self.clear_highlight(box)) |
| 227 | |
| 228 | def clear_highlight(self, box): |
| 229 | self.sheet[box].dehighlight() |
| 230 | |
| 231 | |
| 232 | app = demo() |
| 233 | app.mainloop() |
| 234 | ``` |
| 235 | |
| 236 | --- |
| 237 | # **Initialization Options** |
| 238 | |
| 239 | These are all the initialization parameters, the only required argument is the sheets `parent`, every other parameter has default arguments. |
| 240 | |
| 241 | ```python |
| 242 | def __init__( |
| 243 | parent: tk.Misc, |
| 244 | name: str = "!sheet", |
| 245 | show_table: bool = True, |
| 246 | show_top_left: bool | None = None, |
| 247 | show_row_index: bool = True, |
| 248 | show_header: bool = True, |
| 249 | show_x_scrollbar: bool = True, |
| 250 | show_y_scrollbar: bool = True, |
| 251 | width: int | None = None, |
| 252 | height: int | None = None, |
| 253 | headers: None | list[object] = None, |
| 254 | header: None | list[object] = None, |
| 255 | row_index: None | list[object] = None, |
| 256 | index: None | list[object] = None, |
| 257 | default_header: Literal["letters", "numbers", "both"] | None = "letters", |
| 258 | default_row_index: Literal["letters", "numbers", "both"] | None = "numbers", |
| 259 | data_reference: None | Sequence[Sequence[object]] = None, |
| 260 | data: None | Sequence[Sequence[object]] = None, |
| 261 | # either (start row, end row, "rows"), (start column, end column, "rows") or |
| 262 | # (cells start row, cells start column, cells end row, cells end column, "cells") # noqa: E501 |
| 263 | startup_select: tuple[int, int, str] | tuple[int, int, int, int, str] = None, |
| 264 | startup_focus: bool = True, |
| 265 | total_columns: int | None = None, |
| 266 | total_rows: int | None = None, |
| 267 | default_column_width: int = 120, |
| 268 | default_header_height: str | int = "1", |
| 269 | default_row_index_width: int = 70, |
| 270 | default_row_height: str | int = "1", |
| 271 | min_column_width: int = 1, |
| 272 | max_column_width: float = float("inf"), |
| 273 | max_row_height: float = float("inf"), |
| 274 | max_header_height: float = float("inf"), |
| 275 | max_index_width: float = float("inf"), |
| 276 | after_redraw_time_ms: int = 20, |
| 277 | set_all_heights_and_widths: bool = False, |
| 278 | zoom: int = 100, |
| 279 | align: str = "w", |
| 280 | header_align: str = "center", |
| 281 | row_index_align: str | None = None, |
| 282 | index_align: str = "center", |
| 283 | displayed_columns: list[int] = [], |
| 284 | all_columns_displayed: bool = True, |
| 285 | displayed_rows: list[int] = [], |
| 286 | all_rows_displayed: bool = True, |
| 287 | to_clipboard_delimiter: str = "\t", |
| 288 | to_clipboard_quotechar: str = '"', |
| 289 | to_clipboard_lineterminator: str = "\n", |
| 290 | from_clipboard_delimiters: list[str] | str = ["\t"], |
| 291 | show_default_header_for_empty: bool = True, |
| 292 | show_default_index_for_empty: bool = True, |
| 293 | page_up_down_select_row: bool = True, |
| 294 | paste_can_expand_x: bool = False, |
| 295 | paste_can_expand_y: bool = False, |
| 296 | paste_insert_column_limit: int | None = None, |
| 297 | paste_insert_row_limit: int | None = None, |
| 298 | show_dropdown_borders: bool = False, |
| 299 | arrow_key_down_right_scroll_page: bool = False, |
| 300 | cell_auto_resize_enabled: bool = True, |
| 301 | auto_resize_row_index: bool | Literal["empty"] = "empty", |
| 302 | auto_resize_columns: int | None = None, |
| 303 | auto_resize_rows: int | None = None, |
| 304 | set_cell_sizes_on_zoom: bool = False, |
| 305 | font: tuple[str, int, str] = FontTuple( |
| 306 | "Calibri", |
| 307 | 13 if USER_OS == "darwin" else 11, |
| 308 | "normal", |
| 309 | ), |
| 310 | header_font: tuple[str, int, str] = FontTuple( |
| 311 | "Calibri", |
| 312 | 13 if USER_OS == "darwin" else 11, |
| 313 | "normal", |
| 314 | ), |
| 315 | index_font: tuple[str, int, str] = FontTuple( |
| 316 | "Calibri", |
| 317 | 13 if USER_OS == "darwin" else 11, |
| 318 | "normal", |
| 319 | ), # currently has no effect |
| 320 | popup_menu_font: tuple[str, int, str] = FontTuple( |
| 321 | "Calibri", |
| 322 | 13 if USER_OS == "darwin" else 11, |
| 323 | "normal", |
| 324 | ), |
| 325 | max_undos: int = 30, |
| 326 | column_drag_and_drop_perform: bool = True, |
| 327 | row_drag_and_drop_perform: bool = True, |
| 328 | empty_horizontal: int = 50, |
| 329 | empty_vertical: int = 50, |
| 330 | selected_rows_to_end_of_window: bool = False, |
| 331 | horizontal_grid_to_end_of_window: bool = False, |
| 332 | vertical_grid_to_end_of_window: bool = False, |
| 333 | show_vertical_grid: bool = True, |
| 334 | show_horizontal_grid: bool = True, |
| 335 | display_selected_fg_over_highlights: bool = False, |
| 336 | show_selected_cells_border: bool = True, |
| 337 | edit_cell_tab: Literal["right", "down", ""] = "right", |
| 338 | edit_cell_return: Literal["right", "down", ""] = "down", |
| 339 | editor_del_key: Literal["forward", "backward", ""] = "forward", |
| 340 | treeview: bool = False, |
| 341 | treeview_indent: str | int = "5", |
| 342 | rounded_boxes: bool = True, |
| 343 | alternate_color: str = "", |
| 344 | allow_cell_overflow: bool = False, |
| 345 | table_wrap: Literal["", "w", "c"] = "c", # "" no wrap, "w" word wrap, "c" char wrap |
| 346 | index_wrap: Literal["", "w", "c"] = "c", # "" no wrap, "w" word wrap, "c" char wrap |
| 347 | header_wrap: Literal["", "w", "c"] = "c", # "" no wrap, "w" word wrap, "c" char wrap |
| 348 | sort_key: Callable = natural_sort_key, |
| 349 | # colors |
| 350 | outline_thickness: int = 0, |
| 351 | outline_color: str = theme_light_blue["outline_color"], |
| 352 | theme: str = "light blue", |
| 353 | frame_bg: str = theme_light_blue["table_bg"], |
| 354 | popup_menu_fg: str = theme_light_blue["popup_menu_fg"], |
| 355 | popup_menu_bg: str = theme_light_blue["popup_menu_bg"], |
| 356 | popup_menu_highlight_bg: str = theme_light_blue["popup_menu_highlight_bg"], |
| 357 | popup_menu_highlight_fg: str = theme_light_blue["popup_menu_highlight_fg"], |
| 358 | table_grid_fg: str = theme_light_blue["table_grid_fg"], |
| 359 | table_bg: str = theme_light_blue["table_bg"], |
| 360 | table_fg: str = theme_light_blue["table_fg"], |
| 361 | table_editor_bg: str = theme_light_blue["table_editor_bg"], |
| 362 | table_editor_fg: str = theme_light_blue["table_editor_fg"], |
| 363 | table_editor_select_bg: str = theme_light_blue["table_editor_select_bg"], |
| 364 | table_editor_select_fg: str = theme_light_blue["table_editor_select_fg"], |
| 365 | table_selected_box_cells_fg: str = theme_light_blue["table_selected_box_cells_fg"], |
| 366 | table_selected_box_rows_fg: str = theme_light_blue["table_selected_box_rows_fg"], |
| 367 | table_selected_box_columns_fg: str = theme_light_blue["table_selected_box_columns_fg"], |
| 368 | table_selected_cells_border_fg: str = theme_light_blue["table_selected_cells_border_fg"], |
| 369 | table_selected_cells_bg: str = theme_light_blue["table_selected_cells_bg"], |
| 370 | table_selected_cells_fg: str = theme_light_blue["table_selected_cells_fg"], |
| 371 | table_selected_rows_border_fg: str = theme_light_blue["table_selected_rows_border_fg"], |
| 372 | table_selected_rows_bg: str = theme_light_blue["table_selected_rows_bg"], |
| 373 | table_selected_rows_fg: str = theme_light_blue["table_selected_rows_fg"], |
| 374 | table_selected_columns_border_fg: str = theme_light_blue["table_selected_columns_border_fg"], |
| 375 | table_selected_columns_bg: str = theme_light_blue["table_selected_columns_bg"], |
| 376 | table_selected_columns_fg: str = theme_light_blue["table_selected_columns_fg"], |
| 377 | resizing_line_fg: str = theme_light_blue["resizing_line_fg"], |
| 378 | drag_and_drop_bg: str = theme_light_blue["drag_and_drop_bg"], |
| 379 | index_bg: str = theme_light_blue["index_bg"], |
| 380 | index_border_fg: str = theme_light_blue["index_border_fg"], |
| 381 | index_grid_fg: str = theme_light_blue["index_grid_fg"], |
| 382 | index_fg: str = theme_light_blue["index_fg"], |
| 383 | index_editor_bg: str = theme_light_blue["index_editor_bg"], |
| 384 | index_editor_fg: str = theme_light_blue["index_editor_fg"], |
| 385 | index_editor_select_bg: str = theme_light_blue["index_editor_select_bg"], |
| 386 | index_editor_select_fg: str = theme_light_blue["index_editor_select_fg"], |
| 387 | index_selected_cells_bg: str = theme_light_blue["index_selected_cells_bg"], |
| 388 | index_selected_cells_fg: str = theme_light_blue["index_selected_cells_fg"], |
| 389 | index_selected_rows_bg: str = theme_light_blue["index_selected_rows_bg"], |
| 390 | index_selected_rows_fg: str = theme_light_blue["index_selected_rows_fg"], |
| 391 | index_hidden_rows_expander_bg: str = theme_light_blue["index_hidden_rows_expander_bg"], |
| 392 | header_bg: str = theme_light_blue["header_bg"], |
| 393 | header_border_fg: str = theme_light_blue["header_border_fg"], |
| 394 | header_grid_fg: str = theme_light_blue["header_grid_fg"], |
| 395 | header_fg: str = theme_light_blue["header_fg"], |
| 396 | header_editor_bg: str = theme_light_blue["header_editor_bg"], |
| 397 | header_editor_fg: str = theme_light_blue["header_editor_fg"], |
| 398 | header_editor_select_bg: str = theme_light_blue["header_editor_select_bg"], |
| 399 | header_editor_select_fg: str = theme_light_blue["header_editor_select_fg"], |
| 400 | header_selected_cells_bg: str = theme_light_blue["header_selected_cells_bg"], |
| 401 | header_selected_cells_fg: str = theme_light_blue["header_selected_cells_fg"], |
| 402 | header_selected_columns_bg: str = theme_light_blue["header_selected_columns_bg"], |
| 403 | header_selected_columns_fg: str = theme_light_blue["header_selected_columns_fg"], |
| 404 | header_hidden_columns_expander_bg: str = theme_light_blue["header_hidden_columns_expander_bg"], |
| 405 | top_left_bg: str = theme_light_blue["top_left_bg"], |
| 406 | top_left_fg: str = theme_light_blue["top_left_fg"], |
| 407 | top_left_fg_highlight: str = theme_light_blue["top_left_fg_highlight"], |
| 408 | vertical_scroll_background: str = theme_light_blue["vertical_scroll_background"], |
| 409 | horizontal_scroll_background: str = theme_light_blue["horizontal_scroll_background"], |
| 410 | vertical_scroll_troughcolor: str = theme_light_blue["vertical_scroll_troughcolor"], |
| 411 | horizontal_scroll_troughcolor: str = theme_light_blue["horizontal_scroll_troughcolor"], |
| 412 | vertical_scroll_lightcolor: str = theme_light_blue["vertical_scroll_lightcolor"], |
| 413 | horizontal_scroll_lightcolor: str = theme_light_blue["horizontal_scroll_lightcolor"], |
| 414 | vertical_scroll_darkcolor: str = theme_light_blue["vertical_scroll_darkcolor"], |
| 415 | horizontal_scroll_darkcolor: str = theme_light_blue["horizontal_scroll_darkcolor"], |
| 416 | vertical_scroll_relief: str = theme_light_blue["vertical_scroll_relief"], |
| 417 | horizontal_scroll_relief: str = theme_light_blue["horizontal_scroll_relief"], |
| 418 | vertical_scroll_troughrelief: str = theme_light_blue["vertical_scroll_troughrelief"], |
| 419 | horizontal_scroll_troughrelief: str = theme_light_blue["horizontal_scroll_troughrelief"], |
| 420 | vertical_scroll_bordercolor: str = theme_light_blue["vertical_scroll_bordercolor"], |
| 421 | horizontal_scroll_bordercolor: str = theme_light_blue["horizontal_scroll_bordercolor"], |
| 422 | vertical_scroll_borderwidth: int = 1, |
| 423 | horizontal_scroll_borderwidth: int = 1, |
| 424 | vertical_scroll_gripcount: int = 0, |
| 425 | horizontal_scroll_gripcount: int = 0, |
| 426 | vertical_scroll_active_bg: str = theme_light_blue["vertical_scroll_active_bg"], |
| 427 | horizontal_scroll_active_bg: str = theme_light_blue["horizontal_scroll_active_bg"], |
| 428 | vertical_scroll_not_active_bg: str = theme_light_blue["vertical_scroll_not_active_bg"], |
| 429 | horizontal_scroll_not_active_bg: str = theme_light_blue["horizontal_scroll_not_active_bg"], |
| 430 | vertical_scroll_pressed_bg: str = theme_light_blue["vertical_scroll_pressed_bg"], |
| 431 | horizontal_scroll_pressed_bg: str = theme_light_blue["horizontal_scroll_pressed_bg"], |
| 432 | vertical_scroll_active_fg: str = theme_light_blue["vertical_scroll_active_fg"], |
| 433 | horizontal_scroll_active_fg: str = theme_light_blue["horizontal_scroll_active_fg"], |
| 434 | vertical_scroll_not_active_fg: str = theme_light_blue["vertical_scroll_not_active_fg"], |
| 435 | horizontal_scroll_not_active_fg: str = theme_light_blue["horizontal_scroll_not_active_fg"], |
| 436 | vertical_scroll_pressed_fg: str = theme_light_blue["vertical_scroll_pressed_fg"], |
| 437 | horizontal_scroll_pressed_fg: str = theme_light_blue["horizontal_scroll_pressed_fg"], |
| 438 | scrollbar_theme_inheritance: str = "default", |
| 439 | scrollbar_show_arrows: bool = True, |
| 440 | # changing the arrowsize (width) of the scrollbars |
| 441 | # is not working with 'default' theme |
| 442 | # use 'clam' theme instead if you want to change the width |
| 443 | vertical_scroll_arrowsize: str | int = "", |
| 444 | horizontal_scroll_arrowsize: str | int = "", |
| 445 | ) -> None |
| 446 | ``` |
| 447 | - `name` setting a name for the sheet is useful when you have multiple sheets and you need to determine which one an event came from. |
| 448 | - `auto_resize_columns` (`int`, `None`) if set as an `int` the columns will automatically resize to fit the width of the window, the `int` value being the minimum of each column in pixels. |
| 449 | - `auto_resize_rows` (`int`, `None`) if set as an `int` the rows will automatically resize to fit the height of the window, the `int` value being the minimum height of each row in pixels. |
| 450 | - `startup_select` selects cells, rows or columns at initialization by using a `tuple` e.g. `(0, 0, "cells")` for cell A0 or `(0, 5, "rows")` for rows 0 to 5. |
| 451 | - `data_reference` and `data` are essentially the same. |
| 452 | - `row_index` and `index` are the same, `index` takes priority, same as with `headers` and `header`. |
| 453 | - `startup_select` either `(start row, end row, "rows")`, `(start column, end column, "rows")` or `(start row, start column, end row, end column, "cells")`. The start/end row/column variables need to be `int`s. |
| 454 | - `auto_resize_row_index` either `True`, `False` or `"empty"`. |
| 455 | - `"empty"` it will only automatically resize if the row index is empty. |
| 456 | - `True` it will always automatically resize. |
| 457 | - `False` it will never automatically resize. |
| 458 | - If `show_selected_cells_border` is `False` then the colors for `table_selected_box_cells_fg` / `table_selected_box_rows_fg` / `table_selected_box_columns_fg` will be used for the currently selected cells background. |
| 459 | - Only set `show_top_left` to `True` if you want to always show the top left rectangle of the sheet. Leave as `None` to only show it when both the index and header are showing. |
| 460 | - For help with `treeview` mode see [here](#treeview-mode). |
| 461 | |
| 462 | You can change most of these settings after initialization using the [`set_options()` function](#sheet-options-and-other-functions). |
| 463 | - `scrollbar_theme_inheritance` and `scrollbar_show_arrows` will only work on `Sheet()` initialization, not with `set_options()` |
| 464 | |
| 465 | --- |
| 466 | # **Sheet Appearance** |
| 467 | |
| 468 | ### **Sheet Colors** |
| 469 | |
| 470 | To change the colors of individual cells, rows or columns use the functions listed under [highlighting cells](#highlighting-cells). |
| 471 | |
| 472 | For the colors of specific parts of the table such as gridlines and backgrounds use the function `set_options()`, keyword arguments specific to sheet colors are listed below. All the other `set_options()` arguments can be found [here](#sheet-options-and-other-functions). |
| 473 | |
| 474 | Use a tkinter color or a hex string e.g. |
| 475 | |
| 476 | ```python |
| 477 | my_sheet_widget.set_options(table_bg="black") |
| 478 | my_sheet_widget.set_options(table_bg="#000000") |
| 479 | my_sheet_widget.set_options(horizontal_scroll_pressed_bg="red") |
| 480 | ``` |
| 481 | |
| 482 | #### **Set options** |
| 483 | |
| 484 | ```python |
| 485 | set_options( |
| 486 | top_left_bg |
| 487 | top_left_fg |
| 488 | top_left_fg_highlight |
| 489 | |
| 490 | table_bg |
| 491 | table_grid_fg |
| 492 | table_fg |
| 493 | table_selected_box_cells_fg |
| 494 | table_selected_box_rows_fg |
| 495 | table_selected_box_columns_fg |
| 496 | table_selected_cells_border_fg |
| 497 | table_selected_cells_bg |
| 498 | table_selected_cells_fg |
| 499 | table_selected_rows_border_fg |
| 500 | table_selected_rows_bg |
| 501 | table_selected_rows_fg |
| 502 | table_selected_columns_border_fg |
| 503 | table_selected_columns_bg |
| 504 | table_selected_columns_fg |
| 505 | |
| 506 | header_bg |
| 507 | header_border_fg |
| 508 | header_grid_fg |
| 509 | header_fg |
| 510 | header_selected_cells_bg |
| 511 | header_selected_cells_fg |
| 512 | header_selected_columns_bg |
| 513 | header_selected_columns_fg |
| 514 | |
| 515 | index_bg |
| 516 | index_border_fg |
| 517 | index_grid_fg |
| 518 | index_fg |
| 519 | index_selected_cells_bg |
| 520 | index_selected_cells_fg |
| 521 | index_selected_rows_bg |
| 522 | index_selected_rows_fg |
| 523 | |
| 524 | resizing_line_fg |
| 525 | drag_and_drop_bg |
| 526 | outline_thickness |
| 527 | outline_color |
| 528 | frame_bg |
| 529 | popup_menu_font |
| 530 | popup_menu_fg |
| 531 | popup_menu_bg |
| 532 | popup_menu_highlight_bg |
| 533 | popup_menu_highlight_fg |
| 534 | |
| 535 | # scroll bars |
| 536 | vertical_scroll_background |
| 537 | horizontal_scroll_background |
| 538 | vertical_scroll_troughcolor |
| 539 | horizontal_scroll_troughcolor |
| 540 | vertical_scroll_lightcolor |
| 541 | horizontal_scroll_lightcolor |
| 542 | vertical_scroll_darkcolor |
| 543 | horizontal_scroll_darkcolor |
| 544 | vertical_scroll_bordercolor |
| 545 | horizontal_scroll_bordercolor |
| 546 | vertical_scroll_active_bg |
| 547 | horizontal_scroll_active_bg |
| 548 | vertical_scroll_not_active_bg |
| 549 | horizontal_scroll_not_active_bg |
| 550 | vertical_scroll_pressed_bg |
| 551 | horizontal_scroll_pressed_bg |
| 552 | vertical_scroll_active_fg |
| 553 | horizontal_scroll_active_fg |
| 554 | vertical_scroll_not_active_fg |
| 555 | horizontal_scroll_not_active_fg |
| 556 | vertical_scroll_pressed_fg |
| 557 | horizontal_scroll_pressed_fg |
| 558 | ) |
| 559 | ``` |
| 560 | |
| 561 | Otherwise you can change the theme using the below function. |
| 562 | ```python |
| 563 | change_theme(theme: str = "light blue", redraw: bool = True) -> Sheet |
| 564 | ``` |
| 565 | - `theme` (`str`) options (themes) are currently `"light blue"`, `"light green"`, `"dark"`, `"black"`, `"dark blue"` and `"dark green"`. |
| 566 | |
| 567 | ### **Scrollbar Appearance** |
| 568 | |
| 569 | **Scrollbar colors:** |
| 570 | |
| 571 | The above [function and keyword arguments](#set-options) can be used to change the colors of the scroll bars. |
| 572 | |
| 573 | **Scrollbar relief, size, arrows, etc.** |
| 574 | |
| 575 | Some scroll bar style options can only be changed on `Sheet()` initialization, others can be changed whenever using `set_options()`: |
| 576 | |
| 577 | - Options that can only be set in the `= Sheet(...)` initialization: |
| 578 | - `scrollbar_theme_inheritance: str = "default"` |
| 579 | - This is which tkinter theme to inherit the new style from, changing the width of the scroll bar might not work with the `"default"` theme. If this is the case try using `"clam"` instead. |
| 580 | - `scrollbar_show_arrows: bool` |
| 581 | - When `False` the scroll bars arrow buttons on either end will be hidden, this may effect the width of the scroll bar. |
| 582 | - Options that can be set using `set_options()` also: |
| 583 | - `vertical_scroll_borderwidth: int` |
| 584 | - `horizontal_scroll_borderwidth: int` |
| 585 | - `vertical_scroll_gripcount: int` |
| 586 | - `horizontal_scroll_gripcount: int` |
| 587 | - `vertical_scroll_arrowsize: str | int` |
| 588 | - `horizontal_scroll_arrowsize: str | int` |
| 589 | |
| 590 | --- |
| 591 | # **Alternate Row Colors** |
| 592 | |
| 593 | For basic alternate row colors in the main table either: |
| 594 | |
| 595 | - Use the `Sheet()` initialization keyword argument `alternate_color` (`str`) or |
| 596 | - Use the `set_options()` function with the keyword argument `alternate_color` |
| 597 | |
| 598 | Examples: |
| 599 | |
| 600 | ```python |
| 601 | set_options(alternate_color="#E2EAF4") |
| 602 | ``` |
| 603 | |
| 604 | ```python |
| 605 | my_sheet = Sheet(parent, alternate_color="gray80") |
| 606 | ``` |
| 607 | |
| 608 | Note that any cell, row or column highlights will display over alternate row colors. |
| 609 | |
| 610 | --- |
| 611 | # **Header and Index** |
| 612 | |
| 613 | #### **Set the header** |
| 614 | |
| 615 | ```python |
| 616 | set_header_data(value: object, c: int | None | Iterator = None, redraw: bool = True) -> Sheet |
| 617 | ``` |
| 618 | - `value` (`iterable`, `int`, `Any`) if `c` is left as `None` then it attempts to set the whole header as the `value` (converting a generator to a list). If `value` is `int` it sets the header to display the row with that position. |
| 619 | - `c` (`iterable`, `int`, `None`) if both `value` and `c` are iterables it assumes `c` is an iterable of positions and `value` is an iterable of values and attempts to set each value to each position. If `c` is `int` it attempts to set the value at that position. |
| 620 | |
| 621 | ```python |
| 622 | headers( |
| 623 | newheaders: object = None, |
| 624 | index: None | int = None, |
| 625 | reset_col_positions: bool = False, |
| 626 | show_headers_if_not_sheet: bool = True, |
| 627 | redraw: bool = True, |
| 628 | ) -> object |
| 629 | ``` |
| 630 | - Using an integer `int` for argument `newheaders` makes the sheet use that row as a header e.g. `headers(0)` means the first row will be used as a header (the first row will not be hidden in the sheet though), this is sort of equivalent to freezing the row. |
| 631 | - Leaving `newheaders` as `None` and using the `index` argument returns the existing header value in that index. |
| 632 | - Leaving all arguments as default e.g. `headers()` returns existing headers. |
| 633 | |
| 634 | ___ |
| 635 | |
| 636 | #### **Set the index** |
| 637 | |
| 638 | ```python |
| 639 | set_index_data(value: object, r: int | None | Iterator = None, redraw: bool = True) -> Sheet |
| 640 | ``` |
| 641 | - `value` (`iterable`, `int`, `Any`) if `r` is left as `None` then it attempts to set the whole index as the `value` (converting a generator to a list). If `value` is `int` it sets the index to display the row with that position. |
| 642 | - `r` (`iterable`, `int`, `None`) if both `value` and `r` are iterables it assumes `r` is an iterable of positions and `value` is an iterable of values and attempts to set each value to each position. If `r` is `int` it attempts to set the value at that position. |
| 643 | |
| 644 | ```python |
| 645 | row_index( |
| 646 | newindex: object = None, |
| 647 | index: None | int = None, |
| 648 | reset_row_positions: bool = False, |
| 649 | show_index_if_not_sheet: bool = True, |
| 650 | redraw: bool = True, |
| 651 | ) -> object |
| 652 | ``` |
| 653 | - Using an integer `int` for argument `newindex` makes the sheet use that column as an index e.g. `row_index(0)` means the first column will be used as an index (the first column will not be hidden in the sheet though), this is sort of equivalent to freezing the column. |
| 654 | - Leaving `newindex` as `None` and using the `index` argument returns the existing row index value in that index. |
| 655 | - Leaving all arguments as default e.g. `row_index()` returns the existing row index. |
| 656 | |
| 657 | --- |
| 658 | # **Text Wrap and Overflow** |
| 659 | |
| 660 | #### **Control text wrapping** |
| 661 | |
| 662 | You can set table, header and index text wrapping either at `Sheet()` initialization or using `set_options()`. |
| 663 | |
| 664 | Make use of the following parameters: |
| 665 | |
| 666 | - `table_wrap` |
| 667 | - `index_wrap` |
| 668 | - `header_wrap` |
| 669 | |
| 670 | With one of the following arguments: |
| 671 | |
| 672 | - `""` - For no text wrapping. |
| 673 | - `"c"` - For character wrapping. |
| 674 | - `"w"` - For word wrapping. |
| 675 | |
| 676 | **Examples:** |
| 677 | ```python |
| 678 | # for word wrap at initialization |
| 679 | my_sheet = Sheet(parent, table_wrap="w") |
| 680 | |
| 681 | # for character wrap using set_options() |
| 682 | my_sheet.set_options(table_wrap="c") |
| 683 | ``` |
| 684 | |
| 685 | #### **Control table text overflow** |
| 686 | |
| 687 | This setting only works for cells that are not center (north) aligned. Cell text can be set to overflow adjacent empty cells in the table like so: |
| 688 | |
| 689 | **Examples:** |
| 690 | ```python |
| 691 | # for word wrap at initialization |
| 692 | my_sheet = Sheet(parent, allow_cell_overflow=True) |
| 693 | |
| 694 | # for character wrap using set_options() |
| 695 | my_sheet.set_options(allow_cell_overflow=True) |
| 696 | ``` |
| 697 | - Set it to `False` to disable it. |
| 698 | - It is only available as a global setting for the table, not on a cell by cell basis. |
| 699 | |
| 700 | --- |
| 701 | # **Table functionality and bindings** |
| 702 | |
| 703 | #### **Enable table functionality and bindings** |
| 704 | |
| 705 | ```python |
| 706 | enable_bindings(*bindings) |
| 707 | ``` |
| 708 | - `bindings` (`str`) options are (rc stands for right click): |
| 709 | - `"all"` # enables all bindings with `single_select` mode, except the bindings that have to be specifically enabled by name. |
| 710 | - `"single_select"` # normal selection mode |
| 711 | - `"toggle_select"` # has issues but to enable a selection mode where cell/row/column selection is toggled |
| 712 | - `"drag_select"` # to allow mouse click and drag selection of cells/rows/columns |
| 713 | - `"select_all"` # drag_select also enables select_all |
| 714 | - `"column_drag_and_drop"` / `"move_columns"` # to allow drag and drop of columns |
| 715 | - `"row_drag_and_drop"` / `"move_rows"` # to allow drag and drop of rows |
| 716 | - `"column_select"` # to allow column selection |
| 717 | - `"row_select"` # to allow row selection |
| 718 | - `"column_width_resize"` # for resizing columns |
| 719 | - `"double_click_column_resize"` # for resizing columns to row text width |
| 720 | - `"row_width_resize"` # to resize the index width |
| 721 | - `"column_height_resize"` # to resize the header height |
| 722 | - `"arrowkeys"` # all arrowkeys including page up and down |
| 723 | - `"up"` # individual arrow key |
| 724 | - `"down"` # individual arrow key |
| 725 | - `"left"` # individual arrow key |
| 726 | - `"right"` # individual arrow key |
| 727 | - `"prior"` # page up |
| 728 | - `"next"` # page down |
| 729 | - `"row_height_resize"` # to resize rows |
| 730 | - `"double_click_row_resize"` # for resizing rows to row text height |
| 731 | - `"right_click_popup_menu"` / `"rc_popup_menu"` / `"rc_menu"` # for the in-built table context menu |
| 732 | - `"rc_select"` # for selecting cells using right click |
| 733 | - `"rc_insert_column"` # for a menu option to add columns |
| 734 | - `"rc_delete_column"` # for a menu option to delete columns |
| 735 | - `"rc_insert_row"` # for a menu option to add rows |
| 736 | - `"rc_delete_row"` # for a menu option to delete rows |
| 737 | - `"sort_cells"` |
| 738 | - `"sort_row"` |
| 739 | - `"sort_column"` / `"sort_col"` |
| 740 | - `"sort_rows"` |
| 741 | - `"sort_columns"` / `"sort_cols"` |
| 742 | - `"copy"` # for copying to clipboard |
| 743 | - `"cut"` # for cutting to clipboard |
| 744 | - `"paste"` # for pasting into the table |
| 745 | - `"delete"` # for clearing cells with the delete key |
| 746 | - `"undo"` # for undo and redo |
| 747 | - `"edit_cell"` # allow table cell editing |
| 748 | - `"find"` # for a pop-up find window (does not find in index or header) |
| 749 | - `"replace"` # additional functionality for the find window, replace and replace all |
| 750 | - *`"ctrl_click_select"` / `"ctrl_select"` # for selecting multiple non-adjacent cells/rows/columns |
| 751 | - *`"edit_header"` # allow header cell editing |
| 752 | - *`"edit_index"` # allow index cell editing |
| 753 | |
| 754 | *has to be specifically enabled - See Notes. |
| 755 | |
| 756 | Notes: |
| 757 | |
| 758 | - You can change the Sheets key bindings for functionality such as copy, paste, up, down etc. Instructions can be found [here](#changing-key-bindings). |
| 759 | - **Note** that the following functionalities are not enabled using `"all"` and have to be specifically enabled: |
| 760 | - `"ctrl_click_select"` / `"ctrl_select"` |
| 761 | - `"edit_header"` |
| 762 | - `"edit_index"` |
| 763 | - To allow table expansion when pasting data which doesn't fit in the table use either: |
| 764 | - `paste_can_expand_x=True`, `paste_can_expand_y=True` in sheet initialization arguments or the same keyword arguments with the function `set_options()`. |
| 765 | |
| 766 | Example: |
| 767 | |
| 768 | - `sheet.enable_bindings()` to enable everything except `"ctrl_select"`, `"edit_index"`, `"edit_header"`. |
| 769 | |
| 770 | ___ |
| 771 | |
| 772 | #### **Disable table functionality and bindings** |
| 773 | |
| 774 | ```python |
| 775 | disable_bindings(*bindings) |
| 776 | ``` |
| 777 | Notes: |
| 778 | |
| 779 | - Uses the same arguments as `enable_bindings()`. |
| 780 | |
| 781 | ___ |
| 782 | |
| 783 | #### **Bind specific table functionality** |
| 784 | |
| 785 | This function allows you to bind **very** specific table functionality to your own functions: |
| 786 | |
| 787 | - If you want less specificity in event names you can also bind all sheet modifying events to a single function, [see here](#tkinter-and-tksheet-events). |
| 788 | - If you want to validate/modify user cell edits [see here](#validate-user-cell-edits). |
| 789 | |
| 790 | ```python |
| 791 | extra_bindings( |
| 792 | bindings: str | list | tuple, |
| 793 | func: Callable | None = None, |
| 794 | ) -> Sheet |
| 795 | ``` |
| 796 | |
| 797 | **There are several ways to use this function:** |
| 798 | |
| 799 | - `bindings` as a `str` and `func` as either `None` or a function. Using `None` as an argument for `func` will effectively unbind the function. |
| 800 | - `extra_bindings("edit_cell", func=my_function)` |
| 801 | - `bindings` as an `iterable` of `str`s and `func` as either `None` or a function. Using `None` as an argument for `func` will effectively unbind the function. |
| 802 | - `extra_bindings(["all_select_events", "copy", "cut"], func=my_function)` |
| 803 | - `bindings` as an `iterable` of `list`s or `tuple`s with length of two, e.g. |
| 804 | - `extra_bindings([(binding, function), (binding, function), ...])` In this example you could also use `None` in the place of `function` to unbind the binding. |
| 805 | - In this case the arg `func` is totally ignored. |
| 806 | - For `"end_..."` events the bound function is run before the value is set. |
| 807 | - **To unbind** a function either set `func` argument to `None` or leave it as default e.g. `extra_bindings("begin_copy")` to unbind `"begin_copy"`. |
| 808 | - Even though undo/redo edits or adds or deletes rows/columns the bound functions for those actions will not be called. Undo/redo must be specifically bound in order for a function to be called. |
| 809 | |
| 810 | **`bindings` (`str`) options:** |
| 811 | |
| 812 | Undo/Redo: |
| 813 | |
| 814 | - `"begin_undo", "begin_ctrl_z"` |
| 815 | - `"ctrl_z", "end_undo", "end_ctrl_z", "undo"` |
| 816 | |
| 817 | Editing: |
| 818 | |
| 819 | - `"begin_copy", "begin_ctrl_c"` |
| 820 | - `"ctrl_c", "end_copy", "end_ctrl_c", "copy"` |
| 821 | - `"begin_cut", "begin_ctrl_x"` |
| 822 | - `"ctrl_x", "end_cut", "end_ctrl_x", "cut"` |
| 823 | - `"begin_paste", "begin_ctrl_v"` |
| 824 | - `"ctrl_v", "end_paste", "end_ctrl_v", "paste"` |
| 825 | - `"begin_delete_key", "begin_delete"` |
| 826 | - `"delete_key", "end_delete", "end_delete_key", "delete"` |
| 827 | - `"begin_edit_cell", "begin_edit_table"` |
| 828 | - `"end_edit_cell", "edit_cell", "edit_table"` |
| 829 | - `"begin_edit_header"` |
| 830 | - `"end_edit_header", "edit_header"` |
| 831 | - `"begin_edit_index"` |
| 832 | - `"end_edit_index", "edit_index"` |
| 833 | - `"replace_all"` |
| 834 | |
| 835 | Moving: |
| 836 | |
| 837 | - `"begin_row_index_drag_drop", "begin_move_rows"` |
| 838 | - `"row_index_drag_drop", "move_rows", "end_move_rows", "end_row_index_drag_drop"` |
| 839 | - `"begin_column_header_drag_drop", "begin_move_columns"` |
| 840 | - `"column_header_drag_drop", "move_columns", "end_move_columns", "end_column_header_drag_drop"` |
| 841 | - `"begin_sort_cells"` |
| 842 | - `"sort_cells", "end_sort_cells"` |
| 843 | - `"begin_sort_rows"` |
| 844 | - `"sort_rows", "end_sort_rows"` |
| 845 | - `"begin_sort_columns"` |
| 846 | - `"sort_columns", "end_sort_columns"` |
| 847 | |
| 848 | Deleting: |
| 849 | |
| 850 | - `"begin_rc_delete_row", "begin_delete_rows"` |
| 851 | - `"rc_delete_row", "end_rc_delete_row", "end_delete_rows", "delete_rows"` |
| 852 | - `"begin_rc_delete_column", "begin_delete_columns"` |
| 853 | - `"rc_delete_column", "end_rc_delete_column", "end_delete_columns", "delete_columns"` |
| 854 | |
| 855 | Adding: |
| 856 | |
| 857 | - `"begin_rc_insert_column", "begin_insert_column", "begin_insert_columns", "begin_add_column", "begin_rc_add_column", "begin_add_columns"` |
| 858 | - `"rc_insert_column", "end_rc_insert_column", "end_insert_column", "end_insert_columns", "rc_add_column", "end_rc_add_column", "end_add_column", "end_add_columns", "add_columns"` |
| 859 | - `"begin_rc_insert_row", "begin_insert_row", "begin_insert_rows", "begin_rc_add_row", "begin_add_row", "begin_add_rows"` |
| 860 | - `"rc_insert_row", "end_rc_insert_row", "end_insert_row", "end_insert_rows", "rc_add_row", "end_rc_add_row", "end_add_row", "end_add_rows", "add_rows"` |
| 861 | |
| 862 | Resizing rows/columns: |
| 863 | |
| 864 | - `"row_height_resize"` |
| 865 | - `"column_width_resize"` |
| 866 | |
| 867 | Selection: |
| 868 | |
| 869 | - `"cell_select"` |
| 870 | - `"all_select"` |
| 871 | - `"row_select"` |
| 872 | - `"column_select"` |
| 873 | - `"drag_select_cells"` |
| 874 | - `"drag_select_rows"` |
| 875 | - `"drag_select_columns"` |
| 876 | - `"shift_cell_select"` |
| 877 | - `"shift_row_select"` |
| 878 | - `"shift_column_select"` |
| 879 | - `"ctrl_cell_select"` |
| 880 | - `"ctrl_row_select"` |
| 881 | - `"ctrl_column_select"` |
| 882 | - `"deselect"` |
| 883 | |
| 884 | Event collections: |
| 885 | |
| 886 | - `"all_select_events", "select", "selectevents", "select_events"` |
| 887 | - `"all_modified_events", "sheetmodified", "sheet_modified" "modified_events", "modified"` |
| 888 | - `"bind_all"` |
| 889 | - `"unbind_all"` |
| 890 | |
| 891 | **Further Notes:** |
| 892 | |
| 893 | - `func` argument is the function you want to send the binding event to. |
| 894 | - Using one of the following `"all_modified_events"`, `"sheetmodified"`, `"sheet_modified"`, `"modified_events"`, `"modified"` will make any insert, delete or cell edit including pastes and undos send an event to your function. |
| 895 | - For events `"begin_move_columns"`/`"begin_move_rows"` the point where columns/rows will be moved to will be accessible by the key named `"value"`. |
| 896 | - For `"begin_edit..."` events the bound function must return a value to open the cell editor with, example [here](#example-custom-right-click-and-text-editor-validation). |
| 897 | |
| 898 | #### **Event Data** |
| 899 | |
| 900 | Using `extra_bindings()` the function you bind needs to have at least one argument which will receive a `dict`. The values of which can be accessed by dot notation e.g. `event.eventname` or `event.cells.table`: |
| 901 | |
| 902 | ```python |
| 903 | for (row, column), old_value in event.cells.table.items(): |
| 904 | print (f"R{row}", f"C{column}", "Old Value:", old_value) |
| 905 | ``` |
| 906 | |
| 907 | It has the following layout and keys: |
| 908 | |
| 909 | ```python |
| 910 | { |
| 911 | "eventname": "", |
| 912 | "sheetname": "", |
| 913 | "cells": { |
| 914 | "table": {}, |
| 915 | "header": {}, |
| 916 | "index": {}, |
| 917 | }, |
| 918 | "moved": { |
| 919 | "rows": {}, |
| 920 | "columns": {}, |
| 921 | }, |
| 922 | "added": { |
| 923 | "rows": {}, |
| 924 | "columns": {}, |
| 925 | }, |
| 926 | "deleted": { |
| 927 | "rows": {}, |
| 928 | "columns": {}, |
| 929 | "header": {}, |
| 930 | "index": {}, |
| 931 | "column_widths": {}, |
| 932 | "row_heights": {}, |
| 933 | "options": {}, |
| 934 | "displayed_columns": None, |
| 935 | "displayed_rows": None, |
| 936 | }, |
| 937 | "named_spans": {}, |
| 938 | "selection_boxes": {}, |
| 939 | "selected": tuple(), |
| 940 | "being_selected": tuple(), |
| 941 | "data": [], |
| 942 | "key": "", |
| 943 | "value": None, |
| 944 | "loc": tuple(), |
| 945 | "row": None, |
| 946 | "column": None, |
| 947 | "resized": { |
| 948 | "rows": {}, |
| 949 | "columns": {}, |
| 950 | }, |
| 951 | "widget": None, |
| 952 | } |
| 953 | ``` |
| 954 | |
| 955 | Keys: |
| 956 | |
| 957 | - A function bound using `extra_bindings()` will receive event data with one of the following **`["eventname"]`** keys: |
| 958 | - `"begin_ctrl_c"` |
| 959 | - `"end_ctrl_c"` |
| 960 | - `"begin_ctrl_x"` |
| 961 | - `"end_ctrl_x"` |
| 962 | - `"begin_ctrl_v"` |
| 963 | - `"end_ctrl_v"` |
| 964 | - `"begin_delete"` |
| 965 | - `"end_delete"` |
| 966 | - `"begin_undo"` |
| 967 | - `"end_undo"` |
| 968 | - `"begin_add_columns"` |
| 969 | - `"end_add_columns"` |
| 970 | - `"begin_add_rows"` |
| 971 | - `"end_add_rows"` |
| 972 | - `"begin_delete_columns"` |
| 973 | - `"end_delete_columns"` |
| 974 | - `"begin_delete_rows"` |
| 975 | - `"end_delete_rows"` |
| 976 | - `"begin_edit_table"` |
| 977 | - `"end_edit_table"` |
| 978 | - `"begin_edit_index"` |
| 979 | - `"end_edit_index"` |
| 980 | - `"begin_edit_header"` |
| 981 | - `"end_edit_header"` |
| 982 | - `"select"` |
| 983 | - `"resize"` |
| 984 | - *`"begin_move_rows"` |
| 985 | - *`"end_move_rows"` |
| 986 | - *`"begin_move_columns"` |
| 987 | - *`"end_move_columns"` |
| 988 | |
| 989 | - *is also used as the event name for sorting rows/columns events. |
| 990 | |
| 991 | - `EventDataDict`s will otherwise have one of the following event names: |
| 992 | - `"edit_table"` when a user has cut, paste, delete or made any cell edits including using dropdown boxes etc. in the table. |
| 993 | - `"edit_index"` when a user has edited a index cell. |
| 994 | - `"edit_header"` when a user has edited a header cell. |
| 995 | - `"add_columns"` when a user has inserted columns. |
| 996 | - `"add_rows"` when a user has inserted rows. |
| 997 | - `"delete_columns"` when a user has deleted columns. |
| 998 | - `"delete_rows"` when a user has deleted rows. |
| 999 | - `"move_columns"` when a user has dragged and dropped OR sorted columns. |
| 1000 | - `"move_rows"` when a user has dragged and dropped OR sorted rows. |
| 1001 | - `"select"` |
| 1002 | - `"resize"` |
| 1003 | - These event names would be used for `"<<SheetModified>>"` bound events for example. |
| 1004 | |
| 1005 | - For events `"begin_move_columns"`/`"begin_move_rows"` the point where columns/rows will be moved to will be under the `event_data` key `"value"`. |
| 1006 | - Key **`["sheetname"]`** is the [name given to the sheet widget on initialization](#initialization-options), useful if you have multiple sheets to determine which one emitted the event. |
| 1007 | - Key **`["cells"]["table"]`** if any table cells have been modified by cut, paste, delete, cell editors, dropdown boxes, check boxes, undo or redo this will be a `dict` with `tuple` keys of `(data row index: int, data column index: int)` and the values will be the cell values at that location **prior** to the change. The `dict` will be empty if no such changes have taken place. |
| 1008 | - Key **`["cells"]["header"]`** if any header cells have been modified by cell editors, dropdown boxes, check boxes, undo or redo this will be a `dict` with keys of `int: data column index` and the values will be the cell values at that location **prior** to the change. The `dict` will be empty if no such changes have taken place. |
| 1009 | - Key **`["cells"]["index"]`** if any index cells have been modified by cell editors, dropdown boxes, check boxes, undo or redo this will be a `dict` with keys of `int: data row index` and the values will be the cell values at that location **prior** to the change. The `dict` will be empty if no such changes have taken place. |
| 1010 | - Key **`["moved"]["rows"]`** if any rows have been moved by dragging and dropping or undoing/redoing of dragging and dropping rows this will be a `dict` with the following keys: |
| 1011 | - `{"data": {old data index: new data index, ...}, "displayed": {old displayed index: new displayed index, ...}}` |
| 1012 | - `"data"` will be a `dict` where the keys are the old data indexes of the rows and the values are the data indexes they have moved to. |
| 1013 | - `"displayed"` will be a `dict` where the keys are the old displayed indexes of the rows and the values are the displayed indexes they have moved to. |
| 1014 | - If no rows have been moved the `dict` under `["moved"]["rows"]` will be empty. |
| 1015 | - Note that if there are hidden rows the values for `"data"` will include all currently displayed row indexes and their new locations. If required and available, the values under `"displayed"` include only the directly moved rows, convert to data indexes using `Sheet.data_r()`. |
| 1016 | - For events `"begin_move_rows"` the point where rows will be moved to will be under the `event_data` key `"value"`. |
| 1017 | - Key **`["moved"]["columns"]`** if any columns have been moved by dragging and dropping or undoing/redoing of dragging and dropping columns this will be a `dict` with the following keys: |
| 1018 | - `{"data": {old data index: new data index, ...}, "displayed": {old displayed index: new displayed index, ...}}` |
| 1019 | - `"data"` will be a `dict` where the keys are the old data indexes of the columns and the values are the data indexes they have moved to. |
| 1020 | - `"displayed"` will be a `dict` where the keys are the old displayed indexes of the columns and the values are the displayed indexes they have moved to. |
| 1021 | - If no columns have been moved the `dict` under `["moved"]["columns"]` will be empty. |
| 1022 | - Note that if there are hidden columns the values for `"data"` will include all currently displayed column indexes and their new locations. If required and available, the values under `"displayed"` include only the directly moved columns, convert to data indexes using `Sheet.data_c()`. |
| 1023 | - For events `"begin_move_columns"` the point where columns will be moved to will be under the `event_data` key `"value"`. |
| 1024 | - Key **`["added"]["rows"]`** if any rows have been added by the inbuilt popup menu insert rows or by a paste which expands the sheet then this will be a `dict` with the following keys: |
| 1025 | - `{"data_index": int, "displayed_index": int, "num": int, "displayed": []}` |
| 1026 | - `"data_index"` is an `int` representing the row where the rows were added in the data. |
| 1027 | - `"displayed_index"` is an `int` representing the displayed table index where the rows were added (which will be different from the data index if there are hidden rows). |
| 1028 | - `"displayed"` is a copied list of the `Sheet()`s displayed rows immediately prior to the change. |
| 1029 | - If no rows have been added the `dict` will be empty. |
| 1030 | - Key **`["added"]["columns"]`** if any columns have been added by the inbuilt popup menu insert columns or by a paste which expands the sheet then this will be a `dict` with the following keys: |
| 1031 | - `{"data_index": int, "displayed_index": int, "num": int, "displayed": []}` |
| 1032 | - `"data_index"` is an `int` representing the column where the columns were added in the data. |
| 1033 | - `"displayed_index"` is an `int` representing the displayed table index where the columns were added (which will be different from the data index if there are hidden columns). |
| 1034 | - `"displayed"` is a copied list of the `Sheet()`s displayed columns immediately prior to the change. |
| 1035 | - If no columns have been added the `dict` will be empty. |
| 1036 | - Key **`["deleted"]["columns"]`** if any columns have been deleted by the inbuilt popup menu delete columns or by undoing a paste which added columns then this will be a `dict`. This `dict` will look like the following: |
| 1037 | - `{[column data index]: {[row data index]: cell value, [row data index]: cell value}, [column data index]: {...} ...}` |
| 1038 | - If no columns have been deleted then the `dict` value for `["deleted"]["columns"]` will be empty. |
| 1039 | - Key **`["deleted"]["rows"]`** if any rows have been deleted by the inbuilt popup menu delete rows or by undoing a paste which added rows then this will be a `dict`. This `dict` will look like the following: |
| 1040 | - `{[row data index]: {[column data index]: cell value, [column data index]: cell value}, [row data index]: {...} ...}` |
| 1041 | - If no rows have been deleted then the `dict` value for `["deleted"]["rows"]` will be empty. |
| 1042 | - Key **`["deleted"]["header"]`** if any header values have been deleted by the inbuilt popup menu delete columns or by undoing a paste which added columns and header values then this will be a `dict`. This `dict` will look like the following: |
| 1043 | - `{[column data index]: header cell value, [column data index]: header cell value, ...}` |
| 1044 | - If no columns have been deleted by the mentioned methods then the `dict` value for `["deleted"]["header"]` will be empty. |
| 1045 | - Key **`["deleted"]["index"]`** if any index values have been deleted by the inbuilt popup menu delete rows or by undoing a paste which added rows and index values then this will be a `dict`. This `dict` will look like the following: |
| 1046 | - `{[row data index]: index cell value, [row data index]: index cell value, ...}` |
| 1047 | - If no index values have been deleted by the mentioned methods then the `dict` value for `["deleted"]["index"]` will be empty. |
| 1048 | - Key **`["deleted"]["column_widths"]`** if any columns have been deleted by the inbuilt popup menu delete columns or by undoing a paste which added columns then this will be a `dict`. This `dict` will look like the following: |
| 1049 | - `{[column data index]: column width, [column data index]: column width, ...}` |
| 1050 | - If no columns have been deleted then the `dict` value for `["deleted"]["column_widths"]` will be empty. |
| 1051 | - Key **`["deleted"]["row_heights"]`** if any rows have been deleted by the inbuilt popup menu delete rows or by undoing a paste which added rows then this will be a `dict`. This `dict` will look like the following: |
| 1052 | - `{[row data index]: row height, [row data index]: row height, ...}` |
| 1053 | - If no rows have been deleted then the `dict` value for `["deleted"]["row_heights"]` will be empty. |
| 1054 | - Key **`["deleted"]["displayed_columns"]`** if any columns have been deleted by the inbuilt popup menu delete columns or by undoing a paste which added columns then this will be a `list`. This `list` stores the displayed columns (the columns that are showing when others are hidden) immediately prior to the change. |
| 1055 | - Key **`["deleted"]["displayed_rows"]`** if any rows have been deleted by the inbuilt popup menu delete rows or by undoing a paste which added rows then this will be a `list`. This `list` stores the displayed rows (the rows that are showing when others are hidden) immediately prior to the change. |
| 1056 | - Key **`["named_spans"]`** This `dict` serves as storage for the `Sheet()`s named spans. Each value in the `dict` is a pickled `span` object. |
| 1057 | - Key **`["options"]`** This serves as storage for the `Sheet()`s options such as highlights, formatting, alignments, dropdown boxes, check boxes etc. It is a `dict` where the values are the sheets internal cell/row/column options `dicts`. |
| 1058 | - Key **`["selection_boxes"]`** the value of this is all selection boxes on the sheet in the form of a `dict` as shown below: |
| 1059 | - For every event except `"select"` events the selection boxes are those immediately prior to the modification, for `"select"` events they are the current selection boxes. |
| 1060 | - The layout is always: `"selection_boxes": {(start row, start column, up to but not including row, up to but not including column): selection box type}`. |
| 1061 | - The row/column indexes are `int`s and the selection box type is a `str` either `"cells"`, `"rows"` or `"columns"`. |
| 1062 | - The `dict` will be empty if there is nothing selected. |
| 1063 | - Key **`["selected"]`** the value of this when there is something selected on the sheet is a `namedtuple`. The values of which can be found [here](#get-the-currently-selected-cell). |
| 1064 | - When nothing is selected or the event is not relevant to the currently selected box, such as a resize event it will be an empty `tuple`. |
| 1065 | - Key **`["being_selected"]`** if any selection box is in the process of being drawn by holding down mouse button 1 and dragging then this will be a tuple with the following layout: |
| 1066 | - `(start row, start column, up to but not including row, up to but not including column, selection box type)`. |
| 1067 | - The selection box type is a `str` either `"cells"`, `"rows"` or `"columns"`. |
| 1068 | - If no box is in the process of being created then this will be a an empty `tuple`. |
| 1069 | - [See here](#example-displaying-selections) for an example. |
| 1070 | - Key **`["data"]`** - `dict[tuple[int, int], Any]` - changed from only being used by paste, now stores a `dict` of cell coordinates and values that make up a table edit event of more than one cell. |
| 1071 | - Key **`["key"]`** - `str` - is primarily used for cell edit events where a key press has occurred. For `"begin_edit..."` events the value is the actual key which was pressed (or `"??"` for using the mouse to open a cell). It also might be one of the following for end edit events: |
| 1072 | - `"Return"` - enter key. |
| 1073 | - `"FocusOut"` - the editor or box lost focus, perhaps by mouse clicking elsewhere. |
| 1074 | - `"Tab"` - tab key. |
| 1075 | - Key **`["value"]`** is used primarily by cell editing events. For `"begin_edit..."` events it's the value displayed in he text editor when it opens. For `"end_edit..."` events it's the value in the text editor when it was closed, for example by hitting `Return`. It also used by `"begin_move_columns"`/`"begin_move_rows"` - the point where columns/rows will be moved to will be under the `event_data` key `"value"`. |
| 1076 | - Key **`["loc"]`** is for cell editing events to show the displayed (not data) coordinates of the event. It will be **either:** |
| 1077 | - A tuple of `(int displayed row index, int displayed column index)` in the case of editing table cells. |
| 1078 | - A single `int` in the case of editing index/header cells. |
| 1079 | - Key **`["row"]`** is for cell editing events to show the displayed (not data) row number `int` of the event. If the event was not a cell editing event or a header cell was edited the value will be `None`. |
| 1080 | - Key **`["column"]`** is for cell editing events to show the displayed (not data) column number `int` of the event. If the event was not a cell editing event or an index cell was edited the value will be `None`. |
| 1081 | - Key **`["resized"]["rows"]`** is for row height resizing events, it will be a `dict` with the following layout: |
| 1082 | - `{int displayed row index: {"old_size": old_height, "new_size": new_height}}`. |
| 1083 | - If no rows have been resized then the value for `["resized"]["rows"]` will be an empty `dict`. |
| 1084 | - Key **`["resized"]["columns"]`** is for column width resizing events, it will be a `dict` with the following layout: |
| 1085 | - `{int displayed column index: {"old_size": old_width, "new_size": new_width}}`. |
| 1086 | - If no columns have been resized then the value for `["resized"]["columns"]` will be an empty `dict`. |
| 1087 | - Key **`["widget"]`** will contain the widget which emitted the event, either the `MainTable()`, `ColumnHeaders()` or `RowIndex()` which are all `tk.Canvas` widgets. |
| 1088 | |
| 1089 | ___ |
| 1090 | |
| 1091 | #### **Validate user cell edits** |
| 1092 | |
| 1093 | With these functions you can validate or modify most user sheet edits, includes cut, paste, delete (including column/row clear), dropdown boxes and cell edits. |
| 1094 | |
| 1095 | **Edit validation** |
| 1096 | |
| 1097 | This function will be called for every cell edit in an action. |
| 1098 | |
| 1099 | ```python |
| 1100 | edit_validation(func: Callable | None = None) -> Sheet |
| 1101 | ``` |
| 1102 | Parameters: |
| 1103 | |
| 1104 | - `func` (`Callable`, `None`) must either be a function which will receive a tksheet event dict which looks like [this](#event-data) or `None` which unbinds the function. |
| 1105 | |
| 1106 | Notes: |
| 1107 | |
| 1108 | - If your bound function returns `None` then that specific cell edit will not be performed. |
| 1109 | - For examples of this function see [here](#usage-examples) and [here](#example-custom-right-click-and-text-editor-validation). |
| 1110 | |
| 1111 | **Bulk edit validation** |
| 1112 | |
| 1113 | This function will be called at the end of an action and delay any edits until after validation. |
| 1114 | |
| 1115 | ```python |
| 1116 | bulk_table_edit_validation(func: Callable | None = None) -> Sheet |
| 1117 | ``` |
| 1118 | Parameters: |
| 1119 | |
| 1120 | - `func` (`Callable`, `None`) must either be a function which will receive a tksheet event dict which looks like [this](#event-data) or `None` which unbinds the function. |
| 1121 | |
| 1122 | Notes: |
| 1123 | |
| 1124 | - See the below example for more information on usage. |
| 1125 | |
| 1126 | Example: |
| 1127 | |
| 1128 | ```python |
| 1129 | from tksheet import Sheet |
| 1130 | import tkinter as tk |
| 1131 | |
| 1132 | from typing import Any |
| 1133 | |
| 1134 | |
| 1135 | class demo(tk.Tk): |
| 1136 | def __init__(self): |
| 1137 | tk.Tk.__init__(self) |
| 1138 | self.grid_columnconfigure(0, weight=1) |
| 1139 | self.grid_rowconfigure(0, weight=1) |
| 1140 | self.frame = tk.Frame(self) |
| 1141 | self.frame.grid_columnconfigure(0, weight=1) |
| 1142 | self.frame.grid_rowconfigure(0, weight=1) |
| 1143 | self.sheet = Sheet( |
| 1144 | self.frame, |
| 1145 | data=[[f"Row {r}, Column {c}" for c in range(3)] for r in range(3)], |
| 1146 | ) |
| 1147 | self.sheet.enable_bindings() |
| 1148 | self.sheet.bulk_table_edit_validation(self.validate) |
| 1149 | self.frame.grid(row=0, column=0, sticky="nswe") |
| 1150 | self.sheet.grid(row=0, column=0, sticky="nswe") |
| 1151 | |
| 1152 | def validate(self, event: dict) -> Any: |
| 1153 | """ |
| 1154 | Whatever keys and values are left in event["data"] |
| 1155 | when the function returns are the edits that will be made |
| 1156 | |
| 1157 | An example below shows preventing edits if the proposed edit |
| 1158 | contains a space |
| 1159 | |
| 1160 | But you can also modify the values, or add more key, value |
| 1161 | pairs to event["data"] |
| 1162 | """ |
| 1163 | not_valid = set() |
| 1164 | for (r, c), value in event.data.items(): |
| 1165 | if " " in value: |
| 1166 | not_valid.add((r, c)) |
| 1167 | event.data = {k: v for k, v in event.data.items() if k not in not_valid} |
| 1168 | |
| 1169 | |
| 1170 | app = demo() |
| 1171 | app.mainloop() |
| 1172 | ``` |
| 1173 | |
| 1174 | ___ |
| 1175 | |
| 1176 | #### **Add commands to the in-built right click popup menu** |
| 1177 | |
| 1178 | ```python |
| 1179 | popup_menu_add_command( |
| 1180 | label: str, |
| 1181 | func: Callable, |
| 1182 | table_menu: bool = True, |
| 1183 | index_menu: bool = True, |
| 1184 | header_menu: bool = True, |
| 1185 | empty_space_menu: bool = True, |
| 1186 | ) -> Sheet |
| 1187 | ``` |
| 1188 | |
| 1189 | ___ |
| 1190 | |
| 1191 | #### **Remove commands added using popup_menu_add_command from the in-built right click popup menu** |
| 1192 | |
| 1193 | ```python |
| 1194 | popup_menu_del_command(label: str | None = None) -> Sheet |
| 1195 | ``` |
| 1196 | - If `label` is `None` then it removes all. |
| 1197 | |
| 1198 | ___ |
| 1199 | |
| 1200 | #### **Enable or disable mousewheel, left click etc** |
| 1201 | |
| 1202 | ```python |
| 1203 | basic_bindings(enable: bool = False) -> Sheet |
| 1204 | ``` |
| 1205 | |
| 1206 | ___ |
| 1207 | |
| 1208 | These functions are links to the Sheets own functionality. Functions such as `cut()` rely on whatever is currently selected on the Sheet. |
| 1209 | ```python |
| 1210 | cut(event: object = None) -> Sheet |
| 1211 | copy(event: object = None) -> Sheet |
| 1212 | paste(event: object = None) -> Sheet |
| 1213 | delete(event: object = None) -> Sheet |
| 1214 | undo(event: object = None) -> Sheet |
| 1215 | redo(event: object = None) -> Sheet |
| 1216 | zoom_in() -> Sheet |
| 1217 | zoom_out() -> Sheet |
| 1218 | ``` |
| 1219 | |
| 1220 | ___ |
| 1221 | |
| 1222 | #### **Get the last event data dict** |
| 1223 | |
| 1224 | ```python |
| 1225 | @property |
| 1226 | def event() -> EventDataDict |
| 1227 | ``` |
| 1228 | - e.g. `last_event_data = sheet.event` |
| 1229 | - Will be empty `EventDataDict` if there is no last event. |
| 1230 | |
| 1231 | ___ |
| 1232 | |
| 1233 | #### **Set focus to the Sheet** |
| 1234 | |
| 1235 | ```python |
| 1236 | focus_set( |
| 1237 | canvas: Literal[ |
| 1238 | "table", |
| 1239 | "header", |
| 1240 | "row_index", |
| 1241 | "index", |
| 1242 | "topleft", |
| 1243 | "top_left", |
| 1244 | ] = "table", |
| 1245 | ) -> Sheet |
| 1246 | ``` |
| 1247 | |
| 1248 | --- |
| 1249 | # **tkinter and tksheet Events** |
| 1250 | |
| 1251 | ### **Sheet bind** |
| 1252 | |
| 1253 | - With the `Sheet.bind()` function you can bind things in the usual way you would in tkinter and they will bind to all the `tksheet` canvases. |
| 1254 | - There are also the following special `tksheet` events you can bind: |
| 1255 | |
| 1256 | | Binding | Usable with `event_generate()` | |
| 1257 | | -------- | ------- | |
| 1258 | | `"<<SheetModified>>"` | - | |
| 1259 | | `"<<SheetRedrawn>>"` | - | |
| 1260 | | `"<<SheetSelect>>"` | - | |
| 1261 | | `"<<Copy>>"` | X | |
| 1262 | | `"<<Cut>>"` | X | |
| 1263 | | `"<<Paste>>"` | X | |
| 1264 | | `"<<Delete>>"` | X | |
| 1265 | | `"<<Undo>>"` | X | |
| 1266 | | `"<<Redo>>"` | X | |
| 1267 | | `"<<SelectAll>>"` | X | |
| 1268 | |
| 1269 | ```python |
| 1270 | bind( |
| 1271 | event: str, |
| 1272 | func: Callable, |
| 1273 | add: str | None = None, |
| 1274 | ) |
| 1275 | ``` |
| 1276 | Parameters: |
| 1277 | |
| 1278 | - `add` may or may not work for various bindings depending on whether they are already in use by `tksheet`. |
| 1279 | - **Note** that while a bound event after a paste/undo/redo might have the event name `"edit_table"` it also might have added/deleted rows/columns, refer to the docs on the event data `dict` for more information. |
| 1280 | - `event` the emitted events are: |
| 1281 | - `"<<SheetModified>>"` emitted whenever the sheet was modified by the end user by editing cells or adding or deleting rows/columns. The function you bind to this event must be able to receive a `dict` argument which will be the same as [the event data dict](#event-data) but with less specific event names. The possible event names are listed below: |
| 1282 | - `"edit_table"` when a user has cut, paste, delete or made any cell edits including using dropdown boxes etc. in the table. |
| 1283 | - `"edit_index"` when a user has edited a index cell. |
| 1284 | - `"edit_header"` when a user has edited a header cell. |
| 1285 | - `"add_columns"` when a user has inserted columns. |
| 1286 | - `"add_rows"` when a user has inserted rows. |
| 1287 | - `"delete_columns"` when a user has deleted columns. |
| 1288 | - `"delete_rows"` when a user has deleted rows. |
| 1289 | - `"move_columns"` when a user has dragged and dropped columns. |
| 1290 | - `"move_rows"` when a user has dragged and dropped rows. |
| 1291 | - `"sort_rows"` when rows have been re-ordered by sorting. |
| 1292 | - `"sort_columns"` when columns have been re-ordered by sorting. |
| 1293 | - `"<<SheetRedrawn>>"` emitted whenever the sheet GUI was refreshed (redrawn). The data for this event will be different than the usual event data, it is: |
| 1294 | - `{"sheetname": name of your sheet, "header": bool True if the header was redrawn, "row_index": bool True if the index was redrawn, "table": bool True if the the table was redrawn}` |
| 1295 | - `"<<SheetSelect>>"` encompasses all select events and emits the same event as `"<<SheetModified>>"` but with the event name: `"select"`. |
| 1296 | - `"<<Copy>>"` emitted when a Sheet copy e.g. `<Control-c>` was performed and will have the `eventname` `"copy"`. |
| 1297 | - `"<<Cut>>"` |
| 1298 | - `"<<Paste>>"` |
| 1299 | - `"<<Delete>>"` emitted when a Sheet delete key function was performed. |
| 1300 | - `"<<SelectAll>>"` |
| 1301 | - `"<<Undo>>"` |
| 1302 | - `"<<Redo>>"` |
| 1303 | |
| 1304 | Example: |
| 1305 | |
| 1306 | ```python |
| 1307 | # self.sheet_was_modified is your function |
| 1308 | self.sheet.bind("<<SheetModified>>", self.sheet_was_modified) |
| 1309 | ``` |
| 1310 | |
| 1311 | Example for `event_generate()`: |
| 1312 | ```python |
| 1313 | self.sheet.event_generate("<<Copy>>") |
| 1314 | ``` |
| 1315 | - Tells the sheet to run its copy function. |
| 1316 | |
| 1317 | ___ |
| 1318 | |
| 1319 | ### **Sheet unbind** |
| 1320 | |
| 1321 | With this function you can unbind things you have bound using the `bind()` function. |
| 1322 | ```python |
| 1323 | unbind(binding: str) -> Sheet |
| 1324 | ``` |
| 1325 | |
| 1326 | --- |
| 1327 | # **Sheet Languages and Bindings** |
| 1328 | |
| 1329 | In this section are instructions to change some of tksheets in-built language and bindings: |
| 1330 | |
| 1331 | - The in-built right click menu. |
| 1332 | - The in-built functionality keybindings, such as copy, paste etc. |
| 1333 | |
| 1334 | Please note that due to the limitations of the Tkinter Canvas tksheet doesn’t support right-to-left (RTL) languages. |
| 1335 | |
| 1336 | #### **Changing right click menu labels** |
| 1337 | |
| 1338 | You can change the labels for tksheets in-built right click popup menu by using the [`set_options()` function](#sheet-options-and-other-functions) with any of the following keyword arguments: |
| 1339 | |
| 1340 | ```python |
| 1341 | edit_header_label |
| 1342 | edit_header_accelerator |
| 1343 | edit_index_label |
| 1344 | edit_index_accelerator |
| 1345 | edit_cell_label |
| 1346 | edit_cell_accelerator |
| 1347 | cut_label |
| 1348 | cut_accelerator |
| 1349 | cut_contents_label |
| 1350 | cut_contents_accelerator |
| 1351 | copy_label |
| 1352 | copy_accelerator |
| 1353 | copy_contents_label |
| 1354 | copy_contents_accelerator |
| 1355 | paste_label |
| 1356 | paste_accelerator |
| 1357 | delete_label |
| 1358 | delete_accelerator |
| 1359 | clear_contents_label |
| 1360 | clear_contents_accelerator |
| 1361 | delete_columns_label |
| 1362 | delete_columns_accelerator |
| 1363 | insert_columns_left_label |
| 1364 | insert_columns_left_accelerator |
| 1365 | insert_column_label |
| 1366 | insert_column_accelerator |
| 1367 | insert_columns_right_label |
| 1368 | insert_columns_right_accelerator |
| 1369 | delete_rows_label |
| 1370 | delete_rows_accelerator |
| 1371 | insert_rows_above_label |
| 1372 | insert_rows_above_accelerator |
| 1373 | insert_rows_below_label |
| 1374 | insert_rows_below_accelerator |
| 1375 | insert_row_label |
| 1376 | insert_row_accelerator |
| 1377 | select_all_label |
| 1378 | select_all_accelerator |
| 1379 | undo_label |
| 1380 | undo_accelerator |
| 1381 | |
| 1382 | sort_cells_label |
| 1383 | sort_cells_x_label |
| 1384 | sort_row_label |
| 1385 | sort_column_label |
| 1386 | sort_rows_label |
| 1387 | sort_columns_label |
| 1388 | sort_cells_reverse_label |
| 1389 | sort_cells_x_reverse_label |
| 1390 | sort_row_reverse_label |
| 1391 | sort_column_reverse_label |
| 1392 | sort_rows_reverse_label |
| 1393 | sort_columns_reverse_label |
| 1394 | sort_cells_accelerator |
| 1395 | sort_cells_x_accelerator |
| 1396 | sort_row_accelerator |
| 1397 | sort_column_accelerator |
| 1398 | sort_rows_accelerator |
| 1399 | sort_columns_accelerator |
| 1400 | sort_cells_reverse_accelerator |
| 1401 | sort_cells_x_reverse_accelerator |
| 1402 | sort_row_reverse_accelerator |
| 1403 | sort_column_reverse_accelerator |
| 1404 | sort_rows_reverse_accelerator |
| 1405 | sort_columns_reverse_accelerator |
| 1406 | ``` |
| 1407 | |
| 1408 | Example: |
| 1409 | |
| 1410 | ```python |
| 1411 | # changing the copy label to the spanish for Copy |
| 1412 | sheet.set_options(copy_label="Copiar") |
| 1413 | ``` |
| 1414 | |
| 1415 | #### **Changing key bindings** |
| 1416 | |
| 1417 | You can change the bindings for tksheets in-built functionality such as cut, copy, paste by using the [`set_options()` function](#sheet-options-and-other-functions) with any the following keyword arguments: |
| 1418 | |
| 1419 | ```python |
| 1420 | copy_bindings |
| 1421 | cut_bindings |
| 1422 | paste_bindings |
| 1423 | undo_bindings |
| 1424 | redo_bindings |
| 1425 | delete_bindings |
| 1426 | select_all_bindings |
| 1427 | select_columns_bindings |
| 1428 | select_rows_bindings |
| 1429 | row_start_bindings |
| 1430 | table_start_bindings |
| 1431 | tab_bindings |
| 1432 | up_bindings |
| 1433 | right_bindings |
| 1434 | down_bindings |
| 1435 | left_bindings |
| 1436 | shift_up_bindings |
| 1437 | shift_right_bindings |
| 1438 | shift_down_bindings |
| 1439 | shift_left_bindings |
| 1440 | prior_bindings |
| 1441 | next_bindings |
| 1442 | find_bindings |
| 1443 | find_next_bindings |
| 1444 | find_previous_bindings |
| 1445 | escape_bindings |
| 1446 | toggle_replace_bindings |
| 1447 | ``` |
| 1448 | |
| 1449 | The argument must be a `list` of **tkinter** binding `str`s. In the below example the binding for copy is changed to `"<Control-e>"` and `"<Control-E>"`. |
| 1450 | |
| 1451 | ```python |
| 1452 | # changing the binding for copy |
| 1453 | sheet.set_options(copy_bindings=["<Control-e>", "<Control-E>"]) |
| 1454 | ``` |
| 1455 | |
| 1456 | The default values for these bindings can be found in the tksheet file `sheet_options.py`. |
| 1457 | |
| 1458 | #### **Key bindings for other languages** |
| 1459 | |
| 1460 | There is limited support in tkinter for keybindings in languages other than english, for example tkinters `.bind()` function doesn't cooperate with cyrillic characters. |
| 1461 | |
| 1462 | There are ways around this however, see below for a limited example of how this might be achieved: |
| 1463 | |
| 1464 | ```python |
| 1465 | from __future__ import annotations |
| 1466 | |
| 1467 | import tkinter as tk |
| 1468 | |
| 1469 | from tksheet import Sheet |
| 1470 | |
| 1471 | |
| 1472 | class demo(tk.Tk): |
| 1473 | def __init__(self) -> None: |
| 1474 | tk.Tk.__init__(self) |
| 1475 | self.grid_columnconfigure(0, weight=1) |
| 1476 | self.grid_rowconfigure(0, weight=1) |
| 1477 | self.sheet = Sheet( |
| 1478 | parent=self, |
| 1479 | data=[[f"{r} {c}" for c in range(5)] for r in range(5)], |
| 1480 | ) |
| 1481 | self.sheet.enable_bindings() |
| 1482 | self.sheet.grid(row=0, column=0, sticky="nswe") |
| 1483 | self.bind_all("<Key>", self.any_key) |
| 1484 | |
| 1485 | def any_key(self, event: tk.Event) -> None: |
| 1486 | """ |
| 1487 | Establish that the Control key is held down |
| 1488 | """ |
| 1489 | ctrl = (event.state & 4 > 0) |
| 1490 | if not ctrl: |
| 1491 | return |
| 1492 | """ |
| 1493 | From here you can use event.keycode and event.keysym to determine |
| 1494 | which key has been pressed along with Control |
| 1495 | """ |
| 1496 | print(event.keycode) |
| 1497 | print(event.keysym) |
| 1498 | """ |
| 1499 | If the keys are the ones you want to have bound to Sheet functionality |
| 1500 | You can then call the Sheets functionality using event_generate() |
| 1501 | For example: |
| 1502 | """ |
| 1503 | # if the key is correct then: |
| 1504 | self.sheet.event_generate("<<Copy>>") |
| 1505 | |
| 1506 | |
| 1507 | app = demo() |
| 1508 | app.mainloop() |
| 1509 | ``` |
| 1510 | |
| 1511 | --- |
| 1512 | # **Span Objects** |
| 1513 | |
| 1514 | In `tksheet` versions > `7` there are functions which utilise an object named `Span`. These objects are a subclass of `dict` but with various additions and dot notation attribute access. |
| 1515 | |
| 1516 | Spans basically represent an **contiguous** area of the sheet. They can be **one** of three **kinds**: |
| 1517 | |
| 1518 | - `"cell"` |
| 1519 | - `"row"` |
| 1520 | - `"column"` |
| 1521 | |
| 1522 | They can be used with some of the sheets functions such as data getting/setting and creation of things on the sheet such as dropdown boxes. |
| 1523 | |
| 1524 | Spans store: |
| 1525 | |
| 1526 | - A reference to the `Sheet()` they were created with. |
| 1527 | - Variables which represent a particular range of cells and properties for accessing these ranges. |
| 1528 | - Variables which represent options for those cells. |
| 1529 | - Methods which can modify the above variables. |
| 1530 | - Methods which can act upon the table using the above variables such as `highlight`, `format`, etc. |
| 1531 | |
| 1532 | Whether cells, rows or columns are affected will depend on the spans [`kind`](#get-a-spans-kind). |
| 1533 | |
| 1534 | ### **Creating a span** |
| 1535 | |
| 1536 | You can create a span by: |
| 1537 | |
| 1538 | - Using the `span()` function e.g. `sheet.span("A1")` represents the cell `A1` |
| 1539 | |
| 1540 | **or** |
| 1541 | |
| 1542 | - Using square brackets on a Sheet object e.g. `sheet["A1"]` represents the cell `A1` |
| 1543 | |
| 1544 | Both methods return the created span object. |
| 1545 | |
| 1546 | ```python |
| 1547 | span( |
| 1548 | *key: CreateSpanTypes, |
| 1549 | type_: str = "", |
| 1550 | name: str = "", |
| 1551 | table: bool = True, |
| 1552 | index: bool = False, |
| 1553 | header: bool = False, |
| 1554 | tdisp: bool = False, |
| 1555 | idisp: bool = True, |
| 1556 | hdisp: bool = True, |
| 1557 | transposed: bool = False, |
| 1558 | ndim: int = 0, |
| 1559 | convert: object = None, |
| 1560 | undo: bool = True, |
| 1561 | emit_event: bool = False, |
| 1562 | widget: object = None, |
| 1563 | expand: None | str = None, |
| 1564 | formatter_options: dict | None = None, |
| 1565 | **kwargs, |
| 1566 | ) -> Span |
| 1567 | """ |
| 1568 | Create a span / get an existing span by name |
| 1569 | Returns the created span |
| 1570 | """ |
| 1571 | ``` |
| 1572 | Parameters: |
| 1573 | |
| 1574 | - `key` you do not have to provide an argument for `key`, if no argument is provided then the span will be a full sheet span. Otherwise `key` can be the following types which are type hinted as `CreateSpanTypes`: |
| 1575 | - `None` |
| 1576 | - `str` e.g. `sheet.span("A1:F1")` |
| 1577 | - `int` e.g. `sheet.span(0)` |
| 1578 | - `slice` e.g. `sheet.span(slice(0, 4))` |
| 1579 | - `Sequence[int | None, int | None]` representing a cell of `row, column` e.g. `sheet.span(0, 0)` |
| 1580 | - `Sequence[Sequence[int | None, int | None], Sequence[int | None, int | None]]` representing `sheet.span(start row, start column, up to but not including row, up to but not including column)` e.g. `sheet.span(0, 0, 2, 2)` |
| 1581 | - `Span` e.g `sheet.span(another_span)` |
| 1582 | - `type_` (`str`) must be either an empty string `""` or one of the following: `"format"`, `"highlight"`, `"dropdown"`, `"checkbox"`, `"readonly"`, `"align"`. |
| 1583 | - `name` (`str`) used for named spans or for identification. If no name is provided then a name is generated for the span which is based on an internal integer ticker and then converted to a string in the same way column names are. |
| 1584 | - `table` (`bool`) when `True` will make all functions used with the span target the main table as well as the header/index if those are `True`. |
| 1585 | - `index` (`bool`) when `True` will make all functions used with the span target the index as well as the table/header if those are `True`. |
| 1586 | - `header` (`bool`) when `True` will make all functions used with the span target the header as well as the table/index if those are `True`. |
| 1587 | - `tdisp` (`bool`) is used by data getting functions that utilize spans and when `True` the function retrieves screen displayed data for the table, not underlying cell data. |
| 1588 | - `idisp` (`bool`) is used by data getting functions that utilize spans and when `True` the function retrieves screen displayed data for the index, not underlying cell data. |
| 1589 | - `hdisp` (`bool`) is used by data getting functions that utilize spans and when `True` the function retrieves screen displayed data for the header, not underlying cell data. |
| 1590 | - `transposed` (`bool`) is used by data getting and setting functions that utilize spans. When `True`: |
| 1591 | - Returned sublists from data getting functions will represent columns rather than rows. |
| 1592 | - Data setting functions will assume that a single sequence is a column rather than row and that a list of lists is a list of columns rather than a list of rows. |
| 1593 | - `ndim` (`int`) is used by data getting functions that utilize spans, it must be either `0` or `1` or `2`. |
| 1594 | - `0` is the default setting which will make the return value vary based on what it is. For example if the gathered data is only a single cell it will return a value instead of a list of lists with a single list containing a single value. A single row will be a single list. |
| 1595 | - `1` will force the return of a single list as opposed to a list of lists. |
| 1596 | - `2` will force the return of a list of lists. |
| 1597 | - `convert` (`None`, `Callable`) can be used to modify the data using a function before returning it. The data sent to the `convert` function will be as it was before normally returning (after `ndim` has potentially modified it). |
| 1598 | - `undo` (`bool`) is used by data modifying functions that utilize spans. When `True` and if undo is enabled for the sheet then the end user will be able to undo/redo the modification. |
| 1599 | - `emit_event` when `True` and when using data setting functions that utilize spans causes a `"<<SheetModified>>` event to occur if it has been bound, see [here](#tkinter-and-tksheet-events) for more information on binding this event. |
| 1600 | - `widget` (`object`) is the reference to the original sheet which created the span. This can be changed to a different sheet if required e.g. `my_span.widget = new_sheet`. |
| 1601 | - `expand` (`None`, `str`) must be either `None` or: |
| 1602 | - `"table"`/`"both"` expand the span both down and right from the span start to the ends of the table. |
| 1603 | - `"right"` expand the span right to the end of the table `x` axis. |
| 1604 | - `"down"` expand the span downwards to the bottom of the table `y` axis. |
| 1605 | - `formatter_options` (`dict`, `None`) must be either `None` or `dict`. If providing a `dict` it must be the same structure as used in format functions, see [here](#data-formatting) for more information. Used to turn the span into a format type span which: |
| 1606 | - When using `get_data()` will format the returned data. |
| 1607 | - When using `set_data()` will format the data being set but **NOT** create a new formatting rule on the sheet. |
| 1608 | - `**kwargs` you can provide additional keyword arguments to the function for example those used in `span.highlight()` or `span.dropdown()` which are used when applying a named span to a table. |
| 1609 | |
| 1610 | Notes: |
| 1611 | |
| 1612 | - To create a named span see [here](#named-spans). |
| 1613 | |
| 1614 | #### **Span creation syntax** |
| 1615 | |
| 1616 | **When creating a span using the below methods:** |
| 1617 | |
| 1618 | - `str`s use excel syntax and the indexing rule of up to **AND** including. |
| 1619 | - `int`s use python syntax and the indexing rule of up to but **NOT** including. |
| 1620 | |
| 1621 | For example python index `0` as in `[0]` is the first whereas excel index `1` as in `"A1"` is the first. |
| 1622 | |
| 1623 | If you need to convert python indexes into column letters you can use the function `num2alpha` importable from `tksheet`: |
| 1624 | |
| 1625 | ```python |
| 1626 | from tksheet import ( |
| 1627 | Sheet, |
| 1628 | num2alpha as n2a, |
| 1629 | ) |
| 1630 | |
| 1631 | # column index five as a letter |
| 1632 | n2a(5) |
| 1633 | ``` |
| 1634 | |
| 1635 | #### **Span creation examples using square brackets** |
| 1636 | |
| 1637 | ```python |
| 1638 | """ |
| 1639 | EXAMPLES USING SQUARE BRACKETS |
| 1640 | """ |
| 1641 | |
| 1642 | span = sheet[0] # first row |
| 1643 | span = sheet["1"] # first row |
| 1644 | |
| 1645 | span = sheet[0:2] # first two rows |
| 1646 | span = sheet["1:2"] # first two rows |
| 1647 | |
| 1648 | span = sheet[:] # entire sheet |
| 1649 | span = sheet[":"] # entire sheet |
| 1650 | |
| 1651 | span = sheet[:2] # first two rows |
| 1652 | span = sheet[":2"] # first two rows |
| 1653 | |
| 1654 | """ THESE TWO HAVE DIFFERENT OUTCOMES """ |
| 1655 | span = sheet[2:] # all rows after and not inlcuding python index 1 |
| 1656 | span = sheet["2:"] # all rows after and not including python index 0 |
| 1657 | |
| 1658 | span = sheet["A"] # first column |
| 1659 | span = sheet["A:C"] # first three columns |
| 1660 | |
| 1661 | """ SOME CELL AREA EXAMPLES """ |
| 1662 | span = sheet[0, 0] # cell A1 |
| 1663 | span = sheet[(0, 0)] # cell A1 |
| 1664 | span = sheet["A1:C1"] # cells A1, B1, C1 |
| 1665 | span = sheet[0, 0, 1, 3] # cells A1, B1, C1 |
| 1666 | span = sheet[(0, 0, 1, 3)] # cells A1, B1, C1 |
| 1667 | span = sheet[(0, 0), (1, 3)] # cells A1, B1, C1 |
| 1668 | span = sheet[((0, 0), (1, 3))] # cells A1, B1, C1 |
| 1669 | |
| 1670 | span = sheet["A1:2"] |
| 1671 | span = sheet[0, 0, 2, None] |
| 1672 | """ |
| 1673 | ["A1:2"] |
| 1674 | All the cells starting from (0, 0) |
| 1675 | expanding down to include row 1 |
| 1676 | but not including cells beyond row |
| 1677 | 1 and expanding out to include all |
| 1678 | columns |
| 1679 | |
| 1680 | A B C D |
| 1681 | 1 x x x x |
| 1682 | 2 x x x x |
| 1683 | 3 |
| 1684 | 4 |
| 1685 | ... |
| 1686 | """ |
| 1687 | |
| 1688 | span = sheet["A1:B"] |
| 1689 | span = sheet[0, 0, None, 2] |
| 1690 | """ |
| 1691 | ["A1:B"] |
| 1692 | All the cells starting from (0, 0) |
| 1693 | expanding out to include column 1 |
| 1694 | but not including cells beyond column |
| 1695 | 1 and expanding down to include all |
| 1696 | rows |
| 1697 | |
| 1698 | A B C D |
| 1699 | 1 x x |
| 1700 | 2 x x |
| 1701 | 3 x x |
| 1702 | 4 x x |
| 1703 | ... |
| 1704 | """ |
| 1705 | |
| 1706 | """ GETTING AN EXISTING NAMED SPAN """ |
| 1707 | # you can retrieve an existing named span quickly by surrounding its name in <> e.g. |
| 1708 | named_span_retrieval = sheet["<the name of the span goes here>"] |
| 1709 | ``` |
| 1710 | |
| 1711 | #### **Span creation examples using sheet.span()** |
| 1712 | |
| 1713 | ```python |
| 1714 | """ |
| 1715 | EXAMPLES USING span() |
| 1716 | """ |
| 1717 | |
| 1718 | """ |
| 1719 | USING NO ARGUMENTS |
| 1720 | """ |
| 1721 | sheet.span() # entire sheet, in this case not including header or index |
| 1722 | |
| 1723 | """ |
| 1724 | USING ONE ARGUMENT |
| 1725 | |
| 1726 | str or int or slice() |
| 1727 | """ |
| 1728 | |
| 1729 | # with one argument you can use the same string syntax used for square bracket span creation |
| 1730 | sheet.span("A1") |
| 1731 | sheet.span(0) # row at python index 0, all columns |
| 1732 | sheet.span(slice(0, 2)) # rows at python indexes 0 and 1, all columns |
| 1733 | sheet.span(":") # entire sheet |
| 1734 | |
| 1735 | """ |
| 1736 | USING TWO ARGUMENTS |
| 1737 | int | None, int | None |
| 1738 | |
| 1739 | or |
| 1740 | |
| 1741 | (int | None, int | None), (int | None, int | None) |
| 1742 | """ |
| 1743 | sheet.span(0, 0) # row 0, column 0 - the first cell |
| 1744 | sheet.span(0, None) # row 0, all columns |
| 1745 | sheet.span(None, 0) # column 0, all rows |
| 1746 | |
| 1747 | sheet.span((0, 0), (1, 1)) # row 0, column 0 - the first cell |
| 1748 | sheet.span((0, 0), (None, 2)) # rows 0 - end, columns 0 and 1 |
| 1749 | |
| 1750 | """ |
| 1751 | USING FOUR ARGUMENTS |
| 1752 | int | None, int | None, int | None, int | None |
| 1753 | """ |
| 1754 | |
| 1755 | sheet.span(0, 0, 1, 1) # row 0, column 0 - the first cell |
| 1756 | sheet.span(0, 0, None, 2) # rows 0 - end, columns 0 and 1 |
| 1757 | ``` |
| 1758 | |
| 1759 | ### **Span properties** |
| 1760 | |
| 1761 | Spans have a few `@property` functions: |
| 1762 | |
| 1763 | - `span.kind` |
| 1764 | - `span.rows` |
| 1765 | - `span.columns` |
| 1766 | - `span.coords` |
| 1767 | |
| 1768 | #### **Get a spans kind** |
| 1769 | |
| 1770 | ```python |
| 1771 | span.kind |
| 1772 | ``` |
| 1773 | - Returns either `"cell"`, `"row"` or `"column"`. |
| 1774 | |
| 1775 | ```python |
| 1776 | span = sheet.span("A1:C4") |
| 1777 | print (span.kind) |
| 1778 | # prints "cell" |
| 1779 | |
| 1780 | span = sheet.span(":") |
| 1781 | print (span.kind) |
| 1782 | # prints "cell" |
| 1783 | |
| 1784 | span = sheet.span("1:3") |
| 1785 | print (span.kind) |
| 1786 | # prints "row" |
| 1787 | |
| 1788 | span = sheet.span("A:C") |
| 1789 | print (span.kind) |
| 1790 | # prints "column" |
| 1791 | |
| 1792 | # after importing num2alpha from tksheet |
| 1793 | print (sheet[num2alpha(0)].kind) |
| 1794 | # prints "column" |
| 1795 | ``` |
| 1796 | |
| 1797 | #### **Get span rows and columns** |
| 1798 | |
| 1799 | ```python |
| 1800 | span.rows |
| 1801 | span.columns |
| 1802 | ``` |
| 1803 | Returns a `SpanRange` object. The below examples are for `span.rows` but you can use `span.columns` for the spans columns exactly the same way. |
| 1804 | |
| 1805 | ```python |
| 1806 | # use as an iterator |
| 1807 | span = sheet.span("A1:C4") |
| 1808 | for row in span.rows: |
| 1809 | pass |
| 1810 | # use as a reversed iterator |
| 1811 | for row in reversed(span.rows): |
| 1812 | pass |
| 1813 | |
| 1814 | # check row membership |
| 1815 | span = sheet.span("A1:C4") |
| 1816 | print (2 in span.rows) |
| 1817 | # prints True |
| 1818 | |
| 1819 | # check span.rows equality, also can do not equal |
| 1820 | span = self.sheet["A1:C4"] |
| 1821 | span2 = self.sheet["1:4"] |
| 1822 | print (span.rows == span2.rows) |
| 1823 | # prints True |
| 1824 | |
| 1825 | # check len |
| 1826 | span = self.sheet["A1:C4"] |
| 1827 | print (len(span.rows)) |
| 1828 | # prints 4 |
| 1829 | ``` |
| 1830 | |
| 1831 | ### **Span methods** |
| 1832 | |
| 1833 | Spans have the following methods, all of which return the span object itself so you can chain the functions e.g. `span.options(undo=True).clear().bg = "indianred1"` |
| 1834 | |
| 1835 | #### **Modify a spans attributes** |
| 1836 | |
| 1837 | ```python |
| 1838 | span.options( |
| 1839 | type_: str | None = None, |
| 1840 | name: str | None = None, |
| 1841 | table: bool | None = None, |
| 1842 | index: bool | None = None, |
| 1843 | header: bool | None = None, |
| 1844 | tdisp: bool | None = None, |
| 1845 | idisp: bool | None = None, |
| 1846 | hdisp: bool | None = None, |
| 1847 | transposed: bool | None = None, |
| 1848 | ndim: int | None = None, |
| 1849 | convert: Callable | None = None, |
| 1850 | undo: bool | None = None, |
| 1851 | emit_event: bool | None = None, |
| 1852 | widget: object = None, |
| 1853 | expand: str | None = None, |
| 1854 | formatter_options: dict | None = None, |
| 1855 | **kwargs, |
| 1856 | ) -> Span |
| 1857 | ``` |
| 1858 | Note that if `None` is used for any of the following parameters then that `Span`s attribute will be unchanged: |
| 1859 | |
| 1860 | - `type_` (`str`, `None`) if not `None` then must be either an empty string `""` or one of the following: `"format"`, `"highlight"`, `"dropdown"`, `"checkbox"`, `"readonly"`, `"align"`. |
| 1861 | - `name` (`str`, `None`) is used for named spans or for identification. |
| 1862 | - `table` (`bool`, `None`) when `True` will make all functions used with the span target the main table as well as the header/index if those are `True`. |
| 1863 | - `index` (`bool`, `None`) when `True` will make all functions used with the span target the index as well as the table/header if those are `True`. |
| 1864 | - `header` (`bool`, `None`) when `True` will make all functions used with the span target the header as well as the table/index if those are `True`. |
| 1865 | - `tdisp` (`bool`, `None`) is used by data getting functions that utilize spans and when `True` the function retrieves screen displayed data for the table, not underlying cell data. |
| 1866 | - `idisp` (`bool`, `None`) is used by data getting functions that utilize spans and when `True` the function retrieves screen displayed data for the index, not underlying cell data. |
| 1867 | - `hdisp` (`bool`, `None`) is used by data getting functions that utilize spans and when `True` the function retrieves screen displayed data for the header, not underlying cell data. |
| 1868 | - `transposed` (`bool`, `None`) is used by data getting and setting functions that utilize spans. When `True`: |
| 1869 | - Returned sublists from data getting functions will represent columns rather than rows. |
| 1870 | - Data setting functions will assume that a single sequence is a column rather than row and that a list of lists is a list of columns rather than a list of rows. |
| 1871 | - `ndim` (`int`, `None`) is used by data getting functions that utilize spans, it must be either `0` or `1` or `2`. |
| 1872 | - `0` is the default setting which will make the return value vary based on what it is. For example if the gathered data is only a single cell it will return a value instead of a list of lists with a single list containing a single value. A single row will be a single list. |
| 1873 | - `1` will force the return of a single list as opposed to a list of lists. |
| 1874 | - `2` will force the return of a list of lists. |
| 1875 | - `convert` (`Callable`, `None`) can be used to modify the data using a function before returning it. The data sent to the `convert` function will be as it was before normally returning (after `ndim` has potentially modified it). |
| 1876 | - `undo` (`bool`, `None`) is used by data modifying functions that utilize spans. When `True` and if undo is enabled for the sheet then the end user will be able to undo/redo the modification. |
| 1877 | - `emit_event` (`bool`, `None`) is used by data modifying functions that utilize spans. When `True` causes a `"<<SheetModified>>` event to occur if it has been bound, see [here](#tkinter-and-tksheet-events) for more information. |
| 1878 | - `widget` (`object`) is the reference to the original sheet which created the span. This can be changed to a different sheet if required e.g. `my_span.widget = new_sheet`. |
| 1879 | - `expand` (`str`, `None`) must be either `None` or: |
| 1880 | - `"table"`/`"both"` expand the span both down and right from the span start to the ends of the table. |
| 1881 | - `"right"` expand the span right to the end of the table `x` axis. |
| 1882 | - `"down"` expand the span downwards to the bottom of the table `y` axis. |
| 1883 | - `formatter_options` (`dict`, `None`) must be either `None` or `dict`. If providing a `dict` it must be the same structure as used in format functions, see [here](#data-formatting) for more information. Used to turn the span into a format type span which: |
| 1884 | - When using `get_data()` will format the returned data. |
| 1885 | - When using `set_data()` will format the data being set but **NOT** create a new formatting rule on the sheet. |
| 1886 | - `**kwargs` you can provide additional keyword arguments to the function for example those used in `span.highlight()` or `span.dropdown()` which are used when applying a named span to a table. |
| 1887 | - This function returns the span instance itself (`self`). |
| 1888 | |
| 1889 | ```python |
| 1890 | # entire sheet |
| 1891 | span = sheet["A1"].options(expand="both") |
| 1892 | |
| 1893 | # column A |
| 1894 | span = sheet["A1"].options(expand="down") |
| 1895 | |
| 1896 | # row 0 |
| 1897 | span = sheet["A1"].options( |
| 1898 | expand="right", |
| 1899 | ndim=1, # to return a single list when getting data |
| 1900 | ) |
| 1901 | ``` |
| 1902 | |
| 1903 | All of a spans modifiable attributes are listed here: |
| 1904 | |
| 1905 | - `from_r` (`int`) represents which row the span starts at, must be a positive `int`. |
| 1906 | - `from_c` (`int`) represents which column the span starts at, must be a positive `int`. |
| 1907 | - `upto_r` (`int`, `None`) represents which row the span ends at, must be a positive `int` or `None`. `None` means always up to and including the last row. |
| 1908 | - `upto_c` (`int`, `None`) represents which column the span ends at, must be a positive `int` or `None`. `None` means always up to and including the last column. |
| 1909 | - `type_` (`str`) must be either an empty string `""` or one of the following: `"format"`, `"highlight"`, `"dropdown"`, `"checkbox"`, `"readonly"`, `"align"`. |
| 1910 | - `name` (`str`) used for named spans or for identification. If no name is provided then a name is generated for the span which is based on an internal integer ticker and then converted to a string in the same way column names are. |
| 1911 | - `table` (`bool`) when `True` will make all functions used with the span target the main table as well as the header/index if those are `True`. |
| 1912 | - `index` (`bool`) when `True` will make all functions used with the span target the index as well as the table/header if those are `True`. |
| 1913 | - `header` (`bool`) when `True` will make all functions used with the span target the header as well as the table/index if those are `True`. |
| 1914 | - `tdisp` (`bool`) is used by data getting functions that utilize spans and when `True` the function retrieves screen displayed data for the table, not underlying cell data. |
| 1915 | - `idisp` (`bool`) is used by data getting functions that utilize spans and when `True` the function retrieves screen displayed data for the index, not underlying cell data. |
| 1916 | - `hdisp` (`bool`) is used by data getting functions that utilize spans and when `True` the function retrieves screen displayed data for the header, not underlying cell data. |
| 1917 | - `transposed` (`bool`) is used by data getting and setting functions that utilize spans. When `True`: |
| 1918 | - Returned sublists from data getting functions will represent columns rather than rows. |
| 1919 | - Data setting functions will assume that a single sequence is a column rather than row and that a list of lists is a list of columns rather than a list of rows. |
| 1920 | - `ndim` (`int`) is used by data getting functions that utilize spans, it must be either `0` or `1` or `2`. |
| 1921 | - `0` is the default setting which will make the return value vary based on what it is. For example if the gathered data is only a single cell it will return a value instead of a list of lists with a single list containing a single value. A single row will be a single list. |
| 1922 | - `1` will force the return of a single list as opposed to a list of lists. |
| 1923 | - `2` will force the return of a list of lists. |
| 1924 | - `convert` (`None`, `Callable`) can be used to modify the data using a function before returning it. The data sent to the `convert` function will be as it was before normally returning (after `ndim` has potentially modified it). |
| 1925 | - `undo` (`bool`) is used by data modifying functions that utilize spans. When `True` and if undo is enabled for the sheet then the end user will be able to undo/redo the modification. |
| 1926 | - `emit_event` (`bool`) is used by data modifying functions that utilize spans. When `True` causes a `"<<SheetModified>>` event to occur if it has been bound, see [here](#tkinter-and-tksheet-events) for more information. |
| 1927 | - `widget` (`object`) is the reference to the original sheet which created the span. This can be changed to a different sheet if required e.g. `my_span.widget = new_sheet`. |
| 1928 | - `kwargs` a `dict` containing keyword arguments relevant for functions such as `span.highlight()` or `span.dropdown()` which are used when applying a named span to a table. |
| 1929 | |
| 1930 | If necessary you can also modify these attributes the same way you would an objects. e.g. |
| 1931 | |
| 1932 | ```python |
| 1933 | # span now takes in all columns, including A |
| 1934 | span = self.sheet("A") |
| 1935 | span.upto_c = None |
| 1936 | |
| 1937 | # span now adds to sheets undo stack when using data modifying functions that use spans |
| 1938 | span = self.sheet("A") |
| 1939 | span.undo = True |
| 1940 | ``` |
| 1941 | |
| 1942 | #### **Using a span to format data** |
| 1943 | |
| 1944 | Formats table data, see the help on [formatting](#data-formatting) for more information. Note that using this function also creates a format rule for the affected table cells. |
| 1945 | |
| 1946 | ```python |
| 1947 | span.format( |
| 1948 | formatter_options: dict = {}, |
| 1949 | formatter_class: object = None, |
| 1950 | redraw: bool = True, |
| 1951 | **kwargs, |
| 1952 | ) -> Span |
| 1953 | ``` |
| 1954 | |
| 1955 | Example: |
| 1956 | ```python |
| 1957 | # using square brackets |
| 1958 | sheet[:].format(int_formatter()) |
| 1959 | |
| 1960 | # or instead using sheet.span() |
| 1961 | sheet.span(":").format(int_formatter()) |
| 1962 | ``` |
| 1963 | |
| 1964 | These examples show the formatting of the entire sheet (not including header and index) as `int` and creates a format rule for all currently existing cells. [Named spans](#named-spans) are required to create a rule for all future existing cells as well, for example those created by the end user inserting rows or columns. |
| 1965 | |
| 1966 | #### **Using a span to delete data format rules** |
| 1967 | |
| 1968 | Delete any currently existing format rules for parts of the table that are covered by the span. Should not be used where there are data formatting rules created by named spans, see [Named spans](#named-spans) for more information. |
| 1969 | |
| 1970 | ```python |
| 1971 | span.del_format() -> Span |
| 1972 | ``` |
| 1973 | |
| 1974 | Example: |
| 1975 | ```python |
| 1976 | span1 = sheet[2:4] |
| 1977 | span1.format(float_formatter()) |
| 1978 | span1.del_format() |
| 1979 | ``` |
| 1980 | |
| 1981 | #### **Using a span to create highlights** |
| 1982 | |
| 1983 | ```python |
| 1984 | span.highlight( |
| 1985 | bg: bool | None | str = False, |
| 1986 | fg: bool | None | str = False, |
| 1987 | end: bool | None = None, |
| 1988 | overwrite: bool = False, |
| 1989 | redraw: bool = True, |
| 1990 | ) -> Span |
| 1991 | ``` |
| 1992 | |
| 1993 | There are two ways to create highlights using a span: |
| 1994 | |
| 1995 | Method 1 example using `.highlight()`: |
| 1996 | ```python |
| 1997 | # highlights column A background red, text color black |
| 1998 | sheet["A"].highlight(bg="red", fg="black") |
| 1999 | |
| 2000 | # the same but after having saved a span |
| 2001 | my_span = sheet["A"] |
| 2002 | my_span.highlight(bg="red", fg="black") |
| 2003 | ``` |
| 2004 | |
| 2005 | Method 2 example using `.bg`/`.fg`: |
| 2006 | ```python |
| 2007 | # highlights column A background red, text color black |
| 2008 | sheet["A"].bg = "red" |
| 2009 | sheet["A"].fg = "black" |
| 2010 | |
| 2011 | # the same but after having saved a span |
| 2012 | my_span = sheet["A"] |
| 2013 | my_span.bg = "red" |
| 2014 | my_span.fg = "black" |
| 2015 | ``` |
| 2016 | |
| 2017 | #### **Using a span to delete highlights** |
| 2018 | |
| 2019 | Delete any currently existing highlights for parts of the sheet that are covered by the span. Should not be used where there are highlights created by named spans, see [Named spans](#named-spans) for more information. |
| 2020 | |
| 2021 | ```python |
| 2022 | span.dehighlight() -> Span |
| 2023 | ``` |
| 2024 | |
| 2025 | Example: |
| 2026 | ```python |
| 2027 | span1 = sheet[2:4].highlight(bg="red", fg="black") |
| 2028 | span1.dehighlight() |
| 2029 | ``` |
| 2030 | |
| 2031 | #### **Using a span to create dropdown boxes** |
| 2032 | |
| 2033 | Creates dropdown boxes for parts of the sheet that are covered by the span. For more information see [here](#dropdown-boxes). |
| 2034 | |
| 2035 | ```python |
| 2036 | span.dropdown( |
| 2037 | values: list = [], |
| 2038 | set_value: object = None, |
| 2039 | state: Literal["normal", "readonly", "disabled"] = "normal", |
| 2040 | redraw: bool = True, |
| 2041 | selection_function: Callable | None = None, |
| 2042 | modified_function: Callable | None = None, |
| 2043 | search_function: Callable = dropdown_search_function, |
| 2044 | validate_input: bool = True, |
| 2045 | text: None | str = None, |
| 2046 | ) -> Span |
| 2047 | ``` |
| 2048 | |
| 2049 | Example: |
| 2050 | ```python |
| 2051 | sheet["D"].dropdown( |
| 2052 | values=["on", "off"], |
| 2053 | set_value="off", |
| 2054 | ) |
| 2055 | ``` |
| 2056 | |
| 2057 | #### **Using a span to delete dropdown boxes** |
| 2058 | |
| 2059 | Delete dropdown boxes for parts of the sheet that are covered by the span. Should not be used where there are dropdown box rules created by named spans, see [Named spans](#named-spans) for more information. |
| 2060 | |
| 2061 | ```python |
| 2062 | span.del_dropdown() -> Span |
| 2063 | ``` |
| 2064 | |
| 2065 | Example: |
| 2066 | ```python |
| 2067 | dropdown_span = sheet["D"].dropdown(values=["on", "off"], |
| 2068 | set_value="off") |
| 2069 | dropdown_span.del_dropdown() |
| 2070 | ``` |
| 2071 | |
| 2072 | #### **Using a span to create check boxes** |
| 2073 | |
| 2074 | Create check boxes for parts of the sheet that are covered by the span. |
| 2075 | |
| 2076 | ```python |
| 2077 | span.checkbox( |
| 2078 | edit_data: bool = True, |
| 2079 | checked: bool | None = None, |
| 2080 | state: Literal["normal", "disabled"] = "normal", |
| 2081 | redraw: bool = True, |
| 2082 | check_function: Callable | None = None, |
| 2083 | text: str = "", |
| 2084 | ) -> Span |
| 2085 | ``` |
| 2086 | Parameters: |
| 2087 | |
| 2088 | - `edit_data` when `True` edits the underlying cell data to either `checked` if `checked` is a `bool` or tries to convert the existing cell data to a `bool`. |
| 2089 | - `checked` is the initial creation value to set the box to, if `None` then and `edit_data` is `True` then it will try to convert the underlying cell data to a `bool`. |
| 2090 | - `state` can be `"normal"` or `"disabled"`. If `"disabled"` then color will be same as table grid lines, else it will be the cells text color. |
| 2091 | - `check_function` can be used to trigger a function when the user clicks a checkbox. |
| 2092 | - `text` displays text next to the checkbox in the cell, but will not be used as data, data will either be `True` or `False`. |
| 2093 | |
| 2094 | Notes: |
| 2095 | |
| 2096 | - To get the current checkbox value either: |
| 2097 | - Get the cell data, more information [here](#getting-sheet-data). |
| 2098 | - Use the parameter `check_function` with a function of your own creation to be called when the checkbox is set by the user. |
| 2099 | |
| 2100 | Example: |
| 2101 | ```python |
| 2102 | sheet["D"].checkbox( |
| 2103 | checked=True, |
| 2104 | text="Switch", |
| 2105 | ) |
| 2106 | ``` |
| 2107 | |
| 2108 | #### **Using a span to delete check boxes** |
| 2109 | |
| 2110 | Delete check boxes for parts of the sheet that are covered by the span. Should not be used where there are check box rules created by named spans, see [Named spans](#named-spans) for more information. |
| 2111 | |
| 2112 | ```python |
| 2113 | span.del_checkbox() -> Span |
| 2114 | ``` |
| 2115 | |
| 2116 | Example: |
| 2117 | ```python |
| 2118 | checkbox_span = sheet["D"].checkbox(checked=True, |
| 2119 | text="Switch") |
| 2120 | checkbox_span.del_checkbox() |
| 2121 | ``` |
| 2122 | |
| 2123 | #### **Using a span to set cells to read only** |
| 2124 | |
| 2125 | Create a readonly rule for parts of the table that are covered by the span. |
| 2126 | |
| 2127 | ```python |
| 2128 | span.readonly(readonly: bool = True) -> Span |
| 2129 | ``` |
| 2130 | - Using `span.readonly(False)` deletes any existing readonly rules for the span. Should not be used where there are readonly rules created by named spans, see [Named spans](#named-spans) for more information. |
| 2131 | |
| 2132 | #### **Using a span to create text alignment rules** |
| 2133 | |
| 2134 | Create a text alignment rule for parts of the sheet that are covered by the span. |
| 2135 | |
| 2136 | ```python |
| 2137 | span.align( |
| 2138 | align: str | None, |
| 2139 | redraw: bool = True, |
| 2140 | ) -> Span |
| 2141 | ``` |
| 2142 | - `align` (`str`, `None`) must be either: |
| 2143 | - `None` - clears the alignment rule |
| 2144 | - `"c"`, `"center"`, `"centre"` |
| 2145 | - `"w"`, `"west"`, `"left"` |
| 2146 | - `"e"`, `"east"`, `"right"` |
| 2147 | |
| 2148 | Example: |
| 2149 | ```python |
| 2150 | sheet["D"].align("right") |
| 2151 | ``` |
| 2152 | |
| 2153 | There are two ways to create alignment rules using a span: |
| 2154 | |
| 2155 | Method 1 example using `.align()`: |
| 2156 | ```python |
| 2157 | # column D right text alignment |
| 2158 | sheet["D"].align("right") |
| 2159 | |
| 2160 | # the same but after having saved a span |
| 2161 | my_span = sheet["D"] |
| 2162 | my_span.align("right") |
| 2163 | ``` |
| 2164 | |
| 2165 | Method 2 example using `.align = `: |
| 2166 | ```python |
| 2167 | # column D right text alignment |
| 2168 | sheet["D"].align = "right" |
| 2169 | |
| 2170 | # the same but after having saved a span |
| 2171 | my_span = sheet["D"] |
| 2172 | my_span.align = "right" |
| 2173 | ``` |
| 2174 | |
| 2175 | #### **Using a span to delete text alignment rules** |
| 2176 | |
| 2177 | Delete text alignment rules for parts of the sheet that are covered by the span. Should not be used where there are alignment rules created by named spans, see [Named spans](#named-spans) for more information. |
| 2178 | |
| 2179 | ```python |
| 2180 | span.del_align() -> Span |
| 2181 | ``` |
| 2182 | |
| 2183 | Example: |
| 2184 | ```python |
| 2185 | align_span = sheet["D"].align("right") |
| 2186 | align_span.del_align() |
| 2187 | ``` |
| 2188 | |
| 2189 | #### **Using a span to clear cells** |
| 2190 | |
| 2191 | Clear cell data from all cells that are covered by the span. |
| 2192 | |
| 2193 | ```python |
| 2194 | span.clear( |
| 2195 | undo: bool | None = None, |
| 2196 | emit_event: bool | None = None, |
| 2197 | redraw: bool = True, |
| 2198 | ) -> Span |
| 2199 | ``` |
| 2200 | Parameters: |
| 2201 | |
| 2202 | - `undo` (`bool`, `None`) When `True` if undo is enabled for the end user they will be able to undo the clear change. |
| 2203 | - `emit_event` when `True` causes a `"<<SheetModified>>` event to occur if it has been bound, see [here](#tkinter-and-tksheet-events) for more information. |
| 2204 | |
| 2205 | Example: |
| 2206 | ```python |
| 2207 | # clears column D |
| 2208 | sheet["D"].clear() |
| 2209 | ``` |
| 2210 | |
| 2211 | #### **Using a span to tag cells** |
| 2212 | |
| 2213 | Tag cells, rows or columns depending on the spans kind, more information on tags [here](#tags). |
| 2214 | |
| 2215 | ```python |
| 2216 | tag(*tags) -> Span |
| 2217 | ``` |
| 2218 | Notes: |
| 2219 | |
| 2220 | - If `span.kind` is `"cell"` then cells will be tagged, if it's a row span then rows will be and so for columns. |
| 2221 | |
| 2222 | Example: |
| 2223 | ```python |
| 2224 | # tags rows 2, 3, 4 with "hello world" |
| 2225 | sheet[2:5].tag("hello world") |
| 2226 | ``` |
| 2227 | |
| 2228 | #### **Using a span to untag cells** |
| 2229 | |
| 2230 | Remove **all** tags from cells, rows or columns depending on the spans kind, more information on tags [here](#tags). |
| 2231 | |
| 2232 | ```python |
| 2233 | untag() -> Span |
| 2234 | ``` |
| 2235 | Notes: |
| 2236 | |
| 2237 | - If `span.kind` is `"cell"` then cells will be untagged, if it's a row span then rows will be and so for columns. |
| 2238 | |
| 2239 | Example: |
| 2240 | ```python |
| 2241 | # tags rows 2, 3, 4 with "hello" and "bye" |
| 2242 | sheet[2:5].tag("hello", "bye") |
| 2243 | |
| 2244 | # removes both "hello" and "bye" tags from rows 2, 3, 4 |
| 2245 | sheet[2:5].untag() |
| 2246 | ``` |
| 2247 | |
| 2248 | #### **Set the spans orientation** |
| 2249 | |
| 2250 | The attribute `span.transposed` (`bool`) is used by data getting and setting functions that utilize spans. When `True`: |
| 2251 | - Returned sublists from data getting functions will represent columns rather than rows. |
| 2252 | - Data setting functions will assume that a single sequence is a column rather than row and that a list of lists is a list of columns rather than a list of rows. |
| 2253 | |
| 2254 | You can toggle the transpotition of the span by using: |
| 2255 | |
| 2256 | ```python |
| 2257 | span.transpose() -> Span |
| 2258 | ``` |
| 2259 | |
| 2260 | If the attribute is already `True` this makes it `False` and vice versa. |
| 2261 | |
| 2262 | ```python |
| 2263 | span = sheet["A:D"].transpose() |
| 2264 | # this span is now transposed |
| 2265 | print (span.transposed) |
| 2266 | # prints True |
| 2267 | |
| 2268 | span.transpose() |
| 2269 | # this span is no longer transposed |
| 2270 | print (span.transposed) |
| 2271 | # prints False |
| 2272 | ``` |
| 2273 | |
| 2274 | #### **Expand the spans area** |
| 2275 | |
| 2276 | Expand the spans area either all the way to the right (x axis) or all the way down (y axis) or both. |
| 2277 | |
| 2278 | ```python |
| 2279 | span.expand(direction: str = "both") -> Span |
| 2280 | ``` |
| 2281 | - `direction` (`None`, `str`) must be either `None` or: |
| 2282 | - `"table"`/`"both"` expand the span both down and right from the span start to the ends of the table. |
| 2283 | - `"right"` expand the span right to the end of the table x axis. |
| 2284 | - `"down"` expand the span downwards to the bottom of the table y axis. |
| 2285 | |
| 2286 | --- |
| 2287 | # **Named Spans** |
| 2288 | |
| 2289 | Named spans are like spans but with a type, some keyword arguments saved in `span.kwargs` and then created by using a `Sheet()` function. Like spans, named spans are also **contiguous** areas of the sheet. |
| 2290 | |
| 2291 | Named spans can be used to: |
| 2292 | |
| 2293 | - Create options (rules) for the sheet which will expand/contract when new cells are added/removed. For example if a user were to insert rows in the middle of some already highlighted rows: |
| 2294 | - With ordinary row highlights the newly inserted rows would **NOT** be highlighted. |
| 2295 | - With named span row highlights the newly inserted rows would also be highlighted. |
| 2296 | - Quickly delete an existing option from the table whereas an ordinary span would not keep track of where the options have been moved. |
| 2297 | |
| 2298 | **Note** that generally when a user moves rows/columns around the dimensions of the named span essentially move with either end of the span: |
| 2299 | |
| 2300 | - The new start of the span will be wherever the start row/column moves. |
| 2301 | - The new end of the span will be wherever the end row/column moves. |
| 2302 | The exceptions to this rule are when a span is expanded or has been created with `None`s or the start of `0` and no end or end of `None`. |
| 2303 | |
| 2304 | For the end user, when a span is just a single row/column (and is not expanded/unlimited) it cannot be expanded but it can be deleted if the row/column is deleted. |
| 2305 | |
| 2306 | #### **Creating a named span** |
| 2307 | |
| 2308 | For a span to become a named span it needs: |
| 2309 | |
| 2310 | - One of the following `type_`s: `"format"`, `"highlight"`, `"dropdown"`, `"checkbox"`, `"readonly"`, `"align"`. |
| 2311 | - Relevant keyword arguments e.g. if the `type_` is `"highlight"` then arguments for `sheet.highlight()` found [here](#highlighting-cells). |
| 2312 | |
| 2313 | After a span has the above items the following function has to be used to make it a named span and create the options on the sheet: |
| 2314 | |
| 2315 | ```python |
| 2316 | named_span(span: Span) |
| 2317 | """ |
| 2318 | Adds a named span to the sheet |
| 2319 | Returns the span |
| 2320 | """ |
| 2321 | ``` |
| 2322 | - `span` must be an existing span with: |
| 2323 | - a `name` (a `name` is automatically generated upon span creation if one is not provided). |
| 2324 | - a `type_` as described above. |
| 2325 | - keyword arguments as described above. |
| 2326 | |
| 2327 | Examples of creating named spans: |
| 2328 | ```python |
| 2329 | # Will highlight rows 3 up to and including 5 |
| 2330 | span1 = self.sheet.span( |
| 2331 | "3:5", |
| 2332 | type_="highlight", |
| 2333 | bg="green", |
| 2334 | fg="black", |
| 2335 | ) |
| 2336 | self.sheet.named_span(span1) |
| 2337 | |
| 2338 | # Will always keep the entire sheet formatted as `int` no matter how many rows/columns are inserted |
| 2339 | span2 = self.sheet.span( |
| 2340 | ":", |
| 2341 | # you don't have to provide a `type_` when using the `formatter_kwargs` argument |
| 2342 | formatter_options=int_formatter(), |
| 2343 | ) |
| 2344 | self.sheet.named_span(span2) |
| 2345 | ``` |
| 2346 | |
| 2347 | #### **Deleting a named span** |
| 2348 | |
| 2349 | ```python |
| 2350 | del_named_span(name: str) |
| 2351 | ``` |
| 2352 | |
| 2353 | Example, creating and deleting a span: |
| 2354 | ```python |
| 2355 | # span covers the entire sheet |
| 2356 | self.sheet.named_span( |
| 2357 | self.sheet.span( |
| 2358 | name="my highlight span", |
| 2359 | type_="highlight", |
| 2360 | bg="dark green", |
| 2361 | fg="#FFFFFF", |
| 2362 | ) |
| 2363 | ) |
| 2364 | self.sheet.del_named_span("my highlight span") |
| 2365 | |
| 2366 | # ValueError is raised if name does not exist |
| 2367 | self.sheet.del_named_span("this name doesnt exist") |
| 2368 | # ValueError: Span 'this name doesnt exist' does not exist. |
| 2369 | ``` |
| 2370 | |
| 2371 | #### **Other named span functions** |
| 2372 | |
| 2373 | Sets the `Sheet`s internal dict of named spans: |
| 2374 | ```python |
| 2375 | set_named_spans(named_spans: None | dict = None) -> Sheet |
| 2376 | ``` |
| 2377 | - Using `None` deletes all existing named spans |
| 2378 | |
| 2379 | Get an existing named span: |
| 2380 | ```python |
| 2381 | get_named_span(name: str) -> dict |
| 2382 | ``` |
| 2383 | |
| 2384 | Get all existing named spans: |
| 2385 | ```python |
| 2386 | get_named_spans() -> dict |
| 2387 | ``` |
| 2388 | |
| 2389 | --- |
| 2390 | # **Getting Sheet Data** |
| 2391 | |
| 2392 | #### **Using a span to get sheet data** |
| 2393 | |
| 2394 | A `Span` object (more information [here](#span-objects)) is returned when using square brackets on a `Sheet` like so: |
| 2395 | |
| 2396 | ```python |
| 2397 | span = self.sheet["A1"] |
| 2398 | ``` |
| 2399 | |
| 2400 | You can also use `sheet.span()`: |
| 2401 | |
| 2402 | ```python |
| 2403 | span = self.sheet.span("A1") |
| 2404 | ``` |
| 2405 | |
| 2406 | The above spans represent the cell `A1` - row 0, column 0. |
| 2407 | |
| 2408 | A reserved span attribute named `data` can then be used to retrieve the data for cell `A1`, example below: |
| 2409 | |
| 2410 | ```python |
| 2411 | span = self.sheet["A1"] |
| 2412 | cell_a1_data = span.data |
| 2413 | ``` |
| 2414 | |
| 2415 | The data that is retrieved entirely depends on the area the span represents. You can also use `span.value` to the same effect. |
| 2416 | |
| 2417 | There are certain other span attributes which have an impact on the data returned, explained below: |
| 2418 | |
| 2419 | - `table` (`bool`) when `True` will make all functions used with the span target the main table as well as the header/index if those are `True`. |
| 2420 | - `index` (`bool`) when `True` will make all functions used with the span target the index as well as the table/header if those are `True`. |
| 2421 | - `header` (`bool`) when `True` will make all functions used with the span target the header as well as the table/index if those are `True`. |
| 2422 | - `tdisp` (`bool`) when `True` the function retrieves screen displayed data for the table, not underlying cell data. |
| 2423 | - `idisp` (`bool`) when `True` the function retrieves screen displayed data for the index, not underlying cell data. |
| 2424 | - `hdisp` (`bool`) when `True` the function retrieves screen displayed data for the header, not underlying cell data. |
| 2425 | - `transposed` (`bool`) is used by data getting and setting functions that utilize spans. When `True`: |
| 2426 | - Returned sublists from **data getting** functions will represent columns rather than rows. |
| 2427 | - Data setting functions will assume that a single sequence is a column rather than row and that a list of lists is a list of columns rather than a list of rows. |
| 2428 | - `ndim` (`int`) is used by data getting functions that utilize spans, it must be either `0` or `1` or `2`. |
| 2429 | - `0` is the default setting which will make the return value vary based on what it is. For example if the gathered data is only a single cell it will return a value instead of a list of lists with a single list containing a single value. A single row will be a single list. |
| 2430 | - `1` will force the return of a single list as opposed to a list of lists. |
| 2431 | - `2` will force the return of a list of lists. |
| 2432 | - `convert` (`None`, `Callable`) can be used to modify the data using a function before returning it. The data sent to the `convert` function will be as it was before normally returning (after `ndim` has potentially modified it). |
| 2433 | - `widget` (`object`) is the reference to the original sheet which created the span (this is the widget that data is retrieved from). This can be changed to a different sheet if required e.g. `my_span.widget = new_sheet`. |
| 2434 | |
| 2435 | Some more complex examples of data retrieval: |
| 2436 | |
| 2437 | ```python |
| 2438 | "single cell" |
| 2439 | cell_a1_data = self.sheet["A1"].data |
| 2440 | |
| 2441 | "entire sheet including headers and index" |
| 2442 | entire_sheet_data = self.sheet["A1"].expand().options(header=True, index=True).data |
| 2443 | |
| 2444 | "header data, no table or index data" |
| 2445 | # a list of displayed header cells |
| 2446 | header_data = self.sheet["A:C"].options(table=False, header=True).data |
| 2447 | |
| 2448 | # a header value |
| 2449 | header_data = self.sheet["A"].options(table=False, hdisp=False, header=True).data |
| 2450 | |
| 2451 | "index data, no table or header data" |
| 2452 | # a list of displayed index cells |
| 2453 | index_data = self.sheet[:3].options(table=False, index=True).data |
| 2454 | |
| 2455 | # or using sheet.span() a list of displayed index cells |
| 2456 | index_data = self.sheet.span(slice(None, 3), table=False, index=True).data |
| 2457 | |
| 2458 | # a row index value |
| 2459 | index_data = self.sheet[3].options(table=False, idisp=False, index=True).data |
| 2460 | |
| 2461 | "sheet data as columns instead of rows, with actual header data" |
| 2462 | sheet_data = self.sheet[:].transpose().options(hdisp=False, header=True).data |
| 2463 | |
| 2464 | # or instead using sheet.span() with only kwargs |
| 2465 | sheet_data = self.sheet.span(transposed=True, hdisp=False, header=True).data |
| 2466 | ``` |
| 2467 | |
| 2468 | There is also a `Sheet()` function for data retrieval (it is used internally by the above data getting methods): |
| 2469 | |
| 2470 | ```python |
| 2471 | sheet.get_data( |
| 2472 | *key: CreateSpanTypes, |
| 2473 | ) -> object |
| 2474 | ``` |
| 2475 | |
| 2476 | Examples: |
| 2477 | ```python |
| 2478 | data = self.sheet.get_data("A1") |
| 2479 | data = self.sheet.get_data(0, 0, 3, 3) |
| 2480 | data = self.sheet.get_data( |
| 2481 | self.sheet.span(":D", transposed=True) |
| 2482 | ) |
| 2483 | ``` |
| 2484 | |
| 2485 | #### **Get a single cells data** |
| 2486 | |
| 2487 | This is a higher performance method to get a single cells data which may be useful for example when performing a very large number of single cell data retrievals in a loop. |
| 2488 | |
| 2489 | ```python |
| 2490 | get_cell_data(r: int, c: int, get_displayed: bool = False) -> object |
| 2491 | ``` |
| 2492 | - `get_displayed` (`bool`) when `True` retrieves the value that is displayed to the user in the sheet, not the underlying data. |
| 2493 | |
| 2494 | ___ |
| 2495 | |
| 2496 | #### **Generate sheet rows one at a time** |
| 2497 | |
| 2498 | This function is useful if you need a lot of sheet data, and produces one row at a time (may save memory use in certain scenarios). It does not use spans. |
| 2499 | |
| 2500 | ```python |
| 2501 | yield_sheet_rows( |
| 2502 | get_displayed: bool = False, |
| 2503 | get_header: bool = False, |
| 2504 | get_index: bool = False, |
| 2505 | get_index_displayed: bool = True, |
| 2506 | get_header_displayed: bool = True, |
| 2507 | only_rows: int | Iterator[int] | None = None, |
| 2508 | only_columns: int | Iterator[int] | None = None, |
| 2509 | ) -> Iterator[list[object]] |
| 2510 | ``` |
| 2511 | Parameters: |
| 2512 | |
| 2513 | - `get_displayed` (`bool`) if `True` it will return cell values as they are displayed on the screen. If `False` it will return any underlying data, for example if the cell is formatted. |
| 2514 | - `get_header` (`bool`) if `True` it will return the header of the sheet even if there is not one. |
| 2515 | - `get_index` (`bool`) if `True` it will return the index of the sheet even if there is not one. |
| 2516 | - `get_index_displayed` (`bool`) if `True` it will return whatever index values are displayed on the screen, for example if there is a dropdown box with `text` set. |
| 2517 | - `get_header_displayed` (`bool`) if `True` it will return whatever header values are displayed on the screen, for example if there is a dropdown box with `text` set. |
| 2518 | - `only_rows` (`None`, `iterable`) with this argument you can supply an iterable of `int` row indexes in any order to be the only rows that are returned. |
| 2519 | - `only_columns` (`None`, `iterable`) with this argument you can supply an iterable of `int` column indexes in any order to be the only columns that are returned. |
| 2520 | |
| 2521 | ___ |
| 2522 | |
| 2523 | #### **Get table data, readonly** |
| 2524 | |
| 2525 | ```python |
| 2526 | @property |
| 2527 | data() |
| 2528 | ``` |
| 2529 | - e.g. `self.sheet.data` |
| 2530 | - Doesn't include header or index data. |
| 2531 | |
| 2532 | ___ |
| 2533 | |
| 2534 | #### **The name of the actual internal sheet data variable** |
| 2535 | |
| 2536 | ```python |
| 2537 | .MT.data |
| 2538 | ``` |
| 2539 | - You can use this to directly modify or retrieve the main table's data e.g. `cell_0_0 = my_sheet_name_here.MT.data[0][0]` but only do so if you know what you're doing. |
| 2540 | |
| 2541 | ___ |
| 2542 | |
| 2543 | #### **Sheet methods** |
| 2544 | |
| 2545 | `Sheet` objects also have some functions similar to lists. **Note** that these functions do **not** include the header or index. |
| 2546 | |
| 2547 | Iterate over table rows: |
| 2548 | ```python |
| 2549 | for row in self.sheet: |
| 2550 | print (row) |
| 2551 | |
| 2552 | # and in reverse |
| 2553 | for row in reversed(self.sheet): |
| 2554 | print (row) |
| 2555 | ``` |
| 2556 | |
| 2557 | Check if the table has a particular value (membership): |
| 2558 | ```python |
| 2559 | # returns True or False |
| 2560 | search_value = "the cell value I'm looking for" |
| 2561 | print (search_value in self.sheet) |
| 2562 | ``` |
| 2563 | - Can also check if a row is in the sheet if a `list` is used. |
| 2564 | |
| 2565 | ___ |
| 2566 | |
| 2567 | #### **Get the number of rows in the sheet** |
| 2568 | |
| 2569 | ```python |
| 2570 | get_total_rows(include_index: bool = False) -> int |
| 2571 | ``` |
| 2572 | |
| 2573 | ___ |
| 2574 | |
| 2575 | #### **Get the number of columns in the sheet** |
| 2576 | |
| 2577 | ```python |
| 2578 | get_total_columns(include_header: bool = False) -> int |
| 2579 | ``` |
| 2580 | |
| 2581 | ___ |
| 2582 | |
| 2583 | #### **Get a value for a particular cell if that cell was empty** |
| 2584 | |
| 2585 | ```python |
| 2586 | get_value_for_empty_cell( |
| 2587 | r: int, |
| 2588 | c: int, |
| 2589 | r_ops: bool = True, |
| 2590 | c_ops: bool = True, |
| 2591 | ) -> object |
| 2592 | ``` |
| 2593 | - `r_ops`/`c_ops` when both are `True` it will take into account whatever cell/row/column options exist. When just `r_ops` is `True` it will take into account row options only and when just `c_ops` is `True` it will take into account column options only. |
| 2594 | |
| 2595 | --- |
| 2596 | # **Setting Sheet Data** |
| 2597 | |
| 2598 | Fundamentally, there are two ways to set table data: |
| 2599 | |
| 2600 | - Overwriting the entire table and setting the table data to a new object. |
| 2601 | - Modifying the existing data. |
| 2602 | |
| 2603 | #### **Overwriting the table** |
| 2604 | |
| 2605 | ```python |
| 2606 | set_sheet_data( |
| 2607 | data: list | tuple | None = None, |
| 2608 | reset_col_positions: bool = True, |
| 2609 | reset_row_positions: bool = True, |
| 2610 | redraw: bool = True, |
| 2611 | verify: bool = False, |
| 2612 | reset_highlights: bool = False, |
| 2613 | keep_formatting: bool = True, |
| 2614 | delete_options: bool = False, |
| 2615 | ) -> object |
| 2616 | ``` |
| 2617 | Parameters: |
| 2618 | |
| 2619 | - `data` (`list`) has to be a list of lists for full functionality, for display only a list of tuples or a tuple of tuples will work. |
| 2620 | - `reset_col_positions` and `reset_row_positions` (`bool`) when `True` will reset column widths and row heights. |
| 2621 | - `redraw` (`bool`) refreshes the table after setting new data. |
| 2622 | - `verify` (`bool`) goes through `data` and checks if it is a list of lists, will raise error if not, disabled by default. |
| 2623 | - `reset_highlights` (`bool`) resets all table cell highlights. |
| 2624 | - `keep_formatting` (`bool`) when `True` re-applies any prior formatting rules to the new data, if `False` all prior formatting rules are deleted. |
| 2625 | - `delete_options` (`bool`) when `True` all table options such as dropdowns, check boxes, formatting, highlighting etc. are deleted. |
| 2626 | |
| 2627 | Notes: |
| 2628 | |
| 2629 | - This function does not impact the sheet header or index. |
| 2630 | |
| 2631 | ___ |
| 2632 | |
| 2633 | ```python |
| 2634 | @data.setter |
| 2635 | data(value: object) |
| 2636 | ``` |
| 2637 | Notes: |
| 2638 | |
| 2639 | - Acts like setting an attribute e.g. `sheet.data = [[1, 2, 3], [4, 5, 6]]` |
| 2640 | - Uses the `set_sheet_data()` function and its default arguments. |
| 2641 | |
| 2642 | ___ |
| 2643 | |
| 2644 | #### **Reset all or specific sheet elements and attributes** |
| 2645 | |
| 2646 | ```python |
| 2647 | reset( |
| 2648 | table: bool = True, |
| 2649 | header: bool = True, |
| 2650 | index: bool = True, |
| 2651 | row_heights: bool = True, |
| 2652 | column_widths: bool = True, |
| 2653 | cell_options: bool = True, |
| 2654 | undo_stack: bool = True, |
| 2655 | selections: bool = True, |
| 2656 | sheet_options: bool = False, |
| 2657 | redraw: bool = True, |
| 2658 | ) -> Sheet |
| 2659 | ``` |
| 2660 | Parameters: |
| 2661 | |
| 2662 | - `table` when `True` resets the table to an empty list. |
| 2663 | - `header` when `True` resets the header to an empty list. |
| 2664 | - `index` when `True` resets the row index to an empty list. |
| 2665 | - `row_heights` when `True` deletes all displayed row lines. |
| 2666 | - `column_widths` when `True` deletes all displayed column lines. |
| 2667 | - `cell_options` when `True` deletes all dropdowns, checkboxes, highlights, data formatting, etc. |
| 2668 | - `undo_stack` when `True` resets the sheets undo stack to empty. |
| 2669 | - `selections` when `True` deletes all selection boxes. |
| 2670 | - `sheet_options` when `True` resets all the sheets options such as colors, font, popup menu labels and many more to default, for a full list of what's reset see the file `sheet_options.py`. |
| 2671 | |
| 2672 | Notes: |
| 2673 | |
| 2674 | - This function could be useful when a whole new sheet needs to be loaded. |
| 2675 | |
| 2676 | ___ |
| 2677 | |
| 2678 | #### **Modifying sheet data** |
| 2679 | |
| 2680 | A `Span` object (more information [here](#span-objects)) is returned when using square brackets on a `Sheet` like so: |
| 2681 | |
| 2682 | ```python |
| 2683 | span = self.sheet["A1"] |
| 2684 | ``` |
| 2685 | |
| 2686 | You can also use `sheet.span()`: |
| 2687 | |
| 2688 | ```python |
| 2689 | span = self.sheet.span("A1") |
| 2690 | ``` |
| 2691 | |
| 2692 | The above span example represents the cell `A1` - row 0, column 0. A reserved span attribute named `data` (you can also use `.value`) can then be used to modify sheet data **starting** from cell `A1`. example below: |
| 2693 | |
| 2694 | ```python |
| 2695 | span = self.sheet["A1"] |
| 2696 | span.data = "new value for cell A1" |
| 2697 | |
| 2698 | # or even shorter: |
| 2699 | self.sheet["A1"].data = "new value for cell A1" |
| 2700 | |
| 2701 | # or with sheet.span() |
| 2702 | self.sheet.span("A1").data = "new value for cell A1" |
| 2703 | ``` |
| 2704 | |
| 2705 | If you provide a list or tuple it will set more than one cell, starting from the spans start cell. In the example below three cells are set in the first row, **starting from cell B1**: |
| 2706 | |
| 2707 | ```python |
| 2708 | self.sheet["B1"].data = ["row 0, column 1 new value (B1)", |
| 2709 | "row 0, column 2 new value (C1)", |
| 2710 | "row 0, column 3 new value (D1)"] |
| 2711 | ``` |
| 2712 | |
| 2713 | You can set data in column orientation with a transposed span: |
| 2714 | |
| 2715 | ```python |
| 2716 | self.sheet["B1"].transpose().data = ["row 0, column 1 new value (B1)", |
| 2717 | "row 1, column 1 new value (B2)", |
| 2718 | "row 2, column 1 new value (B3)"] |
| 2719 | ``` |
| 2720 | |
| 2721 | When setting data only a spans start cell is taken into account, the end cell is ignored. The example below demonstrates this, the spans end - `"B1"` is ignored and 4 cells get new values: |
| 2722 | |
| 2723 | ```python |
| 2724 | self.sheet["A1:B1"].data = ["A1 new val", "B1 new val", "C1 new val", "D1 new val"] |
| 2725 | ``` |
| 2726 | |
| 2727 | These are the span attributes which have an impact on the data set: |
| 2728 | - `table` (`bool`) when `True` will make all functions used with the span target the main table as well as the header/index if those are `True`. |
| 2729 | - `index` (`bool`) when `True` will make all functions used with the span target the index as well as the table/header if those are `True`. |
| 2730 | - `header` (`bool`) when `True` will make all functions used with the span target the header as well as the table/index if those are `True`. |
| 2731 | - `transposed` (`bool`) is used by data getting and setting functions that utilize spans. When `True`: |
| 2732 | - Returned sublists from data getting functions will represent columns rather than rows. |
| 2733 | - **Data setting** functions will assume that a single sequence is a column rather than row and that a list of lists is a list of columns rather than a list of rows. |
| 2734 | - `widget` (`object`) is the reference to the original sheet which created the span (this is the widget that data is set to). This can be changed to a different sheet if required e.g. `my_span.widget = new_sheet`. |
| 2735 | |
| 2736 | Some more complex examples of setting data: |
| 2737 | |
| 2738 | ```python |
| 2739 | """ |
| 2740 | SETTING ROW DATA |
| 2741 | """ |
| 2742 | # first row gets some new values and the index gets a new value also |
| 2743 | self.sheet[0].options(index=True).data = ["index val", "row 0 col 0", "row 0 col 1", "row 0 col 2"] |
| 2744 | |
| 2745 | # or instead using sheet.span() first row gets some new values and the index gets a new value also |
| 2746 | self.sheet.span(0, index=True).data = ["index val", "row 0 col 0", "row 0 col 1", "row 0 col 2"] |
| 2747 | |
| 2748 | # first two rows get some new values, index included |
| 2749 | self.sheet[0].options(index=True).data = [["index 0", "row 0 col 0", "row 0 col 1", "row 0 col 2"], |
| 2750 | ["index 1", "row 1 col 0", "row 1 col 1", "row 1 col 2"]] |
| 2751 | |
| 2752 | """ |
| 2753 | SETTING COLUMN DATA |
| 2754 | """ |
| 2755 | # first column gets some new values and the header gets a new value also |
| 2756 | self.sheet["A"].options(transposed=True, header=True).data = ["header val", "row 0 col 0", "row 1 col 0", "row 2 col 0"] |
| 2757 | |
| 2758 | # or instead using sheet.span() first column gets some new values and the header gets a new value also |
| 2759 | self.sheet.span("A", transposed=True, header=True).data = ["header val", "row 0 col 0", "row 1 col 0", "row 2 col 0"] |
| 2760 | |
| 2761 | # first two columns get some new values, header included |
| 2762 | self.sheet["A"].options(transposed=True, header=True).data = [["header 0", "row 0 col 0", "row 1 col 0", "row 2 col 0"], |
| 2763 | ["header 1", "row 0 col 1", "row 1 col 1", "row 2 col 1"]] |
| 2764 | |
| 2765 | """ |
| 2766 | SETTING CELL AREA DATA |
| 2767 | """ |
| 2768 | # cells B2, C2, B3, C3 get new values |
| 2769 | self.sheet["B2"].data = [["B2 new val", "C2 new val"], |
| 2770 | ["B3 new val", "C3 new val"]] |
| 2771 | |
| 2772 | # or instead using sheet.span() cells B2, C2, B3, C3 get new values |
| 2773 | self.sheet.span("B2").data = [["B2 new val", "C2 new val"], |
| 2774 | ["B3 new val", "C3 new val"]] |
| 2775 | |
| 2776 | """ |
| 2777 | SETTING CELL AREA DATA INCLUDING HEADER AND INDEX |
| 2778 | """ |
| 2779 | self.sheet_span = self.sheet.span( |
| 2780 | header=True, |
| 2781 | index=True, |
| 2782 | hdisp=False, |
| 2783 | idisp=False, |
| 2784 | ) |
| 2785 | |
| 2786 | # set data for the span which was created above |
| 2787 | self.sheet_span.data = [["", "A", "B", "C"] |
| 2788 | ["1", "A1", "B1", "C1"], |
| 2789 | ["2", "A2", "B2", "C2"]] |
| 2790 | ``` |
| 2791 | |
| 2792 | #### **Sheet set data function** |
| 2793 | |
| 2794 | You can also use the `Sheet` function `set_data()`. |
| 2795 | |
| 2796 | ```python |
| 2797 | set_data( |
| 2798 | *key: CreateSpanTypes, |
| 2799 | data: object = None, |
| 2800 | undo: bool | None = None, |
| 2801 | emit_event: bool | None = None, |
| 2802 | redraw: bool = True, |
| 2803 | event_data: EventDataDict | None = None, |
| 2804 | ) -> EventDataDict |
| 2805 | ``` |
| 2806 | Parameters: |
| 2807 | |
| 2808 | - `undo` when `True` adds the change to the Sheets undo stack. |
| 2809 | - `emit_event` when `True` causes a `"<<SheetModified>>` event to occur if it has been bound, see [here](#tkinter-and-tksheet-events) for more information. |
| 2810 | |
| 2811 | Example: |
| 2812 | ```python |
| 2813 | self.sheet.set_data( |
| 2814 | "A1", |
| 2815 | [["", "A", "B", "C"] |
| 2816 | ["1", "A1", "B1", "C1"], |
| 2817 | ["2", "A2", "B2", "C2"]], |
| 2818 | ) |
| 2819 | ``` |
| 2820 | |
| 2821 | You can clear cells/rows/columns using a [spans `clear()` function](#using-a-span-to-clear-cells) or the Sheets `clear()` function. Below is the Sheets clear function: |
| 2822 | |
| 2823 | ```python |
| 2824 | clear( |
| 2825 | *key: CreateSpanTypes, |
| 2826 | undo: bool | None = None, |
| 2827 | emit_event: bool | None = None, |
| 2828 | redraw: bool = True, |
| 2829 | ) -> EventDataDict |
| 2830 | ``` |
| 2831 | - `undo` when `True` adds the change to the Sheets undo stack. |
| 2832 | - `emit_event` when `True` causes a `"<<SheetModified>>` event to occur if it has been bound, see [here](#tkinter-and-tksheet-events) for more information. |
| 2833 | |
| 2834 | #### **Insert a row into the sheet** |
| 2835 | |
| 2836 | ```python |
| 2837 | insert_row( |
| 2838 | row: list[object] | tuple[object] | None = None, |
| 2839 | idx: str | int | None = None, |
| 2840 | height: int | None = None, |
| 2841 | row_index: bool = False, |
| 2842 | fill: bool = True, |
| 2843 | undo: bool = True, |
| 2844 | emit_event: bool = False, |
| 2845 | redraw: bool = True, |
| 2846 | ) -> EventDataDict |
| 2847 | ``` |
| 2848 | Parameters: |
| 2849 | |
| 2850 | - Leaving `row` as `None` inserts an empty row, e.g. `insert_row()` will append an empty row to the sheet. |
| 2851 | - `height` is the new rows displayed height in pixels, leave as `None` for default. |
| 2852 | - `row_index` when `True` assumes there is a row index value at the start of the row. |
| 2853 | - `fill` when `True` any provided rows that are shorter than the Sheets longest row will be filled with empty values up to the length of the longest row. |
| 2854 | - `undo` when `True` adds the change to the Sheets undo stack. |
| 2855 | - `emit_event` when `True` causes a `"<<SheetModified>>` event to occur if it has been bound, see [here](#tkinter-and-tksheet-events) for more information. |
| 2856 | |
| 2857 | ___ |
| 2858 | |
| 2859 | #### **Insert a column into the sheet** |
| 2860 | |
| 2861 | ```python |
| 2862 | insert_column( |
| 2863 | column: list[object] | tuple[object] | None = None, |
| 2864 | idx: str | int | None = None, |
| 2865 | width: int | None = None, |
| 2866 | header: bool = False, |
| 2867 | fill: bool = True, |
| 2868 | undo: bool = True, |
| 2869 | emit_event: bool = False, |
| 2870 | redraw: bool = True, |
| 2871 | ) -> EventDataDict |
| 2872 | ``` |
| 2873 | Parameters: |
| 2874 | |
| 2875 | - Leaving `column` as `None` inserts an empty column, e.g. `insert_column()` will append an empty column to the sheet. |
| 2876 | - `width` is the new columns displayed width in pixels, leave as `None` for default. |
| 2877 | - `header` when `True` assumes there is a header value at the start of the column. |
| 2878 | - `fill` when `True` any provided columns that are shorter than the Sheets longest column will be filled with empty values up to the length of the longest column. |
| 2879 | - `undo` when `True` adds the change to the Sheets undo stack. |
| 2880 | - `emit_event` when `True` causes a `"<<SheetModified>>` event to occur if it has been bound, see [here](#tkinter-and-tksheet-events) for more information. |
| 2881 | |
| 2882 | ___ |
| 2883 | |
| 2884 | #### **Insert multiple columns into the sheet** |
| 2885 | |
| 2886 | ```python |
| 2887 | insert_columns( |
| 2888 | columns: list[tuple[object] | list[object]] | tuple[tuple[object] | list[object]] | int = 1, |
| 2889 | idx: str | int | None = None, |
| 2890 | widths: list[int] | tuple[int] | None = None, |
| 2891 | headers: bool = False, |
| 2892 | fill: bool = True, |
| 2893 | undo: bool = True, |
| 2894 | emit_event: bool = False, |
| 2895 | create_selections: bool = True, |
| 2896 | add_row_heights: bool = True, |
| 2897 | push_ops: bool = True, |
| 2898 | redraw: bool = True, |
| 2899 | ) -> EventDataDict |
| 2900 | ``` |
| 2901 | Parameters: |
| 2902 | |
| 2903 | - `columns` if `int` will insert that number of blank columns. |
| 2904 | - `idx` (`str`, `int`, `None`) either `str` e.g. `"A"` for `0`, `int` or `None` for end. |
| 2905 | - `widths` are the new columns displayed widths in pixels, leave as `None` for default. |
| 2906 | - `headers` when `True` assumes there are headers values at the start of each column. |
| 2907 | - `fill` when `True` any provided columns that are shorter than the Sheets longest column will be filled with empty values up to the length of the longest column. |
| 2908 | - `undo` when `True` adds the change to the Sheets undo stack. |
| 2909 | - `emit_event` when `True` causes a `"<<SheetModified>>` event to occur if it has been bound, see [here](#tkinter-and-tksheet-events) for more information. |
| 2910 | - `create_selections` when `True` creates a selection box for the newly inserted columns. |
| 2911 | - `add_row_heights` when `True` creates rows if there are no pre-existing rows. |
| 2912 | - `push_ops` when `True` increases the indexes of all cell/column options such as dropdown boxes, highlights and data formatting. |
| 2913 | |
| 2914 | ___ |
| 2915 | |
| 2916 | #### **Insert multiple rows into the sheet** |
| 2917 | |
| 2918 | ```python |
| 2919 | insert_rows( |
| 2920 | rows: list[tuple[object] | list[object]] | tuple[tuple[object] | list[object]] | int = 1, |
| 2921 | idx: str | int | None = None, |
| 2922 | heights: list[int] | tuple[int] | None = None, |
| 2923 | row_index: bool = False, |
| 2924 | fill: bool = True, |
| 2925 | undo: bool = True, |
| 2926 | emit_event: bool = False, |
| 2927 | create_selections: bool = True, |
| 2928 | add_column_widths: bool = True, |
| 2929 | push_ops: bool = True, |
| 2930 | tree: bool = True, |
| 2931 | redraw: bool = True, |
| 2932 | ) -> EventDataDict |
| 2933 | ``` |
| 2934 | Parameters: |
| 2935 | |
| 2936 | - `rows` if `int` will insert that number of blank rows. |
| 2937 | - `idx` (`str`, `int`, `None`) either `str` e.g. `"A"` for `0`, `int` or `None` for end. |
| 2938 | - `heights` are the new rows displayed heights in pixels, leave as `None` for default. |
| 2939 | - `row_index` when `True` assumes there are row index values at the start of each row. |
| 2940 | - `fill` when `True` any provided rows that are shorter than the Sheets longest row will be filled with empty values up to the length of the longest row. |
| 2941 | - `undo` when `True` adds the change to the Sheets undo stack. |
| 2942 | - `emit_event` when `True` causes a `"<<SheetModified>>` event to occur if it has been bound, see [here](#tkinter-and-tksheet-events) for more information. |
| 2943 | - `create_selections` when `True` creates a selection box for the newly inserted rows. |
| 2944 | - `add_column_widths` when `True` creates columns if there are no pre-existing columns. |
| 2945 | - `push_ops` when `True` increases the indexes of all cell/row options such as dropdown boxes, highlights and data formatting. |
| 2946 | - `tree` is mainly used internally but when `True` and also when treeview mode is enabled it performs the necessary actions to create new ids and add them to the tree. |
| 2947 | |
| 2948 | ___ |
| 2949 | |
| 2950 | #### **Delete a row from the sheet** |
| 2951 | |
| 2952 | ```python |
| 2953 | del_row( |
| 2954 | idx: int = 0, |
| 2955 | data_indexes: bool = True, |
| 2956 | undo: bool = True, |
| 2957 | emit_event: bool = False, |
| 2958 | redraw: bool = True, |
| 2959 | ) -> EventDataDict |
| 2960 | ``` |
| 2961 | Parameters: |
| 2962 | |
| 2963 | - `idx` is the row to delete. |
| 2964 | - `data_indexes` only applicable when there are hidden rows. When `False` it makes the `idx` represent a displayed row and not the underlying Sheet data row. When `True` the index represent a data index. |
| 2965 | - `undo` when `True` adds the change to the Sheets undo stack. |
| 2966 | - `emit_event` when `True` causes a `"<<SheetModified>>` event to occur if it has been bound, see [here](#tkinter-and-tksheet-events) for more information. |
| 2967 | |
| 2968 | ___ |
| 2969 | |
| 2970 | #### **Delete multiple rows from the sheet** |
| 2971 | |
| 2972 | ```python |
| 2973 | del_rows( |
| 2974 | rows: int | Iterator[int], |
| 2975 | data_indexes: bool = True, |
| 2976 | undo: bool = True, |
| 2977 | emit_event: bool = False, |
| 2978 | redraw: bool = True, |
| 2979 | ) -> EventDataDict |
| 2980 | ``` |
| 2981 | Parameters: |
| 2982 | |
| 2983 | - `rows` can be either `int` or an iterable of `int`s representing row indexes. |
| 2984 | - `data_indexes` only applicable when there are hidden rows. When `False` it makes the `rows` indexes represent displayed rows and not the underlying Sheet data rows. When `True` the indexes represent data indexes. |
| 2985 | - `undo` when `True` adds the change to the Sheets undo stack. |
| 2986 | - `emit_event` when `True` causes a `"<<SheetModified>>` event to occur if it has been bound, see [here](#tkinter-and-tksheet-events) for more information. |
| 2987 | |
| 2988 | ___ |
| 2989 | |
| 2990 | #### **Delete a column from the sheet** |
| 2991 | |
| 2992 | ```python |
| 2993 | del_column( |
| 2994 | idx: int = 0, |
| 2995 | data_indexes: bool = True, |
| 2996 | undo: bool = True, |
| 2997 | emit_event: bool = False, |
| 2998 | redraw: bool = True, |
| 2999 | ) -> EventDataDict |
| 3000 | ``` |
| 3001 | Parameters: |
| 3002 | |
| 3003 | - `idx` is the column to delete. |
| 3004 | - `data_indexes` only applicable when there are hidden columns. When `False` it makes the `idx` represent a displayed column and not the underlying Sheet data column. When `True` the index represent a data index. |
| 3005 | - `undo` when `True` adds the change to the Sheets undo stack. |
| 3006 | - `emit_event` when `True` causes a `"<<SheetModified>>` event to occur if it has been bound, see [here](#tkinter-and-tksheet-events) for more information. |
| 3007 | |
| 3008 | ___ |
| 3009 | |
| 3010 | #### **Delete multiple columns from the sheet** |
| 3011 | |
| 3012 | ```python |
| 3013 | del_columns( |
| 3014 | columns: int | Iterator[int], |
| 3015 | data_indexes: bool = True, |
| 3016 | undo: bool = True, |
| 3017 | emit_event: bool = False, |
| 3018 | redraw: bool = True, |
| 3019 | ) -> EventDataDict |
| 3020 | ``` |
| 3021 | Parameters: |
| 3022 | |
| 3023 | - `columns` can be either `int` or an iterable of `int`s representing column indexes. |
| 3024 | - `data_indexes` only applicable when there are hidden columns. When `False` it makes the `columns` indexes represent displayed columns and not the underlying Sheet data columns. When `True` the indexes represent data indexes. |
| 3025 | - `undo` when `True` adds the change to the Sheets undo stack. |
| 3026 | - `emit_event` when `True` causes a `"<<SheetModified>>` event to occur if it has been bound, see [here](#tkinter-and-tksheet-events) for more information. |
| 3027 | |
| 3028 | ___ |
| 3029 | |
| 3030 | Expands or contracts the sheet **data** dimensions. |
| 3031 | ```python |
| 3032 | sheet_data_dimensions( |
| 3033 | total_rows: int | None = None, |
| 3034 | total_columns: int | None = None, |
| 3035 | ) -> Sheet |
| 3036 | ``` |
| 3037 | Parameters: |
| 3038 | |
| 3039 | - `total_rows` sets the Sheets number of data rows. |
| 3040 | - `total_columns` sets the Sheets number of data columns. |
| 3041 | |
| 3042 | ___ |
| 3043 | |
| 3044 | ```python |
| 3045 | set_sheet_data_and_display_dimensions( |
| 3046 | total_rows: int | None = None, |
| 3047 | total_columns: int | None = None, |
| 3048 | ) -> Sheet |
| 3049 | ``` |
| 3050 | Parameters: |
| 3051 | |
| 3052 | - `total_rows` when `int` will set the number of the Sheets data and display rows by deleting or adding rows. |
| 3053 | - `total_columns` when `int` will set the number of the Sheets data and display columns by deleting or adding columns. |
| 3054 | |
| 3055 | ___ |
| 3056 | |
| 3057 | ```python |
| 3058 | total_rows( |
| 3059 | number: int | None = None, |
| 3060 | mod_positions: bool = True, |
| 3061 | mod_data: bool = True, |
| 3062 | ) -> int | Sheet |
| 3063 | ``` |
| 3064 | Parameters: |
| 3065 | |
| 3066 | - `number` sets the Sheets number of data rows. When `None` function will return the Sheets number of data rows including the number of rows in the index. |
| 3067 | - `mod_positions` when `True` also sets the number of displayed rows. |
| 3068 | - `mod_data` when `True` also sets the number of data rows. |
| 3069 | |
| 3070 | ___ |
| 3071 | |
| 3072 | ```python |
| 3073 | total_columns( |
| 3074 | number: int | None = None, |
| 3075 | mod_positions: bool = True, |
| 3076 | mod_data: bool = True, |
| 3077 | ) -> int | Sheet |
| 3078 | ``` |
| 3079 | Parameters: |
| 3080 | |
| 3081 | - `number` sets the Sheets number of data columns. When `None` function will return the Sheets number of data columns including the number of columns in the header. |
| 3082 | - `mod_positions` when `True` also sets the number of displayed columns. |
| 3083 | - `mod_data` when `True` also sets the number of data columns. |
| 3084 | |
| 3085 | ___ |
| 3086 | |
| 3087 | #### **Move a single row to a new location** |
| 3088 | |
| 3089 | ```python |
| 3090 | move_row( |
| 3091 | row: int, |
| 3092 | moveto: int, |
| 3093 | ) -> tuple[dict, dict, dict] |
| 3094 | ``` |
| 3095 | - Note that `row` and `moveto` indexes represent displayed indexes and not data. When there are hidden rows this is an important distinction, otherwise it is not at all important. To specifically use data indexes use the function `move_rows()`. |
| 3096 | |
| 3097 | ___ |
| 3098 | |
| 3099 | #### **Move a single column to a new location** |
| 3100 | |
| 3101 | ```python |
| 3102 | move_column( |
| 3103 | column: int, |
| 3104 | moveto: int, |
| 3105 | ) -> tuple[dict[int, int], dict[int, int], EventDataDict]: |
| 3106 | ``` |
| 3107 | - Note that `column` and `moveto` indexes represent displayed indexes and not data. When there are hidden columns this is an important distinction, otherwise it is not at all important. To specifically use data indexes use the function `move_columns()`. |
| 3108 | |
| 3109 | ___ |
| 3110 | |
| 3111 | #### **Move any rows to a new location** |
| 3112 | |
| 3113 | ```python |
| 3114 | move_rows( |
| 3115 | move_to: int | None = None, |
| 3116 | to_move: list[int] | None = None, |
| 3117 | move_data: bool = True, |
| 3118 | data_indexes: bool = False, |
| 3119 | create_selections: bool = True, |
| 3120 | undo: bool = True, |
| 3121 | emit_event: bool = False, |
| 3122 | move_heights: bool = True, |
| 3123 | event_data: EventDataDict | None = None, |
| 3124 | redraw: bool = True, |
| 3125 | ) -> tuple[dict[int, int], dict[int, int], EventDataDict]: |
| 3126 | ``` |
| 3127 | Parameters: |
| 3128 | |
| 3129 | - `move_to` is the new start index for the rows to be moved to. |
| 3130 | - `to_move` is a `list` of row indexes to move to that new position, they will appear in the same order provided. |
| 3131 | - `move_data` when `True` moves not just the displayed row positions but the Sheet data as well. |
| 3132 | - `data_indexes` is only applicable when there are hidden rows. When `False` it makes the `move_to` and `to_move` indexes represent displayed rows and not the underlying Sheet data rows. When `True` the indexes represent data indexes. |
| 3133 | - `create_selections` creates new selection boxes based on where the rows have moved. |
| 3134 | - `undo` when `True` adds the change to the Sheets undo stack. |
| 3135 | - `emit_event` when `True` causes a `"<<SheetModified>>` event to occur if it has been bound, see [here](#tkinter-and-tksheet-events) for more information. |
| 3136 | - `move_heights` when `True` also moves the displayed row lines. |
| 3137 | |
| 3138 | Notes: |
| 3139 | - The rows in `to_move` do **not** have to be contiguous. |
| 3140 | |
| 3141 | ___ |
| 3142 | |
| 3143 | #### **Move any columns to a new location** |
| 3144 | |
| 3145 | ```python |
| 3146 | move_columns( |
| 3147 | move_to: int | None = None, |
| 3148 | to_move: list[int] | None = None, |
| 3149 | move_data: bool = True, |
| 3150 | data_indexes: bool = False, |
| 3151 | create_selections: bool = True, |
| 3152 | undo: bool = True, |
| 3153 | emit_event: bool = False, |
| 3154 | move_widths: bool = True, |
| 3155 | event_data: EventDataDict | None = None, |
| 3156 | redraw: bool = True, |
| 3157 | ) -> tuple[dict[int, int], dict[int, int], EventDataDict]: |
| 3158 | ``` |
| 3159 | Parameters: |
| 3160 | |
| 3161 | - `move_to` is the new start index for the columns to be moved to. |
| 3162 | - `to_move` is a `list` of column indexes to move to that new position, they will appear in the same order provided. |
| 3163 | - `move_data` when `True` moves not just the displayed column positions but the Sheet data as well. |
| 3164 | - `data_indexes` is only applicable when there are hidden columns. When `False` it makes the `move_to` and `to_move` indexes represent displayed columns and not the underlying Sheet data columns. When `True` the indexes represent data indexes. |
| 3165 | - `create_selections` creates new selection boxes based on where the columns have moved. |
| 3166 | - `undo` when `True` adds the change to the Sheets undo stack. |
| 3167 | - `emit_event` when `True` causes a `"<<SheetModified>>` event to occur if it has been bound, see [here](#tkinter-and-tksheet-events) for more information. |
| 3168 | - `move_widths` when `True` also moves the displayed column lines. |
| 3169 | |
| 3170 | Notes: |
| 3171 | |
| 3172 | - The columns in `to_move` do **not** have to be contiguous. |
| 3173 | |
| 3174 | ___ |
| 3175 | |
| 3176 | #### **Move any columns to new locations** |
| 3177 | |
| 3178 | ```python |
| 3179 | mapping_move_columns( |
| 3180 | data_new_idxs: dict[int, int], |
| 3181 | disp_new_idxs: None | dict[int, int] = None, |
| 3182 | move_data: bool = True, |
| 3183 | create_selections: bool = True, |
| 3184 | undo: bool = True, |
| 3185 | emit_event: bool = False, |
| 3186 | redraw: bool = True, |
| 3187 | ) -> tuple[dict[int, int], dict[int, int], EventDataDict] |
| 3188 | ``` |
| 3189 | Parameters: |
| 3190 | |
| 3191 | - `data_new_idxs` (`dict[int, int]`) must be a `dict` where the keys are the data columns to move as `int`s and the values are their new locations as `int`s. |
| 3192 | - `disp_new_idxs` (`None | dict[int, int]`) either `None` or a `dict` where the keys are the displayed columns (basically the column widths) to move as `int`s and the values are their new locations as `int`s. If `None` then no column widths will be moved. |
| 3193 | - `move_data` when `True` moves not just the displayed column positions but the Sheet data as well. |
| 3194 | - `create_selections` creates new selection boxes based on where the columns have moved. |
| 3195 | - `undo` when `True` adds the change to the Sheets undo stack. |
| 3196 | - `emit_event` when `True` causes a `"<<SheetModified>>` event to occur if it has been bound, see [here](#tkinter-and-tksheet-events) for more information. |
| 3197 | |
| 3198 | ___ |
| 3199 | |
| 3200 | Get a mapping (`dict`) of all `old: new` column indexes. |
| 3201 | |
| 3202 | ```python |
| 3203 | full_move_columns_idxs(data_idxs: dict[int, int]) -> dict[int, int] |
| 3204 | ``` |
| 3205 | - e.g. converts `{0: 1}` to `{0: 1, 1: 0}` if the maximum Sheet column number is `1`. |
| 3206 | |
| 3207 | ___ |
| 3208 | |
| 3209 | #### **Move any rows to new locations** |
| 3210 | |
| 3211 | ```python |
| 3212 | mapping_move_rows( |
| 3213 | data_new_idxs: dict[int, int], |
| 3214 | disp_new_idxs: None | dict[int, int] = None, |
| 3215 | move_data: bool = True, |
| 3216 | create_selections: bool = True, |
| 3217 | undo: bool = True, |
| 3218 | emit_event: bool = False, |
| 3219 | redraw: bool = True, |
| 3220 | ) -> tuple[dict[int, int], dict[int, int], EventDataDict] |
| 3221 | ``` |
| 3222 | Parameters: |
| 3223 | |
| 3224 | - `data_new_idxs` (`dict[int, int]`) must be a `dict` where the keys are the data rows to move as `int`s and the values are their new locations as `int`s. |
| 3225 | - `disp_new_idxs` (`None | dict[int, int]`) either `None` or a `dict` where the keys are the displayed rows (basically the row heights) to move as `int`s and the values are their new locations as `int`s. If `None` then no row heights will be moved. |
| 3226 | - `move_data` when `True` moves not just the displayed row positions but the Sheet data as well. |
| 3227 | - `create_selections` creates new selection boxes based on where the rows have moved. |
| 3228 | - `undo` when `True` adds the change to the Sheets undo stack. |
| 3229 | - `emit_event` when `True` causes a `"<<SheetModified>>` event to occur if it has been bound, see [here](#tkinter-and-tksheet-events) for more information. |
| 3230 | |
| 3231 | ___ |
| 3232 | |
| 3233 | Get a mapping (`dict`) of all `old: new` row indexes. |
| 3234 | |
| 3235 | ```python |
| 3236 | full_move_rows_idxs(data_idxs: dict[int, int]) -> dict[int, int] |
| 3237 | ``` |
| 3238 | - e.g. converts `{0: 1}` to `{0: 1, 1: 0}` if the maximum Sheet row number is `1`. |
| 3239 | |
| 3240 | ___ |
| 3241 | |
| 3242 | #### **Make all data rows the same length** |
| 3243 | |
| 3244 | ```python |
| 3245 | equalize_data_row_lengths(include_header: bool = True) -> int |
| 3246 | ``` |
| 3247 | - Makes every list in the table have the same number of elements, goes by longest list. This will only affect the data variable, not visible columns. |
| 3248 | - Returns the new row length for all rows in the Sheet. |
| 3249 | |
| 3250 | --- |
| 3251 | # **Sorting the Table** |
| 3252 | |
| 3253 | There are three built-in sorting keys to choose from but you can always create your own and use that instead. See [here](#setting-the-default-sorting-key) for more information on how to set the default sorting key. |
| 3254 | |
| 3255 | #### **natural_sort_key** |
| 3256 | |
| 3257 | This is the **default** sorting key for natural sorting of various Python types: |
| 3258 | |
| 3259 | - Won't sort string version numbers. |
| 3260 | - Will convert strings to floats. |
| 3261 | - Will sort strings that are file paths. |
| 3262 | |
| 3263 | Order: |
| 3264 | |
| 3265 | 0. None |
| 3266 | 1. Empty strings |
| 3267 | 2. bool |
| 3268 | 3. int, float (inc. strings that are numbers) |
| 3269 | 4. datetime (inc. strings that are dates) |
| 3270 | 5. strings (including string file paths and paths as POSIX strings) & unknown objects with __str__ |
| 3271 | 6. unknown objects |
| 3272 | |
| 3273 | #### **version_sort_key** |
| 3274 | |
| 3275 | An alternative sorting key that respects and sorts most version numbers: |
| 3276 | |
| 3277 | - Won't convert strings to floats. |
| 3278 | - Will sort string version numbers. |
| 3279 | - Will sort strings that are file paths. |
| 3280 | |
| 3281 | 0. None |
| 3282 | 1. Empty strings |
| 3283 | 2. bool |
| 3284 | 3. int, float |
| 3285 | 4. datetime (inc. strings that are dates) |
| 3286 | 5. strings (including string file paths and paths as POSIX strings) & unknown objects with __str__ |
| 3287 | 6. unknown objects |
| 3288 | |
| 3289 | #### **fast_sort_key** |
| 3290 | |
| 3291 | A faster key for natural sorting of various Python types. This key should probably be used if you intend on sorting sheets with over a million cells: |
| 3292 | |
| 3293 | - Won't sort strings that are dates very well. |
| 3294 | - Won't convert strings to floats. |
| 3295 | - Won't sort string file paths very well. |
| 3296 | - Will do ok with string version numbers. |
| 3297 | |
| 3298 | 0. None |
| 3299 | 1. Empty strings |
| 3300 | 2. bool |
| 3301 | 3. int, float |
| 3302 | 4. datetime |
| 3303 | 5. strings (including paths as POSIX strings) & unknown objects with __str__ |
| 3304 | 6. unknown objects |
| 3305 | |
| 3306 | #### **Setting the default sorting key** |
| 3307 | |
| 3308 | Setting the sorting key at initialization: |
| 3309 | ```python |
| 3310 | from tksheet import Sheet, natural_sort_key |
| 3311 | |
| 3312 | my_sheet = Sheet(parent=parent, sort_key=natural_sort_key) |
| 3313 | ``` |
| 3314 | |
| 3315 | Setting the sorting key after initialization: |
| 3316 | ```python |
| 3317 | from tksheet import Sheet, natural_sort_key |
| 3318 | |
| 3319 | my_sheet.set_options(sort_key=natural_sort_key) |
| 3320 | ``` |
| 3321 | |
| 3322 | Using a sorting key with a tksheet sort function call: |
| 3323 | ```python |
| 3324 | from tksheet import Sheet, natural_sort_key |
| 3325 | |
| 3326 | my_sheet.sort_columns(0, key=natural_sort_key) |
| 3327 | ``` |
| 3328 | - Setting the key like this will, for this call, override whatever key was set at initialization or using `set_options()`. |
| 3329 | |
| 3330 | #### **Notes about sorting** |
| 3331 | |
| 3332 | - The readonly functions can be used to disallow sorting of particular cells/rows/columns values. |
| 3333 | |
| 3334 | #### **Sorting cells** |
| 3335 | |
| 3336 | ```python |
| 3337 | sort( |
| 3338 | *box: CreateSpanTypes, |
| 3339 | reverse: bool = False, |
| 3340 | row_wise: bool = False, |
| 3341 | validation: bool = True, |
| 3342 | key: Callable | None = None, |
| 3343 | undo: bool = True, |
| 3344 | ) -> EventDataDict |
| 3345 | ``` |
| 3346 | Parameters: |
| 3347 | |
| 3348 | - `boxes` (`CreateSpanTypes`). |
| 3349 | - `reverse` (`bool`) if `True` sorts in reverse (descending) order. |
| 3350 | - `row-wise` (`bool`) if `True` sorts values row-wise. Default is column-wise. |
| 3351 | - `key` (`Callable`, `None`) if `None` then uses the default sorting key. |
| 3352 | - `undo` (`bool`) if `True` then adds the change (if a change was made) to the undo stack. |
| 3353 | |
| 3354 | Notes: |
| 3355 | |
| 3356 | - Sort the values of the box columns, or the values of the box rows if `row_wise` is `True`. |
| 3357 | - **Will not shift cell options (properties) around, only cell values.** |
| 3358 | - The event name in `EventDataDict` for sorting table values is `"edit_table"`. |
| 3359 | - The readonly functions can be used to disallow sorting of particular cells values. |
| 3360 | |
| 3361 | Example: |
| 3362 | ```python |
| 3363 | # a box of cells row-wise |
| 3364 | # start at row 0 & col 0, up to but not including row 10 & col 10 |
| 3365 | my_sheet.sort(0, 0, 10, 10, row_wise=True) |
| 3366 | |
| 3367 | # sort the whole table |
| 3368 | my_sheet.sort(None) |
| 3369 | ``` |
| 3370 | |
| 3371 | #### **Sorting row values** |
| 3372 | |
| 3373 | ```python |
| 3374 | def sort_rows( |
| 3375 | rows: AnyIter[int] | Span | int | None = None, |
| 3376 | reverse: bool = False, |
| 3377 | validation: bool = True, |
| 3378 | key: Callable | None = None, |
| 3379 | undo: bool = True, |
| 3380 | ) -> EventDataDict |
| 3381 | ``` |
| 3382 | Parameters: |
| 3383 | |
| 3384 | - `rows` (`AnyIter[int]` , `Span`, `int`, `None`) the rows to sort. |
| 3385 | - `reverse` (`bool`) if `True` then sorts in reverse (descending) order. |
| 3386 | - `key` (`Callable`, `None`) if `None` then uses the default sorting key. |
| 3387 | - `undo` (`bool`) if `True` then adds the change (if a change was made) to the undo stack. |
| 3388 | |
| 3389 | Notes: |
| 3390 | |
| 3391 | - Sorts the values of each row independently. |
| 3392 | - **Will not shift cell options (properties) around, only cell values.** |
| 3393 | - The event name in `EventDataDict` for sorting table values is `"edit_table"`. |
| 3394 | - The readonly functions can be used to disallow sorting of particular rows values. |
| 3395 | |
| 3396 | #### **Sorting column values** |
| 3397 | |
| 3398 | ```python |
| 3399 | def sort_columns( |
| 3400 | columns: AnyIter[int] | Span | int | None = None, |
| 3401 | reverse: bool = False, |
| 3402 | validation: bool = True, |
| 3403 | key: Callable | None = None, |
| 3404 | undo: bool = True, |
| 3405 | ) -> EventDataDict |
| 3406 | ``` |
| 3407 | Parameters: |
| 3408 | |
| 3409 | - `columns` (`AnyIter[int]` , `Span`, `int`, `None`) the columns to sort. |
| 3410 | - `reverse` (`bool`) if `True` then sorts in reverse (descending) order. |
| 3411 | - `key` (`Callable`, `None`) if `None` then uses the default sorting key. |
| 3412 | - `undo` (`bool`) if `True` then adds the change (if a change was made) to the undo stack. |
| 3413 | |
| 3414 | Notes: |
| 3415 | |
| 3416 | - Sorts the values of each column independently. |
| 3417 | - **Will not shift cell options (properties) around, only cell values.** |
| 3418 | - The event name in `EventDataDict` for sorting table values is `"edit_table"`. |
| 3419 | - The readonly functions can be used to disallow sorting of particular columns values. |
| 3420 | |
| 3421 | #### **Sorting the order of all rows using a column** |
| 3422 | |
| 3423 | ```python |
| 3424 | def sort_rows_by_column( |
| 3425 | column: int | None = None, |
| 3426 | reverse: bool = False, |
| 3427 | key: Callable | None = None, |
| 3428 | undo: bool = True, |
| 3429 | ) -> EventDataDict |
| 3430 | ``` |
| 3431 | Parameters: |
| 3432 | |
| 3433 | - `column` (`int`, `None`) if `None` then it uses the currently selected column to sort. |
| 3434 | - `reverse` (`bool`) if `True` then sorts in reverse (descending) order. |
| 3435 | - `key` (`Callable`, `None`) if `None` then uses the default sorting key. |
| 3436 | - `undo` (`bool`) if `True` then adds the change (if a change was made) to the undo stack. |
| 3437 | |
| 3438 | Notes: |
| 3439 | |
| 3440 | - Sorts the tree if treeview mode is active. |
| 3441 | |
| 3442 | #### **Sorting the order of all columns using a row** |
| 3443 | |
| 3444 | ```python |
| 3445 | def sort_columns_by_row( |
| 3446 | row: int | None = None, |
| 3447 | reverse: bool = False, |
| 3448 | key: Callable | None = None, |
| 3449 | undo: bool = True, |
| 3450 | ) -> EventDataDict |
| 3451 | ``` |
| 3452 | Parameters: |
| 3453 | |
| 3454 | - `row` (`int`, `None`) if `None` then it uses the currently selected row to sort. |
| 3455 | - `reverse` (`bool`) if `True` then sorts in reverse (descending) order. |
| 3456 | - `key` (`Callable`, `None`) if `None` then uses the default sorting key. |
| 3457 | - `undo` (`bool`) if `True` then adds the change (if a change was made) to the undo stack. |
| 3458 | |
| 3459 | --- |
| 3460 | # **Find and Replace** |
| 3461 | |
| 3462 | An in-built find and replace window can be enabled using `enable_bindings()`, e.g: |
| 3463 | |
| 3464 | ```python |
| 3465 | my_sheet.enable_bindings("find", "replace") |
| 3466 | |
| 3467 | # all bindings, including find and replace |
| 3468 | my_sheet.enable_bindings() |
| 3469 | ``` |
| 3470 | |
| 3471 | See [enable_bindings](#enable-table-functionality-and-bindings) for more information. |
| 3472 | |
| 3473 | There are also some `Sheet()` functions that can be utilized, shown below. |
| 3474 | |
| 3475 | #### **Check if the find window is open** |
| 3476 | |
| 3477 | ```python |
| 3478 | @property |
| 3479 | find_open() -> bool |
| 3480 | ``` |
| 3481 | e.g. `find_is_open = sheet.find_open` |
| 3482 | |
| 3483 | #### **Open or close the find window** |
| 3484 | |
| 3485 | ```python |
| 3486 | open_find(focus: bool = False) -> Sheet |
| 3487 | ``` |
| 3488 | |
| 3489 | ```python |
| 3490 | close_find() -> Sheet |
| 3491 | ``` |
| 3492 | |
| 3493 | #### **Find and select** |
| 3494 | |
| 3495 | ```python |
| 3496 | next_match(within: bool | None = None, find: str | None = None) -> Sheet |
| 3497 | ``` |
| 3498 | |
| 3499 | ```python |
| 3500 | prev_match(within: bool | None = None, find: str | None = None) -> Sheet |
| 3501 | ``` |
| 3502 | Parameters: |
| 3503 | |
| 3504 | - `within` (`bool`, `None`) if `bool` then will override the find windows within selection setting. If `None` then it will use the find windows setting. |
| 3505 | - `find` (`str`, `None`) if `str` then will override the find windows search value. If `None` then it will use the find windows search value. |
| 3506 | |
| 3507 | Notes: |
| 3508 | |
| 3509 | - If looking within selection then hidden rows and columns will be skipped. |
| 3510 | |
| 3511 | #### **Replace all using mapping** |
| 3512 | |
| 3513 | ```python |
| 3514 | replace_all(mapping: dict[str, str], within: bool = False) -> EventDataDict |
| 3515 | ``` |
| 3516 | Parameters: |
| 3517 | |
| 3518 | - `mapping` (`dict[str, str]`) a `dict` of keys to search for and values to replace them with. |
| 3519 | - `within` (`bool`) when `True` will only do replaces inside existing selection boxes. |
| 3520 | |
| 3521 | Notes: |
| 3522 | |
| 3523 | - Will do partial cell data replaces also. |
| 3524 | - If looking within selection then hidden rows and columns will be skipped. |
| 3525 | |
| 3526 | --- |
| 3527 | # **Highlighting Cells** |
| 3528 | |
| 3529 | ### **Creating highlights** |
| 3530 | |
| 3531 | #### **Using spans to create highlights** |
| 3532 | |
| 3533 | `Span` objects (more information [here](#span-objects)) can be used to highlight cells, rows, columns, the entire sheet, headers and the index. |
| 3534 | |
| 3535 | You can use either of the following methods: |
| 3536 | - Using a span method e.g. `span.highlight()` more information [here](#using-a-span-to-create-highlights). |
| 3537 | - Using a sheet method e.g. `sheet.highlight(Span)` |
| 3538 | |
| 3539 | Or if you need user inserted row/columns in the middle of highlight areas to also be highlighted you can use named spans, more information [here](#named-spans). |
| 3540 | |
| 3541 | Whether cells, rows or columns are highlighted depends on the [`kind`](#get-a-spans-kind) of span. |
| 3542 | |
| 3543 | ```python |
| 3544 | highlight( |
| 3545 | *key: CreateSpanTypes, |
| 3546 | bg: bool | None | str = False, |
| 3547 | fg: bool | None | str = False, |
| 3548 | end: bool | None = None, |
| 3549 | overwrite: bool = False, |
| 3550 | redraw: bool = True, |
| 3551 | ) -> Span |
| 3552 | ``` |
| 3553 | Parameters: |
| 3554 | |
| 3555 | - `key` (`CreateSpanTypes`) either a span or a type which can create a span. See [here](#creating-a-span) for more information on the types that can create a span. |
| 3556 | - `bg` and `fg` arguments use either a tkinter color or a hex `str` color. |
| 3557 | - `end` (`bool`) is used for row highlighting where `True` makes the highlight go to the end of the Sheet window on the x axis. |
| 3558 | - `overwrite` (`bool`) when `True` overwrites the any previous highlight for that cell/row/column, whereas `False` will only impact the keyword arguments used. |
| 3559 | - Highlighting cells, rows or columns will also change the colors of dropdown boxes and check boxes. |
| 3560 | |
| 3561 | Example: |
| 3562 | ```python |
| 3563 | # highlight cell - row 3, column 5 |
| 3564 | self.sheet.highlight( |
| 3565 | (3, 5), |
| 3566 | bg="dark green", |
| 3567 | fg="white", |
| 3568 | ) |
| 3569 | |
| 3570 | # or |
| 3571 | |
| 3572 | # same cells, background red, text color black |
| 3573 | sheet[3, 5].bg = "red" |
| 3574 | sheet[3, 5].fg = "black" |
| 3575 | ``` |
| 3576 | |
| 3577 | #### **Other ways to create highlights** |
| 3578 | |
| 3579 | **Cells** |
| 3580 | |
| 3581 | ```python |
| 3582 | highlight_cells( |
| 3583 | row: int | Literal["all"] = 0, |
| 3584 | column: int | Literal["all"] = 0, |
| 3585 | cells: list[tuple[int, int]] = [], |
| 3586 | canvas: Literal["table", "index", "header"] = "table", |
| 3587 | bg: bool | None | str = False, |
| 3588 | fg: bool | None | str = False, |
| 3589 | redraw: bool = True, |
| 3590 | overwrite: bool = True, |
| 3591 | ) -> Sheet |
| 3592 | ``` |
| 3593 | |
| 3594 | **Rows** |
| 3595 | |
| 3596 | ```python |
| 3597 | highlight_rows( |
| 3598 | rows: Iterator[int] | int, |
| 3599 | bg: None | str = None, |
| 3600 | fg: None | str = None, |
| 3601 | highlight_index: bool = True, |
| 3602 | redraw: bool = True, |
| 3603 | end_of_screen: bool = False, |
| 3604 | overwrite: bool = True, |
| 3605 | ) -> Sheet |
| 3606 | ``` |
| 3607 | |
| 3608 | **Columns** |
| 3609 | |
| 3610 | ```python |
| 3611 | highlight_columns( |
| 3612 | columns: Iterator[int] | int, |
| 3613 | bg: bool | None | str = False, |
| 3614 | fg: bool | None | str = False, |
| 3615 | highlight_header: bool = True, |
| 3616 | redraw: bool = True, |
| 3617 | overwrite: bool = True, |
| 3618 | ) -> Sheet |
| 3619 | ``` |
| 3620 | |
| 3621 | ___ |
| 3622 | |
| 3623 | ### **Deleting highlights** |
| 3624 | |
| 3625 | #### **Using spans to delete highlights** |
| 3626 | |
| 3627 | If the highlights were created by a named span then the named span must be deleted, more information [here](#deleting-a-named-span). |
| 3628 | |
| 3629 | Otherwise you can use either of the following methods to delete/remove highlights: |
| 3630 | |
| 3631 | - Using a span method e.g. `span.dehighlight()` more information [here](#using-a-span-to-delete-highlights). |
| 3632 | - Using a sheet method e.g. `sheet.dehighlight(Span)` details below: |
| 3633 | |
| 3634 | ```python |
| 3635 | dehighlight( |
| 3636 | *key: CreateSpanTypes, |
| 3637 | redraw: bool = True, |
| 3638 | ) -> Span |
| 3639 | ``` |
| 3640 | Parameters: |
| 3641 | |
| 3642 | - `key` (`CreateSpanTypes`) either a span or a type which can create a span. See [here](#creating-a-span) for more information on the types that can create a span. |
| 3643 | |
| 3644 | Example: |
| 3645 | ```python |
| 3646 | # highlight column B |
| 3647 | self.sheet.highlight( |
| 3648 | "B", |
| 3649 | bg="dark green", |
| 3650 | fg="white", |
| 3651 | ) |
| 3652 | |
| 3653 | # dehighlight column B |
| 3654 | self.sheet.dehighlight("B") |
| 3655 | ``` |
| 3656 | |
| 3657 | #### **Other ways to delete highlights** |
| 3658 | |
| 3659 | **Cells** |
| 3660 | |
| 3661 | ```python |
| 3662 | dehighlight_cells( |
| 3663 | row: int | Literal["all"] = 0, |
| 3664 | column: int = 0, |
| 3665 | cells: list[tuple[int, int]] = [], |
| 3666 | canvas: Literal["table", "row_index", "header"] = "table", |
| 3667 | all_: bool = False, |
| 3668 | redraw: bool = True, |
| 3669 | ) -> Sheet |
| 3670 | ``` |
| 3671 | |
| 3672 | **Rows** |
| 3673 | |
| 3674 | ```python |
| 3675 | dehighlight_rows( |
| 3676 | rows: list[int] | Literal["all"] = [], |
| 3677 | redraw: bool = True, |
| 3678 | ) -> Sheet |
| 3679 | ``` |
| 3680 | |
| 3681 | **Columns** |
| 3682 | |
| 3683 | ```python |
| 3684 | dehighlight_columns( |
| 3685 | columns: list[int] | Literal["all"] = [], |
| 3686 | redraw: bool = True, |
| 3687 | ) -> Sheet |
| 3688 | ``` |
| 3689 | |
| 3690 | **All** |
| 3691 | |
| 3692 | ```python |
| 3693 | dehighlight_all( |
| 3694 | cells: bool = True, |
| 3695 | rows: bool = True, |
| 3696 | columns: bool = True, |
| 3697 | header: bool = True, |
| 3698 | index: bool = True, |
| 3699 | redraw: bool = True, |
| 3700 | ) -> Sheet |
| 3701 | ``` |
| 3702 | |
| 3703 | --- |
| 3704 | # **Dropdown Boxes** |
| 3705 | |
| 3706 | #### **Creating dropdown boxes** |
| 3707 | |
| 3708 | `Span` objects (more information [here](#span-objects)) can be used to create dropdown boxes for cells, rows, columns, the entire sheet, headers and the index. |
| 3709 | |
| 3710 | You can use either of the following methods: |
| 3711 | |
| 3712 | - Using a span method e.g. `span.dropdown()` more information [here](#using-a-span-to-create-dropdown-boxes). |
| 3713 | - Using a sheet method e.g. `sheet.dropdown(Span)` |
| 3714 | |
| 3715 | Or if you need user inserted row/columns in the middle of areas with dropdown boxes to also have dropdown boxes you can use named spans, more information [here](#named-spans). |
| 3716 | |
| 3717 | Whether dropdown boxes are created for cells, rows or columns depends on the [`kind`](#get-a-spans-kind) of span. |
| 3718 | |
| 3719 | ```python |
| 3720 | dropdown( |
| 3721 | *key: CreateSpanTypes, |
| 3722 | values: list = [], |
| 3723 | edit_data: bool = True, |
| 3724 | set_values: dict[tuple[int, int] | int, object] | None = None, |
| 3725 | set_value: object = None, |
| 3726 | state: Literal["normal", "readonly", "disabled"] = "normal", |
| 3727 | redraw: bool = True, |
| 3728 | selection_function: Callable | None = None, |
| 3729 | modified_function: Callable | None = None, |
| 3730 | search_function: Callable = dropdown_search_function, |
| 3731 | validate_input: bool = True, |
| 3732 | text: None | str = None, |
| 3733 | ) -> Span |
| 3734 | ``` |
| 3735 | Notes: |
| 3736 | |
| 3737 | - `selection_function` / `modified_function` (`Callable`, `None`) parameters require either `None` or a function. The function you use needs at least one argument because tksheet will send information to your function about the triggered dropdown. |
| 3738 | - When a user selects an item from the dropdown box the sheet will set the underlying cells data to the selected item, to bind this event use either the `selection_function` argument or see the function `extra_bindings()` with binding `"end_edit_cell"` [here](#table-functionality-and-bindings). |
| 3739 | |
| 3740 | Parameters: |
| 3741 | |
| 3742 | - `key` (`CreateSpanTypes`) either a span or a type which can create a span. See [here](#creating-a-span) for more information on the types that can create a span. |
| 3743 | - `values` are the values to appear in a list view type interface when the dropdown box is open. |
| 3744 | - `edit_data` when `True` makes edits in the table, header or index (depending on the span) based on `set_values`/`set_value`. |
| 3745 | - `set_values` when combined with `edit_data=True` allows a `dict` to be provided of data coordinates (`tuple[int, int]` for a cell span or `int` for a row/column span) as `key`s and values to set the cell at that coordinate to. |
| 3746 | - e.g. `set_values={(0, 0): "new value for A1"}`. |
| 3747 | - The idea behind this parameter is that an entire column or row can have individual cell values and is not set to `set_value` alone. |
| 3748 | - `set_value` when combined with `edit_data=True` sets every cell in the span to the value provided. If left as `None` and if `set_values` is also `None` then the topmost value from `values` will be used or if not `values` then `""`. |
| 3749 | - `state` determines whether or not there is also an editable text window at the top of the dropdown box when it is open. |
| 3750 | - `redraw` refreshes the sheet so the newly created box is visible. |
| 3751 | - `selection_function` can be used to trigger a specific function when an item from the dropdown box is selected, if you are using the above `extra_bindings()` as well it will also be triggered but after this function. e.g. `selection_function = my_function_name` |
| 3752 | - `modified_function` can be used to trigger a specific function when the `state` of the box is set to `"normal"` and there is an editable text window and a change of the text in that window has occurred. Note that this function occurs before the dropdown boxes search feature. |
| 3753 | - `search_function` (`None`, `callable`) sets the function that will be used to search the dropdown boxes values upon a dropdown text editor modified event when the dropdowns state is `normal`. Set to `None` to disable the search feature or use your own function with the following keyword arguments: `(search_for, data):` and make it return an row number (e.g. select and see the first value would be `0`) if positive and `None` if negative. |
| 3754 | - `validate_input` (`bool`) when `True` will not allow cut, paste, delete or cell editor to input values to cell which are not in the dropdown boxes values. |
| 3755 | - `text` (`None`, `str`) can be set to something other than `None` to always display over whatever value is in the cell, this is useful when you want to display a Header name over a dropdown box selection. |
| 3756 | |
| 3757 | Example: |
| 3758 | ```python |
| 3759 | # create dropdown boxes in column "D" |
| 3760 | self.sheet.dropdown( |
| 3761 | "D", |
| 3762 | values=[0, 1, 2, 3, 4], |
| 3763 | ) |
| 3764 | ``` |
| 3765 | |
| 3766 | ___ |
| 3767 | |
| 3768 | #### **Deleting dropdown boxes** |
| 3769 | |
| 3770 | If the dropdown boxes were created by a named span then the named span must be deleted, more information [here](#deleting-a-named-span). |
| 3771 | |
| 3772 | Otherwise you can use either of the following methods to delete/remove dropdown boxes. |
| 3773 | - Using a span method e.g. `span.del_dropdown()` more information [here](#using-a-span-to-delete-dropdown-boxes). |
| 3774 | - Using a sheet method e.g. `sheet.del_dropdown(Span)` details below: |
| 3775 | |
| 3776 | ```python |
| 3777 | del_dropdown( |
| 3778 | *key: CreateSpanTypes, |
| 3779 | redraw: bool = True, |
| 3780 | ) -> Span |
| 3781 | ``` |
| 3782 | Parameters: |
| 3783 | |
| 3784 | - `key` (`CreateSpanTypes`) either a span or a type which can create a span. See [here](#creating-a-span) for more information on the types that can create a span. |
| 3785 | |
| 3786 | Example: |
| 3787 | ```python |
| 3788 | # create dropdown boxes in column "D" |
| 3789 | self.sheet.dropdown( |
| 3790 | "D", |
| 3791 | values=[0, 1, 2, 3, 4], |
| 3792 | ) |
| 3793 | |
| 3794 | # delete dropdown boxes in column "D" |
| 3795 | self.sheet.del_dropdown("D") |
| 3796 | ``` |
| 3797 | |
| 3798 | ___ |
| 3799 | |
| 3800 | #### **Get chosen dropdown boxes values** |
| 3801 | |
| 3802 | ```python |
| 3803 | get_dropdown_values(r: int = 0, c: int = 0) -> None | list |
| 3804 | ``` |
| 3805 | |
| 3806 | ```python |
| 3807 | get_header_dropdown_values(c: int = 0) -> None | list |
| 3808 | ``` |
| 3809 | |
| 3810 | ```python |
| 3811 | get_index_dropdown_values(r: int = 0) -> None | list |
| 3812 | ``` |
| 3813 | |
| 3814 | ___ |
| 3815 | |
| 3816 | #### **Set the values and cell value of a chosen dropdown box** |
| 3817 | |
| 3818 | ```python |
| 3819 | set_dropdown_values( |
| 3820 | r: int = 0, |
| 3821 | c: int = 0, |
| 3822 | set_existing_dropdown: bool = False, |
| 3823 | values: list = [], |
| 3824 | set_value: object = None, |
| 3825 | ) -> Sheet |
| 3826 | ``` |
| 3827 | |
| 3828 | ```python |
| 3829 | set_header_dropdown_values( |
| 3830 | c: int = 0, |
| 3831 | set_existing_dropdown: bool = False, |
| 3832 | values: list = [], |
| 3833 | set_value: object = None, |
| 3834 | ) -> Sheet |
| 3835 | ``` |
| 3836 | |
| 3837 | ```python |
| 3838 | set_index_dropdown_values( |
| 3839 | r: int = 0, |
| 3840 | set_existing_dropdown: bool = False, |
| 3841 | values: list = [], |
| 3842 | set_value: object = None, |
| 3843 | ) -> Sheet |
| 3844 | ``` |
| 3845 | Parameters: |
| 3846 | |
| 3847 | - `set_existing_dropdown` if `True` takes priority over `r` and `c` and sets the values of the last popped open dropdown box (if one one is popped open, if not then an `Exception` is raised). |
| 3848 | - `values` (`list`, `tuple`) |
| 3849 | - `set_value` (`str`, `None`) if not `None` will try to set the value of the chosen cell to given argument. |
| 3850 | |
| 3851 | ___ |
| 3852 | |
| 3853 | #### **Set or get bound dropdown functions** |
| 3854 | |
| 3855 | ```python |
| 3856 | dropdown_functions( |
| 3857 | r: int, |
| 3858 | c: int, |
| 3859 | selection_function: str | Callable = "", |
| 3860 | modified_function: str | Callable = "", |
| 3861 | ) -> None | dict |
| 3862 | ``` |
| 3863 | |
| 3864 | ```python |
| 3865 | header_dropdown_functions( |
| 3866 | c: int, |
| 3867 | selection_function: str | Callable = "", |
| 3868 | modified_function: str | Callable = "", |
| 3869 | ) -> None | dict |
| 3870 | ``` |
| 3871 | |
| 3872 | ```python |
| 3873 | index_dropdown_functions( |
| 3874 | r: int, |
| 3875 | selection_function: str | Callable = "", |
| 3876 | modified_function: str | Callable = "", |
| 3877 | ) -> None | dict |
| 3878 | ``` |
| 3879 | |
| 3880 | ___ |
| 3881 | |
| 3882 | #### **Get a dictionary of all dropdown boxes** |
| 3883 | |
| 3884 | ```python |
| 3885 | get_dropdowns() -> dict |
| 3886 | ``` |
| 3887 | Returns: |
| 3888 | ```python |
| 3889 | {(row int, column int): {'values': values, |
| 3890 | 'select_function': selection_function, |
| 3891 | 'modified_function': modified_function, |
| 3892 | 'state': state, |
| 3893 | 'text': text}} |
| 3894 | ``` |
| 3895 | |
| 3896 | ```python |
| 3897 | get_header_dropdowns() -> dict |
| 3898 | ``` |
| 3899 | |
| 3900 | ```python |
| 3901 | get_index_dropdowns() -> dict |
| 3902 | ``` |
| 3903 | |
| 3904 | ___ |
| 3905 | |
| 3906 | #### **Open a dropdown box** |
| 3907 | |
| 3908 | ```python |
| 3909 | open_dropdown(r: int, c: int) -> Sheet |
| 3910 | ``` |
| 3911 | |
| 3912 | ```python |
| 3913 | open_header_dropdown(c: int) -> Sheet |
| 3914 | ``` |
| 3915 | |
| 3916 | ```python |
| 3917 | open_index_dropdown(r: int) -> Sheet |
| 3918 | ``` |
| 3919 | |
| 3920 | ___ |
| 3921 | |
| 3922 | #### **Close a dropdown box** |
| 3923 | |
| 3924 | ```python |
| 3925 | close_dropdown(r: int | None = None, c: int | None = None) -> Sheet |
| 3926 | ``` |
| 3927 | |
| 3928 | ```python |
| 3929 | close_header_dropdown(c: int | None = None) -> Sheet |
| 3930 | ``` |
| 3931 | |
| 3932 | ```python |
| 3933 | close_index_dropdown(r: int | None = None) -> Sheet |
| 3934 | ``` |
| 3935 | Notes: |
| 3936 | - Also destroys any opened text editor windows. |
| 3937 | |
| 3938 | --- |
| 3939 | # **Check Boxes** |
| 3940 | |
| 3941 | #### **Creating check boxes** |
| 3942 | |
| 3943 | `Span` objects (more information [here](#span-objects)) can be used to create check boxes for cells, rows, columns, the entire sheet, headers and the index. |
| 3944 | |
| 3945 | You can use either of the following methods: |
| 3946 | |
| 3947 | - Using a span method e.g. `span.checkbox()` more information [here](#using-a-span-to-create-check-boxes). |
| 3948 | - Using a sheet method e.g. `sheet.checkbox(Span)` |
| 3949 | |
| 3950 | Or if you need user inserted row/columns in the middle of areas with check boxes to also have check boxes you can use named spans, more information [here](#named-spans). |
| 3951 | |
| 3952 | Whether check boxes are created for cells, rows or columns depends on the [`kind`](#get-a-spans-kind) of span. |
| 3953 | |
| 3954 | ```python |
| 3955 | checkbox( |
| 3956 | *key: CreateSpanTypes, |
| 3957 | edit_data: bool = True, |
| 3958 | checked: bool | None = None, |
| 3959 | state: Literal["normal", "disabled"] = "normal", |
| 3960 | redraw: bool = True, |
| 3961 | check_function: Callable | None = None, |
| 3962 | text: str = "", |
| 3963 | ) -> Span |
| 3964 | ``` |
| 3965 | Parameters: |
| 3966 | |
| 3967 | - `key` (`CreateSpanTypes`) either a span or a type which can create a span. See [here](#creating-a-span) for more information on the types that can create a span. |
| 3968 | - `edit_data` when `True` edits the underlying cell data to either `checked` if `checked` is a `bool` or tries to convert the existing cell data to a `bool`. |
| 3969 | - `checked` is the initial creation value to set the box to, if `None` then and `edit_data` is `True` then it will try to convert the underlying cell data to a `bool`. |
| 3970 | - `state` can be `"normal"` or `"disabled"`. If `"disabled"` then color will be same as table grid lines, else it will be the cells text color. |
| 3971 | - `check_function` can be used to trigger a function when the user clicks a checkbox. |
| 3972 | - `text` displays text next to the checkbox in the cell, but will not be used as data, data will either be `True` or `False`. |
| 3973 | |
| 3974 | Notes: |
| 3975 | |
| 3976 | - `check_function` (`Callable`, `None`) requires either `None` or a function. The function you use needs at least one argument because when the checkbox is set it will send information to your function about the clicked checkbox. |
| 3977 | - Use `highlight_cells()` or rows or columns to change the color of the checkbox. |
| 3978 | - Check boxes are always left aligned despite any align settings. |
| 3979 | - To get the current checkbox value either: |
| 3980 | - Get the cell data, more information [here](#getting-sheet-data). |
| 3981 | - Use the parameter `check_function` with a function of your own creation to be called when the checkbox is set by the user. |
| 3982 | |
| 3983 | Example: |
| 3984 | ```python |
| 3985 | self.sheet.checkbox( |
| 3986 | "D", |
| 3987 | checked=True, |
| 3988 | ) |
| 3989 | ``` |
| 3990 | |
| 3991 | ___ |
| 3992 | |
| 3993 | #### **Deleting check boxes** |
| 3994 | |
| 3995 | If the check boxes were created by a named span then the named span must be deleted, more information [here](#deleting-a-named-span). |
| 3996 | |
| 3997 | Otherwise you can use either of the following methods to delete/remove check boxes: |
| 3998 | |
| 3999 | - Using a span method e.g. `span.del_checkbox()` more information [here](#using-a-span-to-delete-check-boxes). |
| 4000 | - Using a sheet method e.g. `sheet.del_checkbox(Span)` details below: |
| 4001 | |
| 4002 | ```python |
| 4003 | del_checkbox( |
| 4004 | *key: CreateSpanTypes, |
| 4005 | redraw: bool = True, |
| 4006 | ) -> Span |
| 4007 | ``` |
| 4008 | Parameters: |
| 4009 | |
| 4010 | - `key` (`CreateSpanTypes`) either a span or a type which can create a span. See [here](#creating-a-span) for more information on the types that can create a span. |
| 4011 | |
| 4012 | Example: |
| 4013 | ```python |
| 4014 | # creating checkboxes in column D |
| 4015 | self.sheet.checkbox( |
| 4016 | "D", |
| 4017 | checked=True, |
| 4018 | ) |
| 4019 | |
| 4020 | # deleting checkboxes in column D |
| 4021 | self.sheet.del_checkbox("D") |
| 4022 | ``` |
| 4023 | |
| 4024 | #### **Set or toggle a check box** |
| 4025 | |
| 4026 | ```python |
| 4027 | click_checkbox( |
| 4028 | *key: CreateSpanTypes, |
| 4029 | checked: bool | None = None, |
| 4030 | redraw: bool = True, |
| 4031 | ) -> Span |
| 4032 | ``` |
| 4033 | |
| 4034 | ```python |
| 4035 | click_header_checkbox(c: int, checked: bool | None = None) -> Sheet |
| 4036 | ``` |
| 4037 | |
| 4038 | ```python |
| 4039 | click_index_checkbox(r: int, checked: bool | None = None) -> Sheet |
| 4040 | ``` |
| 4041 | |
| 4042 | ___ |
| 4043 | |
| 4044 | #### **Get a dictionary of all check box dictionaries** |
| 4045 | |
| 4046 | ```python |
| 4047 | get_checkboxes() -> dict |
| 4048 | ``` |
| 4049 | |
| 4050 | ```python |
| 4051 | get_header_checkboxes() -> dict |
| 4052 | ``` |
| 4053 | |
| 4054 | ```python |
| 4055 | get_index_checkboxes() -> dict |
| 4056 | ``` |
| 4057 | |
| 4058 | --- |
| 4059 | # **Data Formatting** |
| 4060 | |
| 4061 | By default tksheet stores all user inputted data as strings and while tksheet can store and display any datatype with a `__str__()` method this has some obvious limitations. |
| 4062 | |
| 4063 | Data formatting aims to provide greater functionality when working with different datatypes and provide strict typing for the sheet. With formatting you can convert sheet data and user input to a specific datatype. |
| 4064 | |
| 4065 | Additionally, formatting also provides a function for displaying data on the table GUI (as a rounded float for example) and logic for handling invalid and missing data. |
| 4066 | |
| 4067 | tksheet has several basic built-in formatters and provides functionality for creating your own custom formats as well. |
| 4068 | |
| 4069 | A demonstration of all the built-in and custom formatters can be found [here](#example-using-and-creating-formatters). |
| 4070 | |
| 4071 | ### **Creation and deletion of data formatting rules** |
| 4072 | |
| 4073 | #### **Creating a data format rule** |
| 4074 | |
| 4075 | `Span` objects (more information [here](#span-objects)) can be used to format data for cells, rows, columns and the entire sheet. |
| 4076 | |
| 4077 | You can use either of the following methods: |
| 4078 | - Using a span method e.g. `span.format()` more information [here](#using-a-span-to-format-data). |
| 4079 | - Using a sheet method e.g. `sheet.format(Span)` |
| 4080 | |
| 4081 | Or if you need user inserted row/columns in the middle of areas with data formatting to also be formatted you can use named spans, more information [here](#named-spans). |
| 4082 | |
| 4083 | Whether data is formatted for cells, rows or columns depends on the [`kind`](#get-a-spans-kind) of span. |
| 4084 | |
| 4085 | ```python |
| 4086 | format( |
| 4087 | *key: CreateSpanTypes, |
| 4088 | formatter_options: dict = {}, |
| 4089 | formatter_class: object = None, |
| 4090 | redraw: bool = True, |
| 4091 | **kwargs, |
| 4092 | ) -> Span |
| 4093 | ``` |
| 4094 | Notes: |
| 4095 | |
| 4096 | 1. When applying multiple overlapping formats with e.g. a formatted cell which overlaps a formatted row, the priority is as follows: |
| 4097 | - Cell formats first. |
| 4098 | - Row formats second. |
| 4099 | - Column formats third. |
| 4100 | 2. Data formatting will effectively override `validate_input = True` on cells with dropdown boxes. |
| 4101 | 3. When getting data take careful note of the `get_displayed` options, as these are the difference between getting the actual formatted data and what is displayed on the table GUI. |
| 4102 | |
| 4103 | Parameters: |
| 4104 | |
| 4105 | - `key` (`CreateSpanTypes`) either a span or a type which can create a span. See [here](#creating-a-span) for more information on the types that can create a span. |
| 4106 | - `formatter_options` (`dict`) a dictionary of keyword options/arguements to pass to the formatter, see [here](#formatters) for information on what argument to use. |
| 4107 | - `formatter_class` (`class`) in case you want to use a custom class to store functions and information as opposed to using the built-in methods. |
| 4108 | - `**kwargs` any additional keyword options/arguements to pass to the formatter. |
| 4109 | |
| 4110 | ___ |
| 4111 | |
| 4112 | #### **Deleting a data format rule** |
| 4113 | |
| 4114 | If the data format rule was created by a named span then the named span must be deleted, more information [here](#deleting-a-named-span). |
| 4115 | |
| 4116 | Otherwise you can use either of the following methods to delete/remove data formatting rules: |
| 4117 | |
| 4118 | - Using a span method e.g. `span.del_format()` more information [here](#using-a-span-to-delete-check-boxes). |
| 4119 | - Using a sheet method e.g. `sheet.del_format(Span)` details below: |
| 4120 | |
| 4121 | ```python |
| 4122 | del_format( |
| 4123 | *key: CreateSpanTypes, |
| 4124 | clear_values: bool = False, |
| 4125 | redraw: bool = True, |
| 4126 | ) -> Span |
| 4127 | ``` |
| 4128 | - `key` (`CreateSpanTypes`) either a span or a type which can create a span. See [here](#creating-a-span) for more information on the types that can create a span. |
| 4129 | - `clear_values` (`bool`) if true, all the cells covered by the span will have their values cleared. |
| 4130 | |
| 4131 | ___ |
| 4132 | |
| 4133 | #### **Delete all formatting** |
| 4134 | |
| 4135 | ```python |
| 4136 | del_all_formatting(clear_values: bool = False) -> Sheet |
| 4137 | ``` |
| 4138 | - `clear_values` (`bool`) if true, all the sheets cell values will be cleared. |
| 4139 | |
| 4140 | ___ |
| 4141 | |
| 4142 | #### **Reapply formatting to entire sheet** |
| 4143 | |
| 4144 | ```python |
| 4145 | reapply_formatting() -> Sheet |
| 4146 | ``` |
| 4147 | - Useful if you have manually changed the entire sheets data using `sheet.MT.data = ` and want to reformat the sheet using any existing formatting you have set. |
| 4148 | |
| 4149 | ___ |
| 4150 | |
| 4151 | #### **Check if a cell is formatted** |
| 4152 | |
| 4153 | ```python |
| 4154 | formatted(r: int, c: int) -> dict |
| 4155 | ``` |
| 4156 | - If the cell is formatted function returns a `dict` with all the format keyword arguments. The `dict` will be empty if the cell is not formatted. |
| 4157 | |
| 4158 | ___ |
| 4159 | |
| 4160 | ### **Formatters** |
| 4161 | |
| 4162 | In addition to the [generic](#generic-formatter) formatter, `tksheet` provides formatters for many different data types. |
| 4163 | |
| 4164 | A basic example showing how formatting some columns as `float` might be done: |
| 4165 | |
| 4166 | ```python |
| 4167 | import tkinter as tk |
| 4168 | |
| 4169 | from tksheet import ( |
| 4170 | Sheet, |
| 4171 | float_formatter, |
| 4172 | ) |
| 4173 | from tksheet import ( |
| 4174 | num2alpha, |
| 4175 | ) |
| 4176 | |
| 4177 | |
| 4178 | class demo(tk.Tk): |
| 4179 | def __init__(self): |
| 4180 | super().__init__() |
| 4181 | self.grid_columnconfigure(0, weight=1) |
| 4182 | self.grid_rowconfigure(0, weight=1) |
| 4183 | self.sheet = Sheet( |
| 4184 | self, |
| 4185 | data=[[f"{r}", f"{r}"] for r in range(5)], |
| 4186 | expand_sheet_if_paste_too_big=True, |
| 4187 | theme="dark blue", |
| 4188 | ) |
| 4189 | """ |
| 4190 | Format example |
| 4191 | """ |
| 4192 | # some keyword arguments inside float_formatter() |
| 4193 | self.sheet.format( |
| 4194 | num2alpha(0), # column A |
| 4195 | formatter_options=float_formatter( |
| 4196 | decimals=1, |
| 4197 | nullable=True, |
| 4198 | ), |
| 4199 | ) |
| 4200 | # some keyword arguments outside |
| 4201 | # of float_formatter() instead |
| 4202 | self.sheet.format( |
| 4203 | "B", # column B |
| 4204 | formatter_options=float_formatter(), |
| 4205 | decimals=3, |
| 4206 | nullable=False, |
| 4207 | ) |
| 4208 | """ |
| 4209 | Rest of code |
| 4210 | """ |
| 4211 | self.sheet.grid(row=0, column=0, sticky="nswe") |
| 4212 | self.sheet.enable_bindings( |
| 4213 | "all", |
| 4214 | "ctrl_select", |
| 4215 | "edit_header", |
| 4216 | "edit_index", |
| 4217 | ) |
| 4218 | |
| 4219 | |
| 4220 | app = demo() |
| 4221 | app.mainloop() |
| 4222 | ``` |
| 4223 | |
| 4224 | **You can use any of the following formatters as an argument for the parameter `formatter_options`.** |
| 4225 | |
| 4226 | A full list of keyword arguments available to these formatters is shown [here](#generic-formatter). |
| 4227 | |
| 4228 | ___ |
| 4229 | |
| 4230 | #### **Int Formatter** |
| 4231 | |
| 4232 | The `int_formatter` is the basic configuration for a simple interger formatter. |
| 4233 | |
| 4234 | ```python |
| 4235 | int_formatter( |
| 4236 | datatypes: tuple[object] | object = int, |
| 4237 | format_function: Callable = to_int, |
| 4238 | to_str_function: Callable = to_str, |
| 4239 | invalid_value: object = "NaN", |
| 4240 | **kwargs, |
| 4241 | ) -> dict |
| 4242 | ``` |
| 4243 | Parameters: |
| 4244 | |
| 4245 | - `format_function` (`function`) a function that takes a string and returns an `int`. By default, this is set to the in-built `tksheet.to_int`. This function will always convert float-likes to its floor, for example `"5.9"` will be converted to `5`. |
| 4246 | - `to_str_function` (`function`) By default, this is set to the in-built `tksheet.to_str`, which is a very basic function that will displace the default string representation of the value. |
| 4247 | |
| 4248 | Example: |
| 4249 | ```python |
| 4250 | sheet.format_cell(0, 0, formatter_options = tksheet.int_formatter()) |
| 4251 | ``` |
| 4252 | |
| 4253 | ___ |
| 4254 | |
| 4255 | #### **Float Formatter** |
| 4256 | |
| 4257 | The `float_formatter` is the basic configuration for a simple float formatter. It will always round float-likes to the specified number of decimal places, for example `"5.999"` will be converted to `"6.0"` if `decimals = 1`. |
| 4258 | |
| 4259 | ```python |
| 4260 | float_formatter( |
| 4261 | datatypes: tuple[object] | object = float, |
| 4262 | format_function: Callable = to_float, |
| 4263 | to_str_function: Callable = float_to_str, |
| 4264 | invalid_value: object = "NaN", |
| 4265 | decimals: int = 2, |
| 4266 | **kwargs |
| 4267 | ) -> dict |
| 4268 | ``` |
| 4269 | Parameters: |
| 4270 | |
| 4271 | - `format_function` (`function`) a function that takes a string and returns a `float`. By default, this is set to the in-built `tksheet.to_float`. |
| 4272 | - `to_str_function` (`function`) By default, this is set to the in-built `tksheet.float_to_str`, which will display the float to the specified number of decimal places. |
| 4273 | - `decimals` (`int`, `None`) the number of decimal places to round to. Defaults to `2`. |
| 4274 | |
| 4275 | Example: |
| 4276 | ```python |
| 4277 | sheet.format_cell(0, 0, formatter_options = tksheet.float_formatter(decimals = None)) # A float formatter with maximum float() decimal places |
| 4278 | ``` |
| 4279 | |
| 4280 | ___ |
| 4281 | |
| 4282 | #### **Percentage Formatter** |
| 4283 | |
| 4284 | The `percentage_formatter` is the basic configuration for a simple percentage formatter. It will always round float-likes as a percentage to the specified number of decimal places, for example `"5.999%"` will be converted to `"6.0%"` if `decimals = 1`. |
| 4285 | |
| 4286 | ```python |
| 4287 | percentage_formatter( |
| 4288 | datatypes: tuple[object] | object = float, |
| 4289 | format_function: Callable = to_percentage, |
| 4290 | to_str_function: Callable = percentage_to_str, |
| 4291 | invalid_value: object = "NaN", |
| 4292 | decimals: int = 0, |
| 4293 | **kwargs, |
| 4294 | ) -> dict |
| 4295 | ``` |
| 4296 | Parameters: |
| 4297 | |
| 4298 | - `format_function` (`function`) a function that takes a string and returns a `float`. By default, this is set to the in-built `tksheet.to_percentage`. This function will always convert percentages to their decimal equivalent, for example `"5%"` will be converted to `0.05`. |
| 4299 | - `to_str_function` (`function`) By default, this is set to the in-built `tksheet.percentage_to_str`, which will display the float as a percentage to the specified number of decimal places. For example, `0.05` will be displayed as `"5.0%"`. |
| 4300 | - `decimals` (`int`) the number of decimal places to round to. Defaults to `0`. |
| 4301 | |
| 4302 | Example: |
| 4303 | ```python |
| 4304 | sheet.format_cell(0, 0, formatter_options=tksheet.percentage_formatter(decimals=1)) # A percentage formatter with 1 decimal place |
| 4305 | ``` |
| 4306 | |
| 4307 | **Note:** |
| 4308 | |
| 4309 | By default `percentage_formatter()` converts user entry `21` to `2100%` and `21%` to `21%`. An example where `21` is converted to `21%` instead is shown below: |
| 4310 | |
| 4311 | ```python |
| 4312 | # formats column A as percentage |
| 4313 | |
| 4314 | # uses: |
| 4315 | # format_function=alt_to_percentage |
| 4316 | # to_str_function=alt_percentage_to_str |
| 4317 | sheet.format( |
| 4318 | "A", |
| 4319 | formatter_options=tksheet.percentage_formatter( |
| 4320 | format_function=alt_to_percentage, |
| 4321 | to_str_function=alt_percentage_to_str, |
| 4322 | ) |
| 4323 | ) |
| 4324 | ``` |
| 4325 | |
| 4326 | ___ |
| 4327 | |
| 4328 | #### **Bool Formatter** |
| 4329 | |
| 4330 | ```python |
| 4331 | bool_formatter( |
| 4332 | datatypes: tuple[object] | object = bool, |
| 4333 | format_function: Callable = to_bool, |
| 4334 | to_str_function: Callable = bool_to_str, |
| 4335 | invalid_value: object = "NA", |
| 4336 | truthy: set = truthy, |
| 4337 | falsy: set = falsy, |
| 4338 | **kwargs, |
| 4339 | ) -> dict |
| 4340 | ``` |
| 4341 | Parameters: |
| 4342 | |
| 4343 | - `format_function` (`function`) a function that takes a string and returns a `bool`. By default, this is set to the in-built `tksheet.to_bool`. |
| 4344 | - `to_str_function` (`function`) By default, this is set to the in-built `tksheet.bool_to_str`, which will display the boolean as `"True"` or `"False"`. |
| 4345 | - `truthy` (`set`) a set of values that will be converted to `True`. Defaults to the in-built `tksheet.truthy`. |
| 4346 | - `falsy` (`set`) a set of values that will be converted to `False`. Defaults to the in-built `tksheet.falsy`. |
| 4347 | |
| 4348 | Example: |
| 4349 | ```python |
| 4350 | # A bool formatter with custom truthy and falsy values to account for aussie and kiwi slang |
| 4351 | sheet.format_cell(0, 0, formatter_options = tksheet.bool_formatter(truthy = tksheet.truthy | {"nah yeah"}, falsy = tksheet.falsy | {"yeah nah"})) |
| 4352 | ``` |
| 4353 | |
| 4354 | ___ |
| 4355 | |
| 4356 | #### **Generic Formatter** |
| 4357 | |
| 4358 | ```python |
| 4359 | formatter( |
| 4360 | datatypes: tuple[object] | object, |
| 4361 | format_function: Callable, |
| 4362 | to_str_function: Callable = to_str, |
| 4363 | invalid_value: object = "NaN", |
| 4364 | nullable: bool = True, |
| 4365 | pre_format_function: Callable | None = None, |
| 4366 | post_format_function: Callable | None = None, |
| 4367 | clipboard_function: Callable | None = None, |
| 4368 | **kwargs, |
| 4369 | ) -> dict |
| 4370 | ``` |
| 4371 | |
| 4372 | This is the generic formatter options interface. You can use this to create your own custom formatters. The following options are available. |
| 4373 | |
| 4374 | Note that all these options can also be passed to the `Sheet()` format functions as keyword arguments and are available as attributes for all formatters. |
| 4375 | |
| 4376 | You can also provide functions of your own creation for all the below arguments which take functions if you require. |
| 4377 | |
| 4378 | - `datatypes` (`list`) a list of datatypes that the formatter will accept. For example, `datatypes = [int, float]` will accept integers and floats. |
| 4379 | - `format_function` (`function`) a function that takes a string and returns a value of the desired datatype. For example, `format_function = int` will convert a string to an integer. |
| 4380 | - `to_str_function` (`function`) a function that takes a value of the desired datatype and returns a string. This determines how the formatter displays its data on the table. For example, `to_str_function = str` will convert an integer to a string. Defaults to `tksheet.to_str`. |
| 4381 | - `invalid_value` (`any`) the value to return if the input string is invalid. For example, `invalid_value = "NA"` will return "NA" if the input string is invalid. |
| 4382 | - `nullable` (`bool`) if true, the formatter will accept `None` as a valid input. |
| 4383 | - `pre_format_function` (`function`) a function that takes a input string and returns a string. This function is called before the `format_function` and can be used to modify the input string before it is converted to the desired datatype. This can be useful if you want to strip out unwanted characters or convert a string to a different format before converting it to the desired datatype. |
| 4384 | - `post_format_function` (`function`) a function that takes a value **which might not be of the desired datatype, e.g. `None` if the cell is nullable and empty** and if successful returns a value of the desired datatype or if not successful returns the input value. This function is called after the `format_function` and can be used to modify the output value after it is converted to the desired datatype. This can be useful if you want to round a float for example. |
| 4385 | - `clipboard_function` (`function`) a function that takes a value of the desired datatype and returns a string. This function is called when the cell value is copied to the clipboard. This can be useful if you want to convert a value to a different format before it is copied to the clipboard. |
| 4386 | - `**kwargs` any additional keyword options/arguements to pass to the formatter. These keyword arguments will be passed to the `format_function`, `to_str_function`, and the `clipboard_function`. These can be useful if you want to specifiy any additional formatting options, such as the number of decimal places to round to. |
| 4387 | |
| 4388 | ___ |
| 4389 | |
| 4390 | ### **Datetime Formatters and Designing Your Own Custom Formatters** |
| 4391 | |
| 4392 | tksheet is at the moment a dependency free library and so doesn't include a datetime parser as is. |
| 4393 | |
| 4394 | You can however very easily make a datetime parser if you are willing to install a third-party package. Recommended are: |
| 4395 | |
| 4396 | - [dateparser](https://dateparser.readthedocs.io/en/latest/) |
| 4397 | - [dateutil](https://dateutil.readthedocs.io/en/stable/) |
| 4398 | |
| 4399 | Both of these packages have a very comprehensive datetime parser which can be used to create a custom datetime formatter for tksheet. |
| 4400 | |
| 4401 | Below is a simple example of how you might create a custom datetime formatter using the `dateutil` package. |
| 4402 | |
| 4403 | ```python |
| 4404 | from tksheet import * |
| 4405 | from datetime import datetime, date |
| 4406 | from dateutil.parser import parse |
| 4407 | |
| 4408 | def to_local_datetime(dt, **kwargs): |
| 4409 | ''' |
| 4410 | Our custom format_function, converts a string or a date to a datetime object in the local timezone. |
| 4411 | ''' |
| 4412 | if isinstance(dt, datetime): |
| 4413 | pass # Do nothing |
| 4414 | elif isinstance(dt, date): |
| 4415 | dt = datetime(dt.year, dt.month, dt.day) # Always good to account for unexpected inputs |
| 4416 | else: |
| 4417 | try: |
| 4418 | dt = parser.parse(dt) |
| 4419 | except: |
| 4420 | raise ValueError(f"Could not parse {dt} as a datetime") |
| 4421 | if dt.tzinfo is None: |
| 4422 | dt = dt.replace(tzinfo = tzlocal()) # If no timezone is specified, assume local timezone |
| 4423 | dt = dt.astimezone(tzlocal()) # Convert to local timezone |
| 4424 | return dt |
| 4425 | |
| 4426 | def datetime_to_str(dt, **kwargs): |
| 4427 | ''' |
| 4428 | Our custom to_str_function, converts a datetime object to a string with a format that can be specfied in kwargs. |
| 4429 | ''' |
| 4430 | return dt.strftime(kwargs['format']) |
| 4431 | |
| 4432 | # Now we can create our custom formatter dictionary from the generic formatter interface in tksheet |
| 4433 | datetime_formatter = formatter(datatypes = datetime, |
| 4434 | format_function = to_local_datetime, |
| 4435 | to_str_function = datetime_to_str, |
| 4436 | invalid_value = "NaT", |
| 4437 | format = "%d/%m/%Y %H:%M:%S", |
| 4438 | ) |
| 4439 | |
| 4440 | # From here we can pass our datetime_formatter into sheet.format() or span.format() just like any other formatter |
| 4441 | ``` |
| 4442 | |
| 4443 | For those wanting even more customisation of their formatters you also have the option of creating a custom formatter class. This is a more advanced topic and is not covered here, but it's recommended to create a new class which is a subclass of the `tksheet.Formatter` class and overriding the methods you would like to customise. This custom class can then be passed into the `format_cells()` `formatter_class` argument. |
| 4444 | |
| 4445 | --- |
| 4446 | # **Readonly Cells** |
| 4447 | |
| 4448 | #### **Creating a readonly rule** |
| 4449 | |
| 4450 | `Span` objects (more information [here](#span-objects)) can be used to create readonly rules for cells, rows, columns, the entire sheet, headers and the index. |
| 4451 | |
| 4452 | You can use either of the following methods: |
| 4453 | |
| 4454 | - Using a span method e.g. `span.readonly()` more information [here](#using-a-span-to-set-cells-to-read-only). |
| 4455 | - Using a sheet method e.g. `sheet.readonly(Span)` |
| 4456 | |
| 4457 | Or if you need user inserted row/columns in the middle of areas with a readonly rule to also have a readonly rule you can use named spans, more information [here](#named-spans). |
| 4458 | |
| 4459 | Whether cells, rows or columns are readonly depends on the [`kind`](#get-a-spans-kind) of span. |
| 4460 | |
| 4461 | ```python |
| 4462 | readonly( |
| 4463 | *key: CreateSpanTypes, |
| 4464 | readonly: bool = True, |
| 4465 | ) -> Span |
| 4466 | ``` |
| 4467 | Parameters: |
| 4468 | |
| 4469 | - `key` (`CreateSpanTypes`) either a span or a type which can create a span. See [here](#creating-a-span) for more information on the types that can create a span. |
| 4470 | - `readonly` (`bool`) `True` to create a rule and `False` to delete one created without the use of named spans. |
| 4471 | |
| 4472 | ___ |
| 4473 | |
| 4474 | #### **Deleting a readonly rule** |
| 4475 | |
| 4476 | If the readonly rule was created by a named span then the named span must be deleted, more information [here](#deleting-a-named-span). |
| 4477 | |
| 4478 | Otherwise you can use either of the following methods to delete/remove readonly rules: |
| 4479 | |
| 4480 | - Using a span method e.g. `span.readonly()` with the keyword argument `readonly=False` more information [here](#using-a-span-to-set-cells-to-read-only). |
| 4481 | - Using a sheet method e.g. `sheet.readonly(Span)` with the keyword argument `readonly=False` example below: |
| 4482 | |
| 4483 | ```python |
| 4484 | # creating a readonly rule |
| 4485 | self.sheet.readonly( |
| 4486 | self.sheet.span("A", header=True), |
| 4487 | readonly=True, |
| 4488 | ) |
| 4489 | |
| 4490 | # deleting the readonly rule |
| 4491 | self.sheet.readonly( |
| 4492 | self.sheet.span("A", header=True), |
| 4493 | readonly=False, |
| 4494 | ) |
| 4495 | ``` |
| 4496 | Parameters: |
| 4497 | |
| 4498 | - `key` (`CreateSpanTypes`) either a span or a type which can create a span. See [here](#creating-a-span) for more information on the types that can create a span. |
| 4499 | - `readonly` (`bool`) `True` to create a rule and `False` to delete one created without the use of named spans. |
| 4500 | |
| 4501 | --- |
| 4502 | # **Text Font and Alignment** |
| 4503 | |
| 4504 | ### **Font** |
| 4505 | |
| 4506 | - Font arguments require a three tuple e.g. `("Arial", 12, "normal")` or `("Arial", 12, "bold")` or `("Arial", 12, "italic")` |
| 4507 | - The table and index currently share a font, it's not possible to change the index font separate from the table font. |
| 4508 | |
| 4509 | **Set the table and index font** |
| 4510 | |
| 4511 | ```python |
| 4512 | font( |
| 4513 | newfont: tuple[str, int, str] | None = None, |
| 4514 | reset_row_positions: bool = True, |
| 4515 | ) -> tuple[str, int, str] |
| 4516 | ``` |
| 4517 | |
| 4518 | **Set the header font** |
| 4519 | |
| 4520 | ```python |
| 4521 | header_font(newfont: tuple[str, int, str] | None = None) -> tuple[str, int, str] |
| 4522 | ``` |
| 4523 | |
| 4524 | ### **Text Alignment** |
| 4525 | |
| 4526 | There are functions to set the text alignment for specific cells/rows/columns and also functions to set the text alignment for a whole part of the sheet (table/index/header). |
| 4527 | |
| 4528 | - Alignment argument (`str`) options are: |
| 4529 | - `"w"`, `"west"`, `"left"` |
| 4530 | - `"e"`, `"east"`, `"right"` |
| 4531 | - `"c"`, `"center"`, `"centre"` |
| 4532 | |
| 4533 | Unfortunately vertical alignment is not available. |
| 4534 | |
| 4535 | #### **Whole widget text alignment** |
| 4536 | |
| 4537 | Set the text alignment for the whole of the table (doesn't include index/header). |
| 4538 | ```python |
| 4539 | table_align( |
| 4540 | align: str = None, |
| 4541 | redraw: bool = True, |
| 4542 | ) -> str | Sheet |
| 4543 | ``` |
| 4544 | |
| 4545 | Set the text alignment for the whole of the header. |
| 4546 | ```python |
| 4547 | header_align( |
| 4548 | align: str = None, |
| 4549 | redraw: bool = True, |
| 4550 | ) -> str | Sheet |
| 4551 | ``` |
| 4552 | |
| 4553 | Set the text alignment for the whole of the index. |
| 4554 | ```python |
| 4555 | row_index_align( |
| 4556 | align: str = None, |
| 4557 | redraw: bool = True, |
| 4558 | ) -> str | Sheet |
| 4559 | |
| 4560 | # can also use index_align() which behaves the same |
| 4561 | ``` |
| 4562 | |
| 4563 | #### **Creating a specific cell row or column text alignment rule** |
| 4564 | |
| 4565 | The following function is for setting text alignment for specific cells, rows or columns in the table, header and index. |
| 4566 | |
| 4567 | `Span` objects (more information [here](#span-objects)) can be used to create text alignment rules for cells, rows, columns, the entire sheet, headers and the index. |
| 4568 | |
| 4569 | You can use either of the following methods: |
| 4570 | |
| 4571 | - Using a span method e.g. `span.align()` more information [here](#using-a-span-to-create-text-alignment-rules). |
| 4572 | - Using a sheet method e.g. `sheet.align(Span)` |
| 4573 | |
| 4574 | Or if you need user inserted row/columns in the middle of areas with an alignment rule to also have an alignment rule you can use named spans, more information [here](#named-spans). |
| 4575 | |
| 4576 | Whether cells, rows or columns are affected depends on the [`kind`](#get-a-spans-kind) of span. |
| 4577 | |
| 4578 | ```python |
| 4579 | align( |
| 4580 | *key: CreateSpanTypes, |
| 4581 | align: str | None = None, |
| 4582 | redraw: bool = True, |
| 4583 | ) -> Span |
| 4584 | ``` |
| 4585 | Parameters: |
| 4586 | |
| 4587 | - `key` (`CreateSpanTypes`) either a span or a type which can create a span. See [here](#creating-a-span) for more information on the types that can create a span. |
| 4588 | - `align` (`str`, `None`) must be one of the following: |
| 4589 | - `"w"`, `"west"`, `"left"` |
| 4590 | - `"e"`, `"east"`, `"right"` |
| 4591 | - `"c"`, `"center"`, `"centre"` |
| 4592 | |
| 4593 | #### **Deleting a specific text alignment rule** |
| 4594 | |
| 4595 | If the text alignment rule was created by a named span then the named span must be deleted, more information [here](#deleting-a-named-span). |
| 4596 | |
| 4597 | Otherwise you can use either of the following methods to delete/remove specific text alignment rules: |
| 4598 | |
| 4599 | - Using a span method e.g. `span.del_align()` more information [here](#using-a-span-to-delete-text-alignment-rules). |
| 4600 | - Using a sheet method e.g. `sheet.del_align(Span)` details below: |
| 4601 | |
| 4602 | ```python |
| 4603 | del_align( |
| 4604 | *key: CreateSpanTypes, |
| 4605 | redraw: bool = True, |
| 4606 | ) -> Span |
| 4607 | ``` |
| 4608 | Parameters: |
| 4609 | |
| 4610 | - `key` (`CreateSpanTypes`) either a span or a type which can create a span. See [here](#creating-a-span) for more information on the types that can create a span. |
| 4611 | |
| 4612 | #### **Get existing specific text alignments** |
| 4613 | |
| 4614 | Cell text alignments: |
| 4615 | ```python |
| 4616 | get_cell_alignments() -> dict |
| 4617 | ``` |
| 4618 | |
| 4619 | Row text alignments: |
| 4620 | ```python |
| 4621 | get_row_alignments() -> dict |
| 4622 | ``` |
| 4623 | |
| 4624 | Column text alignments: |
| 4625 | ```python |
| 4626 | get_column_alignments() -> dict |
| 4627 | ``` |
| 4628 | |
| 4629 | --- |
| 4630 | # **Getting Cell Properties** |
| 4631 | |
| 4632 | The below functions can be used to retrieve cell options/properties such as highlights, format, readonly etc. |
| 4633 | |
| 4634 | #### **Get table cell properties** |
| 4635 | |
| 4636 | Retrieve options for a single cell in the main table. Also retrieves any row/column options impacting that cell. |
| 4637 | |
| 4638 | ```Python |
| 4639 | props( |
| 4640 | row: int, |
| 4641 | column: int | str, |
| 4642 | key: None |
| 4643 | | Literal[ |
| 4644 | "format", |
| 4645 | "highlight", |
| 4646 | "dropdown", |
| 4647 | "checkbox", |
| 4648 | "readonly", |
| 4649 | "align", |
| 4650 | ] = None, |
| 4651 | cellops: bool = True, |
| 4652 | rowops: bool = True, |
| 4653 | columnops: bool = True, |
| 4654 | ) -> dict |
| 4655 | ``` |
| 4656 | Parameters: |
| 4657 | |
| 4658 | - `row` only `int`. |
| 4659 | - `column` `int` or `str` e.g. `"A"` is index `0`. |
| 4660 | - `key`: |
| 4661 | - If left as `None` then all existing properties for that cell will be returned in a `dict`. |
| 4662 | - If using a `str` e.g. `"highlight"` it will only look for highlight properties for that cell. |
| 4663 | - `cellops` when `True` will look for cell options for the cell. |
| 4664 | - `rowops` when `True` will look for row options for the cell. |
| 4665 | - `columnops` when `True` will look for column options for the cell. |
| 4666 | |
| 4667 | Example: |
| 4668 | |
| 4669 | ```python |
| 4670 | # making column B, including header read only |
| 4671 | sheet.readonly(sheet["B"].options(header=True)) |
| 4672 | |
| 4673 | # checking if row 0, column 1 (B) is readonly: |
| 4674 | cell_is_readonly = sheet.props(0, 1, "readonly") |
| 4675 | |
| 4676 | # can also use a string for the column: |
| 4677 | cell_is_readonly = sheet.props(0, "b", "readonly") |
| 4678 | ``` |
| 4679 | |
| 4680 | ___ |
| 4681 | |
| 4682 | #### **Get index cell properties** |
| 4683 | |
| 4684 | Retrieve options for a single cell in the index. |
| 4685 | |
| 4686 | ```python |
| 4687 | index_props( |
| 4688 | row: int, |
| 4689 | key: None |
| 4690 | | Literal[ |
| 4691 | "format", |
| 4692 | "highlight", |
| 4693 | "dropdown", |
| 4694 | "checkbox", |
| 4695 | "readonly", |
| 4696 | "align", |
| 4697 | ] = None, |
| 4698 | ) -> dict |
| 4699 | ``` |
| 4700 | Parameters: |
| 4701 | |
| 4702 | - `row` only `int`. |
| 4703 | - `key`: |
| 4704 | - If left as `None` then all existing properties for that cell will be returned in a `dict`. |
| 4705 | - If using a `str` e.g. `"highlight"` it will only look for highlight properties for that cell. |
| 4706 | |
| 4707 | ___ |
| 4708 | |
| 4709 | #### **Get header cell properties** |
| 4710 | |
| 4711 | Retrieve options for a single cell in the header. |
| 4712 | |
| 4713 | ```python |
| 4714 | header_props( |
| 4715 | column: int | str, |
| 4716 | key: None |
| 4717 | | Literal[ |
| 4718 | "format", |
| 4719 | "highlight", |
| 4720 | "dropdown", |
| 4721 | "checkbox", |
| 4722 | "readonly", |
| 4723 | "align", |
| 4724 | ] = None, |
| 4725 | ) -> dict |
| 4726 | ``` |
| 4727 | Parameters: |
| 4728 | |
| 4729 | - `column` only `int`. |
| 4730 | - `key`: |
| 4731 | - If left as `None` then all existing properties for that cell will be returned in a `dict`. |
| 4732 | - If using a `str` e.g. `"highlight"` it will only look for highlight properties for that cell. |
| 4733 | |
| 4734 | --- |
| 4735 | # **Getting Selected Cells** |
| 4736 | |
| 4737 | All selected cell/box getting functions return or generate **displayed** cell coordinates. |
| 4738 | |
| 4739 | - Displayed cell coordinates ignore hidden rows/columns when indexing cells. |
| 4740 | - Data cell coordinates include hidden rows/columns in indexing cells. |
| 4741 | |
| 4742 | #### **Get the currently selected cell** |
| 4743 | |
| 4744 | This is always a single cell of displayed indices. If you have hidden rows or columns you can change the integers to data indices using the following functions: |
| 4745 | |
| 4746 | - [Change a row](#displayed-row-index-to-data) |
| 4747 | - [Change a column](#displayed-column-index-to-data) |
| 4748 | |
| 4749 | ```python |
| 4750 | get_currently_selected() -> tuple | Selected |
| 4751 | ``` |
| 4752 | Notes: |
| 4753 | |
| 4754 | - Returns either: |
| 4755 | - `namedtuple` of `(row, column, type_, box, iid, fill_iid)`. |
| 4756 | - `type_` can be `"rows"`, `"columns"` or `"cells"`. |
| 4757 | - `box` `tuple[int, int, int, int]` are the coordinates of the box that the currently selected box is attached to. |
| 4758 | - `(from row, from column, up to but not including row, up to but not including column)`. |
| 4759 | - `iid` is the canvas item id of the currently selected box. |
| 4760 | - `fill_iid` is the canvas item id of the box that the currently selected box is attached to. |
| 4761 | - An empty `tuple` if nothing is selected. |
| 4762 | - Can also use `sheet.selected` as shorter `@property` version of the function. |
| 4763 | |
| 4764 | Example: |
| 4765 | ```python |
| 4766 | currently_selected = self.sheet.get_currently_selected() |
| 4767 | if currently_selected: |
| 4768 | row = currently_selected.row |
| 4769 | column = currently_selected.column |
| 4770 | type_ = currently_selected.type_ |
| 4771 | |
| 4772 | if self.sheet.selected: |
| 4773 | ... |
| 4774 | ``` |
| 4775 | |
| 4776 | ___ |
| 4777 | |
| 4778 | #### **Get selected rows** |
| 4779 | |
| 4780 | ```python |
| 4781 | get_selected_rows( |
| 4782 | get_cells: bool = False, |
| 4783 | get_cells_as_rows: bool = False, |
| 4784 | return_tuple: bool = False, |
| 4785 | ) -> tuple[int] | tuple[tuple[int, int]] | set[int] | set[tuple[int, int]] |
| 4786 | ``` |
| 4787 | - Returns displayed indexes. |
| 4788 | |
| 4789 | ___ |
| 4790 | |
| 4791 | #### **Get selected columns** |
| 4792 | |
| 4793 | ```python |
| 4794 | get_selected_columns( |
| 4795 | get_cells: bool = False, |
| 4796 | get_cells_as_columns: bool = False, |
| 4797 | return_tuple: bool = False, |
| 4798 | ) -> tuple[int] | tuple[tuple[int, int]] | set[int] | set[tuple[int, int]] |
| 4799 | ``` |
| 4800 | - Returns displayed indexes. |
| 4801 | |
| 4802 | ___ |
| 4803 | |
| 4804 | #### **Get selected cells** |
| 4805 | |
| 4806 | ```python |
| 4807 | get_selected_cells( |
| 4808 | get_rows: bool = False, |
| 4809 | get_columns: bool = False, |
| 4810 | sort_by_row: bool = False, |
| 4811 | sort_by_column: bool = False, |
| 4812 | reverse: bool = False, |
| 4813 | ) -> list[tuple[int, int]] | set[tuple[int, int]] |
| 4814 | ``` |
| 4815 | - Returns displayed coordinates. |
| 4816 | |
| 4817 | ___ |
| 4818 | |
| 4819 | #### **Generate selected cells** |
| 4820 | |
| 4821 | ```python |
| 4822 | gen_selected_cells( |
| 4823 | get_rows: bool = False, |
| 4824 | get_columns: bool = False, |
| 4825 | ) -> Generator[tuple[int, int]] |
| 4826 | ``` |
| 4827 | - Generates displayed coordinates. |
| 4828 | |
| 4829 | ___ |
| 4830 | |
| 4831 | #### **Get all selection boxes** |
| 4832 | |
| 4833 | ```python |
| 4834 | get_all_selection_boxes() -> tuple[tuple[int, int, int, int]] |
| 4835 | ``` |
| 4836 | - Returns displayed coordinates. |
| 4837 | |
| 4838 | ___ |
| 4839 | |
| 4840 | #### **Get all selection boxes and their types** |
| 4841 | |
| 4842 | ```python |
| 4843 | get_all_selection_boxes_with_types() -> list[tuple[tuple[int, int, int, int], str]] |
| 4844 | ``` |
| 4845 | |
| 4846 | Equivalent to `get_all_selection_boxes_with_types()` but shortened as `@property`. |
| 4847 | |
| 4848 | ```python |
| 4849 | @property |
| 4850 | boxes() -> list[tuple[tuple[int, int, int, int], str]] |
| 4851 | ``` |
| 4852 | |
| 4853 | ___ |
| 4854 | |
| 4855 | #### **Check if cell is selected** |
| 4856 | |
| 4857 | ```python |
| 4858 | cell_selected( |
| 4859 | r: int, |
| 4860 | c: int, |
| 4861 | rows: bool = False, |
| 4862 | columns: bool = False, |
| 4863 | ) -> bool |
| 4864 | ``` |
| 4865 | - `rows` if `True` also checks if provided cell is part of a selected row. |
| 4866 | - `columns` if `True` also checks if provided cell is part of a selected column. |
| 4867 | |
| 4868 | ___ |
| 4869 | |
| 4870 | #### **Check if row is selected** |
| 4871 | |
| 4872 | ```python |
| 4873 | row_selected(r: int, cells: bool = False) -> bool |
| 4874 | ``` |
| 4875 | - `cells` if `True` also checks if provided row is selected as part of a cell selection box. |
| 4876 | |
| 4877 | ___ |
| 4878 | |
| 4879 | #### **Check if column is selected** |
| 4880 | |
| 4881 | ```python |
| 4882 | column_selected(c: int, cells: bool = False) -> bool |
| 4883 | ``` |
| 4884 | - `cells` if `True` also checks if provided column is selected as part of a cell selection box. |
| 4885 | |
| 4886 | ___ |
| 4887 | |
| 4888 | #### **Check if any cells, rows or columns are selected, there are options for exclusions** |
| 4889 | |
| 4890 | ```python |
| 4891 | anything_selected( |
| 4892 | exclude_columns: bool = False, |
| 4893 | exclude_rows: bool = False, |
| 4894 | exclude_cells: bool = False, |
| 4895 | ) -> bool |
| 4896 | ``` |
| 4897 | |
| 4898 | ___ |
| 4899 | |
| 4900 | #### **Check if user has the entire table selected** |
| 4901 | |
| 4902 | ```python |
| 4903 | all_selected() -> bool |
| 4904 | ``` |
| 4905 | |
| 4906 | ___ |
| 4907 | |
| 4908 | ```python |
| 4909 | get_ctrl_x_c_boxes(nrows: bool = True) -> tuple[dict[tuple[int, int, int, int], str], int] |
| 4910 | ``` |
| 4911 | |
| 4912 | ___ |
| 4913 | |
| 4914 | ```python |
| 4915 | @property |
| 4916 | ctrl_boxes() -> dict[tuple[int, int, int, int], str] |
| 4917 | ``` |
| 4918 | |
| 4919 | ___ |
| 4920 | |
| 4921 | ```python |
| 4922 | get_selected_min_max() -> tuple[int, int, int, int] | tuple[None, None, None, None] |
| 4923 | ``` |
| 4924 | - returns `(min_y, min_x, max_y, max_x)` of any selections including rows/columns. |
| 4925 | |
| 4926 | --- |
| 4927 | # **Modifying Selected Cells** |
| 4928 | |
| 4929 | All selected cell/box setting functions use **displayed** cell coordinates. |
| 4930 | |
| 4931 | - Displayed cell coordinates ignore hidden rows/columns when indexing cells. |
| 4932 | - Data cell coordinates include hidden rows/columns in indexing cells. |
| 4933 | |
| 4934 | ```python |
| 4935 | set_currently_selected(row: int | None = None, column: int | None = None) -> Sheet |
| 4936 | ``` |
| 4937 | |
| 4938 | ___ |
| 4939 | |
| 4940 | ```python |
| 4941 | select_row(row: int, redraw: bool = True, run_binding_func: bool = True) -> Sheet |
| 4942 | ``` |
| 4943 | - `run_binding_func` is only relevant if you have `extra_bindings()` with `"row_select"` bound. |
| 4944 | |
| 4945 | ___ |
| 4946 | |
| 4947 | ```python |
| 4948 | select_column(column: int, redraw: bool = True, run_binding_func: bool = True) -> Sheet |
| 4949 | ``` |
| 4950 | - `run_binding_func` is only relevant if you have `extra_bindings()` with `"column_select"` bound. |
| 4951 | |
| 4952 | ___ |
| 4953 | |
| 4954 | ```python |
| 4955 | select_cell(row: int, column: int, redraw: bool = True, run_binding_func: bool = True) -> Sheet |
| 4956 | ``` |
| 4957 | - `run_binding_func` is only relevant if you have `extra_bindings()` with `"cell_select"` bound. |
| 4958 | |
| 4959 | ___ |
| 4960 | |
| 4961 | ```python |
| 4962 | select_all(redraw: bool = True, run_binding_func: bool = True) -> Sheet |
| 4963 | ``` |
| 4964 | - `run_binding_func` is only relevant if you have `extra_bindings()` with `"select_all"` bound. |
| 4965 | |
| 4966 | ___ |
| 4967 | |
| 4968 | Deselect a specific cell, row or column. |
| 4969 | |
| 4970 | ```python |
| 4971 | deselect( |
| 4972 | row: int | None | Literal["all"] = None, |
| 4973 | column: int | None = None, |
| 4974 | cell: tuple | None = None, |
| 4975 | redraw: bool = True, |
| 4976 | ) -> Sheet |
| 4977 | ``` |
| 4978 | - Leave parameters as `None`, or `row` as `"all"` to deselect everything. |
| 4979 | - Can also close text editors and open dropdowns. |
| 4980 | |
| 4981 | ___ |
| 4982 | |
| 4983 | Deselect any cell, row or column selection box conflicting with `rows` and/or `columns`. |
| 4984 | |
| 4985 | ```python |
| 4986 | deselect_any( |
| 4987 | rows: Iterator[int] | int | None, |
| 4988 | columns: Iterator[int] | int | None, |
| 4989 | redraw: bool = True, |
| 4990 | ) -> Sheet |
| 4991 | ``` |
| 4992 | - Leave parameters as `None` to deselect everything. |
| 4993 | - Can also close text editors and open dropdowns. |
| 4994 | |
| 4995 | ___ |
| 4996 | |
| 4997 | ```python |
| 4998 | add_cell_selection( |
| 4999 | row: int, |
| 5000 | column: int, |
| 5001 | redraw: bool = True, |
| 5002 | run_binding_func: bool = True, |
| 5003 | set_as_current: bool = True, |
| 5004 | ) -> Sheet |
| 5005 | ``` |
| 5006 | - `run_binding_func` is only relevant if you have `extra_bindings()` with `"cell_select"` bound. |
| 5007 | |
| 5008 | ___ |
| 5009 | |
| 5010 | ```python |
| 5011 | add_row_selection( |
| 5012 | row: int, |
| 5013 | redraw: bool = True, |
| 5014 | run_binding_func: bool = True, |
| 5015 | set_as_current: bool = True, |
| 5016 | ) -> Sheet |
| 5017 | ``` |
| 5018 | - `run_binding_func` is only relevant if you have `extra_bindings()` with `"row_select"` bound. |
| 5019 | |
| 5020 | ___ |
| 5021 | |
| 5022 | ```python |
| 5023 | add_column_selection( |
| 5024 | column: int, |
| 5025 | redraw: bool = True, |
| 5026 | run_binding_func: bool = True, |
| 5027 | set_as_current: bool = True, |
| 5028 | ) -> Sheet |
| 5029 | ``` |
| 5030 | - `run_binding_func` is only relevant if you have `extra_bindings()` with `"column_select"` bound. |
| 5031 | |
| 5032 | ___ |
| 5033 | |
| 5034 | ```python |
| 5035 | toggle_select_cell( |
| 5036 | row: int, |
| 5037 | column: int, |
| 5038 | add_selection: bool = True, |
| 5039 | redraw: bool = True, |
| 5040 | run_binding_func: bool = True, |
| 5041 | set_as_current: bool = True, |
| 5042 | ) -> Sheet |
| 5043 | ``` |
| 5044 | - `run_binding_func` is only relevant if you have `extra_bindings()` with `"cell_select"` bound. |
| 5045 | |
| 5046 | ___ |
| 5047 | |
| 5048 | ```python |
| 5049 | toggle_select_row( |
| 5050 | row: int, |
| 5051 | add_selection: bool = True, |
| 5052 | redraw: bool = True, |
| 5053 | run_binding_func: bool = True, |
| 5054 | set_as_current: bool = True, |
| 5055 | ) -> Sheet |
| 5056 | ``` |
| 5057 | - `run_binding_func` is only relevant if you have `extra_bindings()` with `"row_select"` bound. |
| 5058 | |
| 5059 | ___ |
| 5060 | |
| 5061 | ```python |
| 5062 | toggle_select_column( |
| 5063 | column: int, |
| 5064 | add_selection: bool = True, |
| 5065 | redraw: bool = True, |
| 5066 | run_binding_func: bool = True, |
| 5067 | set_as_current: bool = True, |
| 5068 | ) -> Sheet |
| 5069 | ``` |
| 5070 | - `run_binding_func` is only relevant if you have `extra_bindings()` with `"column_select"` bound. |
| 5071 | |
| 5072 | ___ |
| 5073 | |
| 5074 | ```python |
| 5075 | create_selection_box( |
| 5076 | r1: int, |
| 5077 | c1: int, |
| 5078 | r2: int, |
| 5079 | c2: int, |
| 5080 | type_: str = "cells", |
| 5081 | ) -> int |
| 5082 | ``` |
| 5083 | - `type_` either `"cells"` or `"rows"` or `"columns"`. |
| 5084 | - Returns the canvas item id for the box. |
| 5085 | |
| 5086 | ___ |
| 5087 | |
| 5088 | ```python |
| 5089 | @boxes.setter |
| 5090 | boxes(boxes: Sequence[tuple[tuple[int, int, int, int], str]]) |
| 5091 | ``` |
| 5092 | - Can be used to set the Sheets selection boxes, deselects everything before setting. |
| 5093 | |
| 5094 | Example: |
| 5095 | ```python |
| 5096 | sheet.boxes = [ |
| 5097 | ((0, 0, 3, 3), "cells"), |
| 5098 | ((4, 0, 5, 10), "rows"), |
| 5099 | ] |
| 5100 | ``` |
| 5101 | - The above would select a cells box from cell `A1` up to and including cell `C3` and row `4` (in python index, `5` as excel index) where the sheet has 10 columns. |
| 5102 | - The `str` in the type hint should be either `"cells"` or `"rows"` or `"columns"`. |
| 5103 | |
| 5104 | ___ |
| 5105 | |
| 5106 | ```python |
| 5107 | recreate_all_selection_boxes() -> Sheet |
| 5108 | ``` |
| 5109 | |
| 5110 | --- |
| 5111 | # **Row Heights and Column Widths** |
| 5112 | |
| 5113 | #### **Auto resize column widths to fit the window** |
| 5114 | |
| 5115 | To enable auto resizing of columns to the Sheet window use `set_options()` with the keyword argument `auto_resize_columns`. This argument can either be an `int` or `None`. If set as an `int` the columns will automatically resize to fit the width of the window, the `int` value being the minimum of each column in pixels. If `None` it will disable the auto resizing. Example: |
| 5116 | |
| 5117 | ```python |
| 5118 | # auto resize columns, column minimum width set to 150 pixels |
| 5119 | set_options(auto_resize_columns=150) |
| 5120 | ``` |
| 5121 | |
| 5122 | ___ |
| 5123 | |
| 5124 | #### **Auto resize row heights to fit the window** |
| 5125 | |
| 5126 | To enable auto resizing of rows to the Sheet window use `set_options()` with the keyword argument `auto_resize_rows`. This argument can either be an `int` or `None`. If set as an `int` the rows will automatically resize to fit the width of the window, the `int` value being the minimum of each row in pixels. If `None` it will disable the auto resizing. Example: |
| 5127 | |
| 5128 | ```python |
| 5129 | # auto resize rows, row minimum width set to 30 pixels |
| 5130 | set_options(auto_resize_rows=30) |
| 5131 | ``` |
| 5132 | |
| 5133 | ___ |
| 5134 | |
| 5135 | #### **Set default column width in pixels** |
| 5136 | |
| 5137 | ```python |
| 5138 | default_column_width(width: int | None = None) -> int |
| 5139 | ``` |
| 5140 | - `width` (`int`, `None`) use an `int` to set the width in pixels, `None` does not set the width. |
| 5141 | |
| 5142 | ___ |
| 5143 | |
| 5144 | #### **Set default row height in pixels or lines** |
| 5145 | |
| 5146 | ```python |
| 5147 | default_row_height(height: int | str | None = None) -> int |
| 5148 | ``` |
| 5149 | - `height` (`int`, `str`, `None`) use a numerical `str` for number of lines e.g. `"3"` for a height that fits 3 lines OR `int` for pixels. |
| 5150 | |
| 5151 | ___ |
| 5152 | |
| 5153 | #### **Set default header bar height in pixels or lines** |
| 5154 | |
| 5155 | ```python |
| 5156 | default_header_height(height: int | str | None = None) -> int |
| 5157 | ``` |
| 5158 | - `height` (`int`, `str`, `None`) use a numerical `str` for number of lines e.g. `"3"` for a height that fits 3 lines or `int` for pixels. |
| 5159 | |
| 5160 | ___ |
| 5161 | |
| 5162 | #### **Set a specific cell size to its text** |
| 5163 | |
| 5164 | ```python |
| 5165 | set_cell_size_to_text( |
| 5166 | row: int, |
| 5167 | column: int, |
| 5168 | only_set_if_too_small: bool = False, |
| 5169 | redraw: bool = True, |
| 5170 | ) -> Sheet |
| 5171 | ``` |
| 5172 | |
| 5173 | ___ |
| 5174 | |
| 5175 | #### **Set all row heights and column widths to cell text sizes** |
| 5176 | |
| 5177 | ```python |
| 5178 | set_all_cell_sizes_to_text( |
| 5179 | redraw: bool = True, |
| 5180 | width: int | None = None, |
| 5181 | slim: bool = False, |
| 5182 | ) -> tuple[list[float], list[float]] |
| 5183 | ``` |
| 5184 | - Returns the Sheets row positions and column positions in that order. |
| 5185 | - `width` a minimum width for all column widths set using this function. |
| 5186 | - `slim` column widths will be set precisely to text width and not add any extra space. |
| 5187 | |
| 5188 | ___ |
| 5189 | |
| 5190 | #### **Set all column widths to a specific width in pixels** |
| 5191 | |
| 5192 | ```python |
| 5193 | set_all_column_widths( |
| 5194 | width: int | None = None, |
| 5195 | only_set_if_too_small: bool = False, |
| 5196 | redraw: bool = True, |
| 5197 | recreate_selection_boxes: bool = True, |
| 5198 | ) -> Sheet |
| 5199 | ``` |
| 5200 | - `width` (`int`, `None`) leave `None` to set to cell text sizes for each column. |
| 5201 | |
| 5202 | ___ |
| 5203 | |
| 5204 | #### **Set all row heights to a specific height in pixels** |
| 5205 | |
| 5206 | ```python |
| 5207 | set_all_row_heights( |
| 5208 | height: int | None = None, |
| 5209 | only_set_if_too_small: bool = False, |
| 5210 | redraw: bool = True, |
| 5211 | recreate_selection_boxes: bool = True, |
| 5212 | ) -> Sheet |
| 5213 | ``` |
| 5214 | - `height` (`int`, `None`) leave `None` to set to cell text sizes for each row. |
| 5215 | |
| 5216 | ___ |
| 5217 | |
| 5218 | #### **Set or get a specific column width** |
| 5219 | |
| 5220 | ```python |
| 5221 | column_width( |
| 5222 | column: int | Literal["all", "displayed"] | None = None, |
| 5223 | width: int | Literal["default", "text"] | None = None, |
| 5224 | only_set_if_too_small: bool = False, |
| 5225 | redraw: bool = True, |
| 5226 | ) -> Sheet | int |
| 5227 | ``` |
| 5228 | |
| 5229 | ___ |
| 5230 | |
| 5231 | #### **Set or get a specific row height** |
| 5232 | |
| 5233 | ```python |
| 5234 | row_height( |
| 5235 | row: int | Literal["all", "displayed"] | None = None, |
| 5236 | height: int | Literal["default", "text"] | None = None, |
| 5237 | only_set_if_too_small: bool = False, |
| 5238 | redraw: bool = True, |
| 5239 | ) -> Sheet | int |
| 5240 | ``` |
| 5241 | |
| 5242 | ___ |
| 5243 | |
| 5244 | #### **Get the sheets column widths** |
| 5245 | |
| 5246 | ```python |
| 5247 | get_column_widths(canvas_positions: bool = False) -> list[float] |
| 5248 | ``` |
| 5249 | - `canvas_positions` (`bool`) gets the actual canvas x coordinates of column lines. |
| 5250 | |
| 5251 | ___ |
| 5252 | |
| 5253 | #### **Get the sheets row heights** |
| 5254 | |
| 5255 | ```python |
| 5256 | get_row_heights(canvas_positions: bool = False) -> list[float] |
| 5257 | ``` |
| 5258 | - `canvas_positions` (`bool`) gets the actual canvas y coordinates of row lines. |
| 5259 | |
| 5260 | ___ |
| 5261 | |
| 5262 | #### **Get a rows text height** |
| 5263 | |
| 5264 | ```python |
| 5265 | get_row_text_height( |
| 5266 | row: int, |
| 5267 | visible_only: bool = False, |
| 5268 | only_if_too_small: bool = False, |
| 5269 | ) -> int |
| 5270 | ``` |
| 5271 | - Returns a height in pixels which will fit all text in the specified row. |
| 5272 | - `visible_only` if `True` only measures rows visible on the Sheet. |
| 5273 | - `only_if_too_small` if `True` will only return a new height if the current row height is too short to accomodate its text. |
| 5274 | |
| 5275 | ___ |
| 5276 | |
| 5277 | #### **Get a columns text width** |
| 5278 | |
| 5279 | ```python |
| 5280 | get_column_text_width( |
| 5281 | column: int, |
| 5282 | visible_only: bool = False, |
| 5283 | only_if_too_small: bool = False, |
| 5284 | ) -> int |
| 5285 | ``` |
| 5286 | - Returns a width in pixels which will fit all text in the specified column. |
| 5287 | - `visible_only` if `True` only measures columns visible on the Sheet. |
| 5288 | - `only_if_too_small` if `True` will only return a new width if the current column width is too thin to accomodate its text. |
| 5289 | |
| 5290 | ___ |
| 5291 | |
| 5292 | #### **Set or reset displayed column widths/lines** |
| 5293 | |
| 5294 | ```python |
| 5295 | set_column_widths( |
| 5296 | column_widths: Iterator[int, float] | None = None, |
| 5297 | canvas_positions: bool = False, |
| 5298 | reset: bool = False, |
| 5299 | ) -> Sheet |
| 5300 | ``` |
| 5301 | |
| 5302 | ___ |
| 5303 | |
| 5304 | #### **Set or reset displayed row heights/lines** |
| 5305 | |
| 5306 | ```python |
| 5307 | set_row_heights( |
| 5308 | row_heights: Iterator[int, float] | None = None, |
| 5309 | canvas_positions: bool = False, |
| 5310 | reset: bool = False, |
| 5311 | ) -> Sheet |
| 5312 | ``` |
| 5313 | |
| 5314 | ___ |
| 5315 | |
| 5316 | #### **Set the width of the index to a str or existing values** |
| 5317 | |
| 5318 | ```python |
| 5319 | set_width_of_index_to_text(text: None | str = None, *args, **kwargs) -> Sheet |
| 5320 | ``` |
| 5321 | - `text` (`str`, `None`) provide a `str` to set the width to or use `None` to set it to existing values in the index. |
| 5322 | |
| 5323 | ___ |
| 5324 | |
| 5325 | #### **Set the width of the index to a value in pixels** |
| 5326 | |
| 5327 | ```python |
| 5328 | set_index_width(pixels: int, redraw: bool = True) -> Sheet |
| 5329 | ``` |
| 5330 | - Note that it disables auto resizing of index. Use [`set_options()`](#sheet-options-and-other-functions) to restore auto resizing. |
| 5331 | |
| 5332 | ___ |
| 5333 | |
| 5334 | #### **Set the height of the header to a str or existing values** |
| 5335 | |
| 5336 | ```python |
| 5337 | set_height_of_header_to_text(text: None | str = None) -> Sheet |
| 5338 | ``` |
| 5339 | - `text` (`str`, `None`) provide a `str` to set the height to or use `None` to set it to existing values in the header. |
| 5340 | |
| 5341 | ___ |
| 5342 | |
| 5343 | #### **Set the height of the header to a value in pixels** |
| 5344 | |
| 5345 | ```python |
| 5346 | set_header_height_pixels(pixels: int, redraw: bool = True) -> Sheet |
| 5347 | ``` |
| 5348 | |
| 5349 | ___ |
| 5350 | |
| 5351 | #### **Set the height of the header to accomodate a number of lines** |
| 5352 | |
| 5353 | ```python |
| 5354 | set_header_height_lines(nlines: int, redraw: bool = True) -> Sheet |
| 5355 | ``` |
| 5356 | |
| 5357 | ___ |
| 5358 | |
| 5359 | #### **Delete displayed column lines** |
| 5360 | |
| 5361 | ```python |
| 5362 | del_column_position(idx: int, deselect_all: bool = False) -> Sheet |
| 5363 | del_column_positions(idxs: Iterator[int] | None = None) -> Sheet |
| 5364 | ``` |
| 5365 | |
| 5366 | ___ |
| 5367 | |
| 5368 | #### **Delete displayed row lines** |
| 5369 | |
| 5370 | ```python |
| 5371 | del_row_position(idx: int, deselect_all: bool = False) -> Sheet |
| 5372 | del_row_positions(idxs: Iterator[int] | None = None) -> Sheet |
| 5373 | ``` |
| 5374 | |
| 5375 | ___ |
| 5376 | |
| 5377 | #### **Insert a displayed row line** |
| 5378 | |
| 5379 | ```python |
| 5380 | insert_row_position( |
| 5381 | idx: Literal["end"] | int = "end", |
| 5382 | height: int | None = None, |
| 5383 | deselect_all: bool = False, |
| 5384 | redraw: bool = False, |
| 5385 | ) -> Sheet |
| 5386 | ``` |
| 5387 | |
| 5388 | ___ |
| 5389 | |
| 5390 | #### **Insert a displayed column line** |
| 5391 | |
| 5392 | ```python |
| 5393 | insert_column_position( |
| 5394 | idx: Literal["end"] | int = "end", |
| 5395 | width: int | None = None, |
| 5396 | deselect_all: bool = False, |
| 5397 | redraw: bool = False, |
| 5398 | ) -> Sheet |
| 5399 | ``` |
| 5400 | |
| 5401 | ___ |
| 5402 | |
| 5403 | #### **Insert multiple displayed row lines** |
| 5404 | |
| 5405 | ```python |
| 5406 | insert_row_positions( |
| 5407 | idx: Literal["end"] | int = "end", |
| 5408 | heights: Sequence[float] | int | None = None, |
| 5409 | deselect_all: bool = False, |
| 5410 | redraw: bool = False, |
| 5411 | ) -> Sheet |
| 5412 | ``` |
| 5413 | |
| 5414 | ___ |
| 5415 | |
| 5416 | #### **Insert multiple displayed column lines** |
| 5417 | |
| 5418 | ```python |
| 5419 | insert_column_positions( |
| 5420 | idx: Literal["end"] | int = "end", |
| 5421 | widths: Sequence[float] | int | None = None, |
| 5422 | deselect_all: bool = False, |
| 5423 | redraw: bool = False, |
| 5424 | ) -> Sheet |
| 5425 | ``` |
| 5426 | |
| 5427 | ___ |
| 5428 | |
| 5429 | #### **Set the number of displayed row lines and column lines** |
| 5430 | |
| 5431 | ```python |
| 5432 | sheet_display_dimensions( |
| 5433 | total_rows: int | None =None, |
| 5434 | total_columns: int | None =None, |
| 5435 | ) -> tuple[int, int] | Sheet |
| 5436 | ``` |
| 5437 | |
| 5438 | ___ |
| 5439 | |
| 5440 | #### **Move a displayed row line** |
| 5441 | |
| 5442 | ```python |
| 5443 | move_row_position(row: int, moveto: int) -> Sheet |
| 5444 | ``` |
| 5445 | |
| 5446 | ___ |
| 5447 | |
| 5448 | #### **Move a displayed column line** |
| 5449 | |
| 5450 | ```python |
| 5451 | move_column_position(column: int, moveto: int) -> Sheet |
| 5452 | ``` |
| 5453 | |
| 5454 | ___ |
| 5455 | |
| 5456 | #### **Get a list of default column width values** |
| 5457 | |
| 5458 | ```python |
| 5459 | get_example_canvas_column_widths(total_cols: int | None = None) -> list[float] |
| 5460 | ``` |
| 5461 | |
| 5462 | ___ |
| 5463 | |
| 5464 | #### **Get a list of default row height values** |
| 5465 | |
| 5466 | ```python |
| 5467 | get_example_canvas_row_heights(total_rows: int | None = None) -> list[float] |
| 5468 | ``` |
| 5469 | |
| 5470 | ___ |
| 5471 | |
| 5472 | #### **Verify a list of row heights or canvas y coordinates** |
| 5473 | |
| 5474 | ```python |
| 5475 | verify_row_heights(row_heights: list[float], canvas_positions: bool = False) -> bool |
| 5476 | ``` |
| 5477 | |
| 5478 | ___ |
| 5479 | |
| 5480 | #### **Verify a list of column widths or canvas x coordinates** |
| 5481 | |
| 5482 | ```python |
| 5483 | verify_column_widths(column_widths: list[float], canvas_positions: bool = False) -> bool |
| 5484 | ``` |
| 5485 | |
| 5486 | ___ |
| 5487 | |
| 5488 | #### **Make a row height valid** |
| 5489 | |
| 5490 | ```python |
| 5491 | valid_row_height(height: int) -> int |
| 5492 | ``` |
| 5493 | |
| 5494 | ___ |
| 5495 | |
| 5496 | #### **Make a column width valid** |
| 5497 | |
| 5498 | ```python |
| 5499 | valid_column_width(width: int) -> int |
| 5500 | ``` |
| 5501 | |
| 5502 | ___ |
| 5503 | |
| 5504 | #### **Get visible rows** |
| 5505 | |
| 5506 | ```python |
| 5507 | @property |
| 5508 | visible_rows() -> tuple[int, int] |
| 5509 | ``` |
| 5510 | - Returns start row, end row |
| 5511 | - e.g. `start_row, end_row = sheet.visible_rows` |
| 5512 | |
| 5513 | ___ |
| 5514 | |
| 5515 | #### **Get visible columns** |
| 5516 | |
| 5517 | ```python |
| 5518 | @property |
| 5519 | visible_columns() -> tuple[int, int] |
| 5520 | ``` |
| 5521 | - Returns start column, end column |
| 5522 | - e.g. `start_column, end_column = sheet.visible_columns` |
| 5523 | |
| 5524 | --- |
| 5525 | # **Identifying Bound Event Mouse Position** |
| 5526 | |
| 5527 | The below functions require a mouse click event, for example you could bind right click, example [here](#example-custom-right-click-and-text-editor-functionality), and then identify where the user has clicked. |
| 5528 | |
| 5529 | ___ |
| 5530 | |
| 5531 | Determine if a tk `event.widget` is the `Sheet`. |
| 5532 | |
| 5533 | ```python |
| 5534 | event_widget_is_sheet( |
| 5535 | event: object, |
| 5536 | table: bool = True, |
| 5537 | index: bool = True, |
| 5538 | header: bool = True, |
| 5539 | top_left: bool = True, |
| 5540 | ) -> bool |
| 5541 | ``` |
| 5542 | Notes: |
| 5543 | |
| 5544 | - Parameters set to `True` will include events that occurred within that widget. |
| 5545 | - e.g. If an event occurs in the top left corner of the sheet but the parameter `top_left` is `False` the function will return `False`. |
| 5546 | |
| 5547 | ___ |
| 5548 | |
| 5549 | Check if any Sheet widgets have focus. |
| 5550 | |
| 5551 | ```python |
| 5552 | has_focus() -> bool: |
| 5553 | ``` |
| 5554 | - Includes child widgets such as scroll bars. |
| 5555 | |
| 5556 | ___ |
| 5557 | |
| 5558 | ```python |
| 5559 | identify_region(event: object) -> Literal["table", "index", "header", "top left"] |
| 5560 | ``` |
| 5561 | |
| 5562 | ___ |
| 5563 | |
| 5564 | ```python |
| 5565 | identify_row( |
| 5566 | event: object, |
| 5567 | exclude_index: bool = False, |
| 5568 | allow_end: bool = True, |
| 5569 | ) -> int | None |
| 5570 | ``` |
| 5571 | |
| 5572 | ___ |
| 5573 | |
| 5574 | ```python |
| 5575 | identify_column( |
| 5576 | event: object, |
| 5577 | exclude_header: bool = False, |
| 5578 | allow_end: bool = True, |
| 5579 | ) -> int | None |
| 5580 | ``` |
| 5581 | |
| 5582 | ___ |
| 5583 | |
| 5584 | #### **Sheet control actions** |
| 5585 | |
| 5586 | For example: `sheet.bind("<Control-B>", sheet.paste)` |
| 5587 | |
| 5588 | ```python |
| 5589 | cut(event: object = None, validation: bool = True) -> None | EventDataDict |
| 5590 | paste(event: object = None, validation: bool = True) -> None | EventDataDict |
| 5591 | delete(event: object = None, validation: bool = True) -> None | EventDataDict |
| 5592 | copy(event: object = None) -> None | EventDataDict |
| 5593 | undo(event: object = None) -> None | EventDataDict |
| 5594 | redo(event: object = None) -> None | EventDataDict |
| 5595 | ``` |
| 5596 | - `validation` (`bool`) when `False` disables any bound `edit_validation()` function from running. |
| 5597 | |
| 5598 | --- |
| 5599 | # **Scroll Positions and Cell Visibility** |
| 5600 | |
| 5601 | #### **Sync scroll positions between widgets** |
| 5602 | |
| 5603 | ```python |
| 5604 | sync_scroll(widget: object) -> Sheet |
| 5605 | ``` |
| 5606 | - Sync scroll positions between `Sheet`s, may or may not work with other widgets. Uses scrollbar positions. |
| 5607 | |
| 5608 | Syncing two sheets: |
| 5609 | ```python |
| 5610 | self.sheet1.sync_scroll(self.sheet2) |
| 5611 | ``` |
| 5612 | |
| 5613 | Syncing three sheets: |
| 5614 | ```python |
| 5615 | # syncs sheet 1 and 2 between each other |
| 5616 | self.sheet1.sync_scroll(self.sheet2) |
| 5617 | |
| 5618 | # syncs sheet 1 and 3 between each other |
| 5619 | self.sheet1.sync_scroll(self.sheet3) |
| 5620 | |
| 5621 | # syncs sheet 2 and 3 between each other |
| 5622 | self.sheet2.sync_scroll(self.sheet3) |
| 5623 | ``` |
| 5624 | |
| 5625 | #### **Unsync scroll positions between widgets** |
| 5626 | |
| 5627 | ```python |
| 5628 | unsync_scroll(widget: object = None) -> Sheet |
| 5629 | ``` |
| 5630 | - Leaving `widget` as `None` unsyncs all previously synced widgets. |
| 5631 | |
| 5632 | #### **See or scroll to a specific cell on the sheet** |
| 5633 | |
| 5634 | ```python |
| 5635 | see( |
| 5636 | row: int = 0, |
| 5637 | column: int = 0, |
| 5638 | keep_yscroll: bool = False, |
| 5639 | keep_xscroll: bool = False, |
| 5640 | bottom_right_corner: bool | None = None, |
| 5641 | check_cell_visibility: bool = True, |
| 5642 | redraw: bool = True, |
| 5643 | ) -> Sheet |
| 5644 | ``` |
| 5645 | |
| 5646 | ___ |
| 5647 | |
| 5648 | #### **Check if a cell has any part of it visible** |
| 5649 | |
| 5650 | ```python |
| 5651 | cell_visible(r: int, c: int) -> bool |
| 5652 | ``` |
| 5653 | |
| 5654 | ___ |
| 5655 | |
| 5656 | #### **Check if a cell is totally visible** |
| 5657 | |
| 5658 | ```python |
| 5659 | cell_completely_visible(r: int, c: int, seperate_axes: bool = False) -> bool |
| 5660 | ``` |
| 5661 | - `separate_axes` returns tuple of bools e.g. `(cell y axis is visible, cell x axis is visible)` |
| 5662 | |
| 5663 | ___ |
| 5664 | |
| 5665 | ```python |
| 5666 | set_xview(position: None | float = None, option: str = "moveto") -> Sheet | tuple[float, float] |
| 5667 | ``` |
| 5668 | Notes: |
| 5669 | - If `position` is `None` then `tuple[float, float]` of main table `xview()` is returned. |
| 5670 | - `xview` and `xview_moveto` have the same behaviour. |
| 5671 | |
| 5672 | ___ |
| 5673 | |
| 5674 | ```python |
| 5675 | set_yview(position: None | float = None, option: str = "moveto") -> Sheet | tuple[float, float] |
| 5676 | ``` |
| 5677 | - If `position` is `None` then `tuple[float, float]` of main table `yview()` is returned. |
| 5678 | - `yview` and `yview_moveto` have the same behaviour. |
| 5679 | |
| 5680 | ___ |
| 5681 | |
| 5682 | ```python |
| 5683 | get_xview() -> tuple[float, float] |
| 5684 | ``` |
| 5685 | |
| 5686 | ___ |
| 5687 | |
| 5688 | ```python |
| 5689 | get_yview() -> tuple[float, float] |
| 5690 | ``` |
| 5691 | |
| 5692 | ___ |
| 5693 | |
| 5694 | ```python |
| 5695 | set_view(x_args: [str, float], y_args: [str, float]) -> Sheet |
| 5696 | ``` |
| 5697 | |
| 5698 | --- |
| 5699 | # **Hiding Columns** |
| 5700 | |
| 5701 | Note that once you have hidden columns you can use the function `displayed_column_to_data(column)` to retrieve a column data index from a displayed index. |
| 5702 | |
| 5703 | #### **Display only certain columns** |
| 5704 | |
| 5705 | ```python |
| 5706 | display_columns( |
| 5707 | columns: None | Literal["all"] | Iterator[int] = None, |
| 5708 | all_columns_displayed: None | bool = None, |
| 5709 | reset_col_positions: bool = True, |
| 5710 | refresh: bool = False, |
| 5711 | redraw: bool = False, |
| 5712 | deselect_all: bool = True, |
| 5713 | **kwargs, |
| 5714 | ) -> list[int] | None |
| 5715 | ``` |
| 5716 | Parameters: |
| 5717 | |
| 5718 | - `columns` (`int`, `iterable`, `"all"`) are the columns to be displayed, omit the columns to be hidden. |
| 5719 | - Use argument `True` with `all_columns_displayed` to display all columns, use `False` to display only the columns you've set using the `columns` arg. |
| 5720 | - You can also use the keyword argument `all_displayed` instead of `all_columns_displayed`. |
| 5721 | |
| 5722 | Examples: |
| 5723 | ```python |
| 5724 | # display all columns |
| 5725 | self.sheet.display_columns("all") |
| 5726 | |
| 5727 | # displaying specific columns only |
| 5728 | self.sheet.display_columns([2, 4, 7], all_displayed = False) |
| 5729 | ``` |
| 5730 | |
| 5731 | ___ |
| 5732 | |
| 5733 | #### **Get all columns displayed boolean** |
| 5734 | |
| 5735 | **Get the bool** |
| 5736 | ```python |
| 5737 | @property |
| 5738 | all_columns() |
| 5739 | ``` |
| 5740 | - e.g. `get_all_columns_displayed = sheet.all_columns`. |
| 5741 | |
| 5742 | **Set the bool** |
| 5743 | ```python |
| 5744 | @all_columns.setter |
| 5745 | all_columns(a: bool) |
| 5746 | ``` |
| 5747 | e.g. `sheet.all_columns = True`. |
| 5748 | |
| 5749 | ```python |
| 5750 | all_columns_displayed(a: bool | None = None) -> bool |
| 5751 | ``` |
| 5752 | - `a` (`bool`, `None`) Either set by using `bool` or get by leaving `None` e.g. `all_columns_displayed()`. |
| 5753 | |
| 5754 | ___ |
| 5755 | |
| 5756 | #### **Hide specific columns** |
| 5757 | |
| 5758 | ```python |
| 5759 | hide_columns( |
| 5760 | columns: int | set | Iterator[int] = set(), |
| 5761 | redraw: bool = True, |
| 5762 | deselect_all: bool = True, |
| 5763 | data_indexes: bool = False, |
| 5764 | ) -> Sheet |
| 5765 | ``` |
| 5766 | Parameters: |
| 5767 | |
| 5768 | - **NOTE**: `columns` (`int`) by default uses displayed column indexes, not data indexes. In other words the indexes of the columns displayed on the screen are the ones that are hidden, this is useful when used in conjunction with `get_selected_columns()`. |
| 5769 | - `data_indexes` when `False` it makes the `columns` parameter indexes represent displayed columns and not the underlying Sheet data columns. When `True` the indexes represent data indexes. |
| 5770 | |
| 5771 | Example: |
| 5772 | ```python |
| 5773 | columns_to_hide = set(sheet.data_c(c) for c in sheet.get_selected_columns()) |
| 5774 | sheet.hide_columns( |
| 5775 | columns_to_hide, |
| 5776 | data_indexes=True, |
| 5777 | ) |
| 5778 | ``` |
| 5779 | |
| 5780 | ___ |
| 5781 | |
| 5782 | #### **Unhide specific columns** |
| 5783 | |
| 5784 | ```python |
| 5785 | show_columns( |
| 5786 | columns: int | Iterator[int], |
| 5787 | redraw: bool = True, |
| 5788 | deselect_all: bool = True, |
| 5789 | ) -> Sheet |
| 5790 | ``` |
| 5791 | Parameters: |
| 5792 | |
| 5793 | - **NOTE**: `columns` (`int`) uses data column indexes, not displayed indexes. In other words the indexes of the columns which represent the underlying data are shown. |
| 5794 | |
| 5795 | Notes: |
| 5796 | |
| 5797 | - Will return if all columns are currently displayed (`Sheet.all_columns`). |
| 5798 | |
| 5799 | Example: |
| 5800 | ```python |
| 5801 | # converting displayed column indexes to data indexes using data_c(c) |
| 5802 | columns = set(sheet.data_c(c) for c in sheet.get_selected_columns()) |
| 5803 | |
| 5804 | # hiding columns |
| 5805 | sheet.hide_columns( |
| 5806 | columns, |
| 5807 | data_indexes=True, |
| 5808 | ) |
| 5809 | |
| 5810 | # showing them again |
| 5811 | sheet.show_columns(columns) |
| 5812 | ``` |
| 5813 | |
| 5814 | ___ |
| 5815 | |
| 5816 | #### **Displayed column index to data** |
| 5817 | |
| 5818 | Convert a displayed column index to a data index. If the internal `all_columns_displayed` attribute is `True` then it will return the provided argument. |
| 5819 | ```python |
| 5820 | displayed_column_to_data(c) |
| 5821 | data_c(c) |
| 5822 | ``` |
| 5823 | |
| 5824 | ___ |
| 5825 | |
| 5826 | #### **Get currently displayed columns** |
| 5827 | |
| 5828 | ```python |
| 5829 | @property |
| 5830 | displayed_columns() -> list[int] |
| 5831 | ``` |
| 5832 | - e.g. `columns = sheet.displayed_columns` |
| 5833 | |
| 5834 | --- |
| 5835 | # **Hiding Rows** |
| 5836 | |
| 5837 | Note that once you have hidden rows you can use the function `displayed_row_to_data(row)` to retrieve a row data index from a displayed index. |
| 5838 | |
| 5839 | #### **Display only certain rows** |
| 5840 | |
| 5841 | ```python |
| 5842 | display_rows( |
| 5843 | rows: None | Literal["all"] | Iterator[int] = None, |
| 5844 | all_rows_displayed: None | bool = None, |
| 5845 | reset_row_positions: bool = True, |
| 5846 | refresh: bool = False, |
| 5847 | redraw: bool = False, |
| 5848 | deselect_all: bool = True, |
| 5849 | **kwargs, |
| 5850 | ) -> list[int] | None |
| 5851 | ``` |
| 5852 | Parameters: |
| 5853 | |
| 5854 | - `rows` (`int`, `iterable`, `"all"`) are the rows to be displayed, omit the rows to be hidden. |
| 5855 | - Use argument `True` with `all_rows_displayed` to display all rows, use `False` to display only the rows you've set using the `rows` arg. |
| 5856 | - You can also use the keyword argument `all_displayed` instead of `all_rows_displayed`. |
| 5857 | |
| 5858 | Examples: |
| 5859 | |
| 5860 | - An example of row filtering using this function can be found [here](#example-header-dropdown-boxes-and-row-filtering). |
| 5861 | - More examples below: |
| 5862 | ```python |
| 5863 | # display all rows |
| 5864 | self.sheet.display_rows("all") |
| 5865 | |
| 5866 | # display specific rows only |
| 5867 | self.sheet.display_rows([2, 4, 7], all_displayed = False) |
| 5868 | ``` |
| 5869 | |
| 5870 | ___ |
| 5871 | |
| 5872 | #### **Hide specific rows** |
| 5873 | |
| 5874 | ```python |
| 5875 | hide_rows( |
| 5876 | rows: int | set | Iterator[int] = set(), |
| 5877 | redraw: bool = True, |
| 5878 | deselect_all: bool = True, |
| 5879 | data_indexes: bool = False, |
| 5880 | ) -> Sheet |
| 5881 | ``` |
| 5882 | Parameters: |
| 5883 | |
| 5884 | - **NOTE**: `rows` (`int`) by default uses displayed row indexes, not data indexes. In other words the indexes of the rows displayed on the screen are the ones that are hidden, this is useful when used in conjunction with `get_selected_rows()`. |
| 5885 | - `data_indexes` when `False` it makes the `rows` parameter indexes represent displayed rows and not the underlying Sheet data rows. When `True` the indexes represent data indexes. |
| 5886 | |
| 5887 | Example: |
| 5888 | ```python |
| 5889 | rows_to_hide = set(sheet.data_r(r) for r in sheet.get_selected_rows()) |
| 5890 | sheet.hide_rows( |
| 5891 | rows_to_hide, |
| 5892 | data_indexes=True, |
| 5893 | ) |
| 5894 | ``` |
| 5895 | |
| 5896 | ___ |
| 5897 | |
| 5898 | #### **Unhide specific rows** |
| 5899 | |
| 5900 | ```python |
| 5901 | show_rows( |
| 5902 | rows: int | Iterator[int], |
| 5903 | redraw: bool = True, |
| 5904 | deselect_all: bool = True, |
| 5905 | ) -> Sheet |
| 5906 | ``` |
| 5907 | Parameters: |
| 5908 | |
| 5909 | - **NOTE**: `rows` (`int`) uses data row indexes, not displayed indexes. In other words the indexes of the rows which represent the underlying data are shown. |
| 5910 | |
| 5911 | Notes: |
| 5912 | |
| 5913 | - Will return if all rows are currently displayed (`Sheet.all_rows`). |
| 5914 | |
| 5915 | Example: |
| 5916 | ```python |
| 5917 | # converting displayed row indexes to data indexes using data_r(r) |
| 5918 | rows = set(sheet.data_r(r) for r in sheet.get_selected_rows()) |
| 5919 | |
| 5920 | # hiding rows |
| 5921 | sheet.hide_rows( |
| 5922 | rows, |
| 5923 | data_indexes=True, |
| 5924 | ) |
| 5925 | |
| 5926 | # showing them again |
| 5927 | sheet.show_rows(rows) |
| 5928 | ``` |
| 5929 | |
| 5930 | ___ |
| 5931 | |
| 5932 | #### **Get or set the all rows displayed boolean** |
| 5933 | |
| 5934 | **Get the bool** |
| 5935 | ```python |
| 5936 | @property |
| 5937 | all_rows() |
| 5938 | ``` |
| 5939 | - e.g. `get_all_rows_displayed = sheet.all_rows`. |
| 5940 | |
| 5941 | **Set the bool** |
| 5942 | ```python |
| 5943 | @all_rows.setter |
| 5944 | all_rows(a: bool) |
| 5945 | ``` |
| 5946 | e.g. `sheet.all_rows = True`. |
| 5947 | |
| 5948 | ```python |
| 5949 | all_rows_displayed(a: bool | None = None) -> bool |
| 5950 | ``` |
| 5951 | - `a` (`bool`, `None`) Either set by using `bool` or get by leaving `None` e.g. `all_rows_displayed()`. |
| 5952 | |
| 5953 | ___ |
| 5954 | |
| 5955 | #### **Displayed row index to data** |
| 5956 | |
| 5957 | Convert a displayed row index to a data index. If the internal `all_rows_displayed` attribute is `True` then it will return the provided argument. |
| 5958 | ```python |
| 5959 | displayed_row_to_data(r) |
| 5960 | data_r(r) |
| 5961 | ``` |
| 5962 | |
| 5963 | ___ |
| 5964 | |
| 5965 | #### **Get currently displayed rows** |
| 5966 | |
| 5967 | ```python |
| 5968 | @property |
| 5969 | displayed_rows() -> list[int] |
| 5970 | ``` |
| 5971 | - e.g. `rows = sheet.displayed_rows` |
| 5972 | |
| 5973 | --- |
| 5974 | # **Hiding Sheet Elements** |
| 5975 | |
| 5976 | #### **Hide parts of the Sheet or all of it** |
| 5977 | |
| 5978 | ```python |
| 5979 | hide( |
| 5980 | canvas: Literal[ |
| 5981 | "all", |
| 5982 | "row_index", |
| 5983 | "header", |
| 5984 | "top_left", |
| 5985 | "x_scrollbar", |
| 5986 | "y_scrollbar", |
| 5987 | ] = "all", |
| 5988 | ) -> Sheet |
| 5989 | ``` |
| 5990 | - `canvas` (`str`) options are `all`, `row_index`, `header`, `top_left`, `x_scrollbar`, `y_scrollbar` |
| 5991 | - `all` hides the entire table and is the default. |
| 5992 | |
| 5993 | ___ |
| 5994 | |
| 5995 | #### **Show parts of the Sheet or all of it** |
| 5996 | |
| 5997 | ```python |
| 5998 | show( |
| 5999 | canvas: Literal[ |
| 6000 | "all", |
| 6001 | "row_index", |
| 6002 | "header", |
| 6003 | "top_left", |
| 6004 | "x_scrollbar", |
| 6005 | "y_scrollbar", |
| 6006 | ] = "all", |
| 6007 | ) -> Sheet |
| 6008 | ``` |
| 6009 | - `canvas` (`str`) options are `all`, `row_index`, `header`, `top_left`, `x_scrollbar`, `y_scrollbar` |
| 6010 | - `all` shows the entire table and is the default. |
| 6011 | |
| 6012 | --- |
| 6013 | # **Sheet Height and Width** |
| 6014 | |
| 6015 | #### **Modify widget height and width in pixels** |
| 6016 | |
| 6017 | ```python |
| 6018 | height_and_width( |
| 6019 | height: int | None = None, |
| 6020 | width: int | None = None, |
| 6021 | ) -> Sheet |
| 6022 | ``` |
| 6023 | - `height` (`int`, `None`) set a height in pixels. |
| 6024 | - `width` (`int`, `None`) set a width in pixels. |
| 6025 | If both arguments are `None` then table will reset to default tkinter canvas dimensions. |
| 6026 | |
| 6027 | ___ |
| 6028 | |
| 6029 | ```python |
| 6030 | get_frame_y(y: int) -> int |
| 6031 | ``` |
| 6032 | - Adds the height of the Sheets header to a y position. |
| 6033 | |
| 6034 | ___ |
| 6035 | |
| 6036 | ```python |
| 6037 | get_frame_x(x: int) -> int |
| 6038 | ``` |
| 6039 | - Adds the width of the Sheets index to an x position. |
| 6040 | |
| 6041 | --- |
| 6042 | # **Cell Text Editor** |
| 6043 | |
| 6044 | #### **Open the currently selected cell in the main table** |
| 6045 | |
| 6046 | ```python |
| 6047 | open_cell(ignore_existing_editor: bool = True) -> Sheet |
| 6048 | ``` |
| 6049 | - Function utilises the currently selected cell in the main table, even if a column/row is selected, to open a non selected cell first use `set_currently_selected()` to set the cell to open. |
| 6050 | |
| 6051 | ___ |
| 6052 | |
| 6053 | #### **Open the currently selected cell but in the header** |
| 6054 | |
| 6055 | ```python |
| 6056 | open_header_cell(ignore_existing_editor: bool = True) -> Sheet |
| 6057 | ``` |
| 6058 | - Also uses currently selected cell, which you can set with `set_currently_selected()`. |
| 6059 | |
| 6060 | ___ |
| 6061 | |
| 6062 | #### **Open the currently selected cell but in the index** |
| 6063 | |
| 6064 | ```python |
| 6065 | open_index_cell(ignore_existing_editor: bool = True) -> Sheet |
| 6066 | ``` |
| 6067 | - Also uses currently selected cell, which you can set with `set_currently_selected()`. |
| 6068 | |
| 6069 | ___ |
| 6070 | |
| 6071 | #### **Set the cell text editor value if it is open** |
| 6072 | |
| 6073 | **Table:** |
| 6074 | |
| 6075 | ```python |
| 6076 | set_text_editor_value( |
| 6077 | text: str = "", |
| 6078 | ) -> Sheet |
| 6079 | ``` |
| 6080 | |
| 6081 | **Index:** |
| 6082 | |
| 6083 | ```python |
| 6084 | set_index_text_editor_value( |
| 6085 | text: str = "", |
| 6086 | ) -> Sheet |
| 6087 | ``` |
| 6088 | |
| 6089 | **Header:** |
| 6090 | |
| 6091 | ```python |
| 6092 | set_header_text_editor_value( |
| 6093 | text: str = "", |
| 6094 | ) -> Sheet |
| 6095 | ``` |
| 6096 | |
| 6097 | ___ |
| 6098 | |
| 6099 | #### **Close any existing text editor** |
| 6100 | |
| 6101 | ```python |
| 6102 | close_text_editor(set_data: bool = True) -> Sheet |
| 6103 | ``` |
| 6104 | Notes: |
| 6105 | |
| 6106 | - Closes any open text editors, including header and index. |
| 6107 | - Also closes any existing `"normal"` state dropdown box. |
| 6108 | |
| 6109 | Parameters: |
| 6110 | |
| 6111 | - `set_data` (`bool`) when `True` sets the cell data to the text editor value (if it is valid). When `False` the text editor is closed without setting data. |
| 6112 | |
| 6113 | ___ |
| 6114 | |
| 6115 | #### **Get an existing text editors value** |
| 6116 | |
| 6117 | ```python |
| 6118 | get_text_editor_value() -> str | None |
| 6119 | ``` |
| 6120 | Notes: |
| 6121 | |
| 6122 | - `None` is returned if no text editor exists, a `str` of the text editors value will be returned if it does. |
| 6123 | |
| 6124 | ___ |
| 6125 | |
| 6126 | #### **Hide all text editors** |
| 6127 | |
| 6128 | ```python |
| 6129 | destroy_text_editor(event: object = None) -> Sheet |
| 6130 | ``` |
| 6131 | |
| 6132 | ___ |
| 6133 | |
| 6134 | #### **Get the table tk Text widget which acts as the text editor** |
| 6135 | |
| 6136 | ```python |
| 6137 | get_text_editor_widget(event: object = None) -> tk.Text | None |
| 6138 | ``` |
| 6139 | |
| 6140 | ___ |
| 6141 | |
| 6142 | #### **Bind additional keys to the text editor window** |
| 6143 | |
| 6144 | ```python |
| 6145 | bind_key_text_editor(key: str, function: Callable) -> Sheet |
| 6146 | ``` |
| 6147 | |
| 6148 | ___ |
| 6149 | |
| 6150 | #### **Unbind additional text editors bindings set using the above function** |
| 6151 | |
| 6152 | ```python |
| 6153 | unbind_key_text_editor(key: str) -> Sheet |
| 6154 | ``` |
| 6155 | |
| 6156 | --- |
| 6157 | # **Sheet Options and Other Functions** |
| 6158 | |
| 6159 | ```python |
| 6160 | set_options(redraw: bool = True, **kwargs) -> Sheet |
| 6161 | ``` |
| 6162 | |
| 6163 | The list of key word arguments available for `set_options()` are as follows, [see here](#initialization-options) as a guide for argument types. |
| 6164 | ```python |
| 6165 | name |
| 6166 | |
| 6167 | show_top_left |
| 6168 | |
| 6169 | sort_key |
| 6170 | edit_cell_tab |
| 6171 | edit_cell_return |
| 6172 | editor_del_key |
| 6173 | auto_resize_columns |
| 6174 | auto_resize_rows |
| 6175 | to_clipboard_delimiter |
| 6176 | to_clipboard_quotechar |
| 6177 | to_clipboard_lineterminator |
| 6178 | from_clipboard_delimiters |
| 6179 | show_dropdown_borders |
| 6180 | show_default_header_for_empty |
| 6181 | show_default_index_for_empty |
| 6182 | selected_rows_to_end_of_window |
| 6183 | horizontal_grid_to_end_of_window |
| 6184 | vertical_grid_to_end_of_window |
| 6185 | paste_insert_column_limit |
| 6186 | paste_insert_row_limit |
| 6187 | paste_can_expand_x |
| 6188 | paste_can_expand_y |
| 6189 | arrow_key_down_right_scroll_page |
| 6190 | enable_edit_cell_auto_resize |
| 6191 | page_up_down_select_row |
| 6192 | display_selected_fg_over_highlights |
| 6193 | show_horizontal_grid |
| 6194 | show_vertical_grid |
| 6195 | empty_horizontal |
| 6196 | empty_vertical |
| 6197 | default_row_height |
| 6198 | default_column_width |
| 6199 | default_header_height |
| 6200 | default_row_index_width |
| 6201 | row_drag_and_drop_perform |
| 6202 | column_drag_and_drop_perform |
| 6203 | auto_resize_default_row_index |
| 6204 | default_header |
| 6205 | default_row_index |
| 6206 | min_column_width |
| 6207 | max_column_width |
| 6208 | max_row_height |
| 6209 | max_header_height |
| 6210 | max_index_width |
| 6211 | font |
| 6212 | header_font |
| 6213 | index_font |
| 6214 | |
| 6215 | show_selected_cells_border |
| 6216 | theme |
| 6217 | top_left_bg |
| 6218 | top_left_fg |
| 6219 | top_left_fg_highlight |
| 6220 | |
| 6221 | table_bg |
| 6222 | table_grid_fg |
| 6223 | table_fg |
| 6224 | table_selected_box_cells_fg |
| 6225 | table_selected_box_rows_fg |
| 6226 | table_selected_box_columns_fg |
| 6227 | table_selected_cells_border_fg |
| 6228 | table_selected_cells_bg |
| 6229 | table_selected_cells_fg |
| 6230 | table_selected_rows_border_fg |
| 6231 | table_selected_rows_bg |
| 6232 | table_selected_rows_fg |
| 6233 | table_selected_columns_border_fg |
| 6234 | table_selected_columns_bg |
| 6235 | table_selected_columns_fg |
| 6236 | |
| 6237 | header_bg |
| 6238 | header_border_fg |
| 6239 | header_grid_fg |
| 6240 | header_fg |
| 6241 | header_selected_cells_bg |
| 6242 | header_selected_cells_fg |
| 6243 | header_selected_columns_bg |
| 6244 | header_selected_columns_fg |
| 6245 | |
| 6246 | index_bg |
| 6247 | index_border_fg |
| 6248 | index_grid_fg |
| 6249 | index_fg |
| 6250 | index_selected_cells_bg |
| 6251 | index_selected_cells_fg |
| 6252 | index_selected_rows_bg |
| 6253 | index_selected_rows_fg |
| 6254 | |
| 6255 | resizing_line_fg |
| 6256 | drag_and_drop_bg |
| 6257 | outline_thickness |
| 6258 | outline_color |
| 6259 | frame_bg |
| 6260 | popup_menu_font |
| 6261 | popup_menu_fg |
| 6262 | popup_menu_bg |
| 6263 | popup_menu_highlight_bg |
| 6264 | popup_menu_highlight_fg |
| 6265 | |
| 6266 | # scroll bars |
| 6267 | vertical_scroll_background |
| 6268 | horizontal_scroll_background |
| 6269 | vertical_scroll_troughcolor |
| 6270 | horizontal_scroll_troughcolor |
| 6271 | vertical_scroll_lightcolor |
| 6272 | horizontal_scroll_lightcolor |
| 6273 | vertical_scroll_darkcolor |
| 6274 | horizontal_scroll_darkcolor |
| 6275 | vertical_scroll_bordercolor |
| 6276 | horizontal_scroll_bordercolor |
| 6277 | vertical_scroll_active_bg |
| 6278 | horizontal_scroll_active_bg |
| 6279 | vertical_scroll_not_active_bg |
| 6280 | horizontal_scroll_not_active_bg |
| 6281 | vertical_scroll_pressed_bg |
| 6282 | horizontal_scroll_pressed_bg |
| 6283 | vertical_scroll_active_fg |
| 6284 | horizontal_scroll_active_fg |
| 6285 | vertical_scroll_not_active_fg |
| 6286 | horizontal_scroll_not_active_fg |
| 6287 | vertical_scroll_pressed_fg |
| 6288 | horizontal_scroll_pressed_fg |
| 6289 | scrollbar_theme_inheritance |
| 6290 | scrollbar_show_arrows |
| 6291 | vertical_scroll_arrowsize |
| 6292 | horizontal_scroll_arrowsize |
| 6293 | vertical_scroll_borderwidth |
| 6294 | horizontal_scroll_borderwidth |
| 6295 | vertical_scroll_gripcount |
| 6296 | horizontal_scroll_gripcount |
| 6297 | |
| 6298 | # for changing the in-built right click menus labels |
| 6299 | # use a string as an argument |
| 6300 | |
| 6301 | sort_cells_label |
| 6302 | sort_cells_x_label |
| 6303 | sort_row_label |
| 6304 | sort_column_label |
| 6305 | sort_rows_label |
| 6306 | sort_columns_label |
| 6307 | sort_cells_reverse_label |
| 6308 | sort_cells_x_reverse_label |
| 6309 | sort_row_reverse_label |
| 6310 | sort_column_reverse_label |
| 6311 | sort_rows_reverse_label |
| 6312 | sort_columns_reverse_label |
| 6313 | sort_cells_accelerator |
| 6314 | sort_cells_x_accelerator |
| 6315 | sort_row_accelerator |
| 6316 | sort_column_accelerator |
| 6317 | sort_rows_accelerator |
| 6318 | sort_columns_accelerator |
| 6319 | sort_cells_reverse_accelerator |
| 6320 | sort_cells_x_reverse_accelerator |
| 6321 | sort_row_reverse_accelerator |
| 6322 | sort_column_reverse_accelerator |
| 6323 | sort_rows_reverse_accelerator |
| 6324 | sort_columns_reverse_accelerator |
| 6325 | |
| 6326 | edit_header_label |
| 6327 | edit_header_accelerator |
| 6328 | edit_index_label |
| 6329 | edit_index_accelerator |
| 6330 | edit_cell_label |
| 6331 | edit_cell_accelerator |
| 6332 | cut_label |
| 6333 | cut_accelerator |
| 6334 | cut_contents_label |
| 6335 | cut_contents_accelerator |
| 6336 | copy_label |
| 6337 | copy_accelerator |
| 6338 | copy_contents_label |
| 6339 | copy_contents_accelerator |
| 6340 | paste_label |
| 6341 | paste_accelerator |
| 6342 | delete_label |
| 6343 | delete_accelerator |
| 6344 | clear_contents_label |
| 6345 | clear_contents_accelerator |
| 6346 | delete_columns_label |
| 6347 | delete_columns_accelerator |
| 6348 | insert_columns_left_label |
| 6349 | insert_columns_left_accelerator |
| 6350 | insert_column_label |
| 6351 | insert_column_accelerator |
| 6352 | insert_columns_right_label |
| 6353 | insert_columns_right_accelerator |
| 6354 | delete_rows_label |
| 6355 | delete_rows_accelerator |
| 6356 | insert_rows_above_label |
| 6357 | insert_rows_above_accelerator |
| 6358 | insert_rows_below_label |
| 6359 | insert_rows_below_accelerator |
| 6360 | insert_row_label |
| 6361 | insert_row_accelerator |
| 6362 | select_all_label |
| 6363 | select_all_accelerator |
| 6364 | undo_label |
| 6365 | undo_accelerator |
| 6366 | |
| 6367 | # for changing the keyboard bindings for copy, paste, etc. |
| 6368 | # use a list of strings as an argument |
| 6369 | copy_bindings |
| 6370 | cut_bindings |
| 6371 | paste_bindings |
| 6372 | undo_bindings |
| 6373 | redo_bindings |
| 6374 | delete_bindings |
| 6375 | select_all_bindings |
| 6376 | select_columns_bindings |
| 6377 | select_rows_bindings |
| 6378 | row_start_bindings |
| 6379 | table_start_bindings |
| 6380 | tab_bindings |
| 6381 | up_bindings |
| 6382 | right_bindings |
| 6383 | down_bindings |
| 6384 | left_bindings |
| 6385 | shift_up_bindings |
| 6386 | shift_right_bindings |
| 6387 | shift_down_bindings |
| 6388 | shift_left_bindings |
| 6389 | prior_bindings |
| 6390 | next_bindings |
| 6391 | ``` |
| 6392 | Notes: |
| 6393 | |
| 6394 | - A dictionary can be provided instead of using the keyword arguments: |
| 6395 | |
| 6396 | ```python |
| 6397 | kwargs = { |
| 6398 | "copy_bindings": [ |
| 6399 | "<Control-g>", |
| 6400 | "<Control-G>", |
| 6401 | ], |
| 6402 | "cut_bindings": [ |
| 6403 | "<Control-c>", |
| 6404 | "<Control-C>", |
| 6405 | ], |
| 6406 | } |
| 6407 | sheet.set_options(**kwargs) |
| 6408 | ``` |
| 6409 | |
| 6410 | ___ |
| 6411 | |
| 6412 | Get internal storage dictionary of highlights, readonly cells, dropdowns etc. Specifically for cell options. |
| 6413 | ```python |
| 6414 | get_cell_options(key: None | str = None, canvas: Literal["table", "row_index", "header"] = "table") -> dict |
| 6415 | ``` |
| 6416 | |
| 6417 | ___ |
| 6418 | |
| 6419 | Get internal storage dictionary of highlights, readonly rows, dropdowns etc. Specifically for row options. |
| 6420 | ```python |
| 6421 | get_row_options(key: None | str = None) -> dict |
| 6422 | ``` |
| 6423 | |
| 6424 | ___ |
| 6425 | |
| 6426 | Get internal storage dictionary of highlights, readonly columns, dropdowns etc. Specifically for column options. |
| 6427 | ```python |
| 6428 | get_column_options(key: None | str = None) -> dict |
| 6429 | ``` |
| 6430 | |
| 6431 | ___ |
| 6432 | |
| 6433 | Get internal storage dictionary of highlights, readonly header cells, dropdowns etc. Specifically for header options. |
| 6434 | ```python |
| 6435 | get_header_options(key: None | str = None) -> dict |
| 6436 | ``` |
| 6437 | |
| 6438 | ___ |
| 6439 | |
| 6440 | Get internal storage dictionary of highlights, readonly row index cells, dropdowns etc. Specifically for row index options. |
| 6441 | ```python |
| 6442 | get_index_options(key: None | str = None) -> dict |
| 6443 | ``` |
| 6444 | |
| 6445 | ___ |
| 6446 | |
| 6447 | Delete any formats, alignments, dropdown boxes, check boxes, highlights etc. that are larger than the sheets currently held data, includes row index and header in measurement of dimensions. |
| 6448 | ```python |
| 6449 | del_out_of_bounds_options() -> Sheet |
| 6450 | ``` |
| 6451 | |
| 6452 | ___ |
| 6453 | |
| 6454 | Delete all alignments, dropdown boxes, check boxes, highlights etc. |
| 6455 | ```python |
| 6456 | reset_all_options() -> Sheet |
| 6457 | ``` |
| 6458 | |
| 6459 | ___ |
| 6460 | |
| 6461 | Flash a dashed box of chosen dimensions. |
| 6462 | ```python |
| 6463 | show_ctrl_outline( |
| 6464 | canvas: Literal["table"] = "table", |
| 6465 | start_cell: tuple[int, int] = (0, 0), |
| 6466 | end_cell: tuple[int, int] = (1, 1), |
| 6467 | ) -> Sheet |
| 6468 | ``` |
| 6469 | |
| 6470 | ___ |
| 6471 | |
| 6472 | Various functions related to the Sheets internal undo and redo stacks. |
| 6473 | ```python |
| 6474 | # clears both undos and redos |
| 6475 | reset_undos() -> Sheet |
| 6476 | |
| 6477 | # get the Sheets modifiable deque variables which store changes for undo and redo |
| 6478 | get_undo_stack() -> deque |
| 6479 | get_redo_stack() -> deque |
| 6480 | |
| 6481 | # set the Sheets undo and redo stacks, returns Sheet widget |
| 6482 | set_undo_stack(stack: deque) -> Sheet |
| 6483 | set_redo_stack(stack: deque) -> Sheet |
| 6484 | ``` |
| 6485 | |
| 6486 | ___ |
| 6487 | |
| 6488 | Refresh the table. |
| 6489 | ```python |
| 6490 | refresh(redraw_header: bool = True, redraw_row_index: bool = True) -> Sheet |
| 6491 | ``` |
| 6492 | |
| 6493 | ___ |
| 6494 | |
| 6495 | Refresh the table. |
| 6496 | ```python |
| 6497 | redraw(redraw_header: bool = True, redraw_row_index: bool = True) -> Sheet |
| 6498 | ``` |
| 6499 | |
| 6500 | --- |
| 6501 | # **Treeview Mode** |
| 6502 | |
| 6503 | tksheet has a treeview mode which behaves similarly to the ttk treeview widget, it is not a drop in replacement for it though. |
| 6504 | |
| 6505 | Always either use a fresh `Sheet()` instance or use [Sheet.reset()](#reset-all-or-specific-sheet-elements-and-attributes) before enabling treeview mode. |
| 6506 | |
| 6507 | ### **TO NOTE:** |
| 6508 | |
| 6509 | - When treeview mode is enabled the row index is a `list` of `Node` objects. The row index should not be modified by the usual `row_index()` function. |
| 6510 | - Most other tksheet functions should work as normal. |
| 6511 | - The index text alignment must be `"w"` aka west or left. |
| 6512 | |
| 6513 | ## **Creating a treeview mode sheet** |
| 6514 | |
| 6515 | You can make a treeview mode sheet by using the initialization parameter `treeview`: |
| 6516 | |
| 6517 | ```python |
| 6518 | sheet = Sheet(parent, treeview=True) |
| 6519 | ``` |
| 6520 | |
| 6521 | Or by using [`Sheet.reset()`](#reset-all-or-specific-sheet-elements-and-attributes) and [`Sheet.set_options()`](#sheet-options-and-other-functions). |
| 6522 | |
| 6523 | ```python |
| 6524 | my_sheet.reset() |
| 6525 | my_sheet.set_options(treeview=True) |
| 6526 | ``` |
| 6527 | |
| 6528 | See the other sections on sheet initialization and examples for the other usual `Sheet()` parameters. |
| 6529 | |
| 6530 | ## **Treeview mode functions** |
| 6531 | |
| 6532 | Functions designed for use with treeview mode. |
| 6533 | |
| 6534 | #### **Insert an item** |
| 6535 | |
| 6536 | ```python |
| 6537 | insert( |
| 6538 | parent: str = "", |
| 6539 | index: None | int | Literal["end"] = None, |
| 6540 | iid: None | str = None, |
| 6541 | text: None | str = None, |
| 6542 | values: None | list[object] = None, |
| 6543 | create_selections: bool = False, |
| 6544 | undo: bool = True, |
| 6545 | ) -> str |
| 6546 | ``` |
| 6547 | Parameters: |
| 6548 | |
| 6549 | - `parent` is the `iid` of the parent item (if any). If left as `""` then the item will not have a parent. |
| 6550 | - `index` is the row number for the item to be placed at, leave as `None` for the end. |
| 6551 | - `iid` is a new and unique item id. It will be generated automatically if left as `None`. |
| 6552 | - `text` is the displayed text in the row index for the item. |
| 6553 | - `values` is a list of values which will become the items row in the sheet. |
| 6554 | - `create_selections` when `True` selects the row that has just been created. |
| 6555 | - `undo` when `True` adds the change to the undo stack. |
| 6556 | |
| 6557 | Notes: |
| 6558 | |
| 6559 | - Returns the `iid`. |
| 6560 | |
| 6561 | Example: |
| 6562 | ```python |
| 6563 | sheet.insert( |
| 6564 | iid="top level", |
| 6565 | text="Top level", |
| 6566 | values=["cell A1", "cell B1"], |
| 6567 | ) |
| 6568 | sheet.insert( |
| 6569 | parent="top level", |
| 6570 | iid="mid level", |
| 6571 | text="Mid level", |
| 6572 | values=["cell A2", "cell B2"], |
| 6573 | ) |
| 6574 | ``` |
| 6575 | |
| 6576 | ___ |
| 6577 | |
| 6578 | #### **Insert multiple items** |
| 6579 | |
| 6580 | ```python |
| 6581 | bulk_insert( |
| 6582 | data: list[list[object]], |
| 6583 | parent: str = "", |
| 6584 | index: None | int | Literal["end"] = None, |
| 6585 | iid_column: int | None = None, |
| 6586 | text_column: int | None | str = None, |
| 6587 | create_selections: bool = False, |
| 6588 | include_iid_column: bool = True, |
| 6589 | include_text_column: bool = True, |
| 6590 | undo: bool = True, |
| 6591 | ) -> dict[str, int] |
| 6592 | ``` |
| 6593 | Parameters: |
| 6594 | |
| 6595 | - `parent` is the `iid` of the parent item (if any). If left as `""` then the items will not have a parent. |
| 6596 | - `index` is the row number for the items to be placed at, leave as `None` for the end. |
| 6597 | - `iid_column` if left as `None` iids will be automatically generated for the new items, else you can specify a column in the `data` which contains the iids. |
| 6598 | - `text_column`: |
| 6599 | - If left as `None` there will be no displayed text next to the items. |
| 6600 | - A text column can be provided in the data and `text_column` set to an `int` representing its index to provide the displayed text for the items. |
| 6601 | - Or if a `str` is used all items will have that `str` as their displayed text. |
| 6602 | - `create_selections` when `True` selects the row that has just been created. |
| 6603 | - `include_iid_column` when `False` excludes the iid column from the inserted rows. |
| 6604 | - `include_text_column` when the `text_column` is an `int` setting this to `False` excludes that column from the treeview. |
| 6605 | - `undo` when `True` adds the change to the undo stack. |
| 6606 | |
| 6607 | Notes: |
| 6608 | |
| 6609 | - Returns a `dict[str, int]` of key: new iids, value: their data row number. |
| 6610 | |
| 6611 | Example: |
| 6612 | ```python |
| 6613 | sheet.insert(iid="ID-1", text="ID-1", values=["ID 1 Value"]) |
| 6614 | sheet.bulk_insert( |
| 6615 | data=[ |
| 6616 | ["CID-1", "CID 1 Value"], |
| 6617 | ["CID-2", "CID 2 Value"], |
| 6618 | ], |
| 6619 | parent="ID-1", |
| 6620 | iid_column=0, |
| 6621 | text_column=0, |
| 6622 | include_iid_column=False, |
| 6623 | include_text_column=False, |
| 6624 | ) |
| 6625 | ``` |
| 6626 | |
| 6627 | ___ |
| 6628 | |
| 6629 | #### **Build a tree from data** |
| 6630 | |
| 6631 | This takes a list of lists where sublists are rows and a few arguments to bulk insert items into the treeview. **Note that**: |
| 6632 | |
| 6633 | - **It resets the sheet** so cannot be used to bulk add to an already existing treeview. |
| 6634 | |
| 6635 | ```python |
| 6636 | tree_build( |
| 6637 | data: list[list[object]], |
| 6638 | iid_column: int, |
| 6639 | parent_column: int, |
| 6640 | text_column: None | int = None, |
| 6641 | push_ops: bool = False, |
| 6642 | row_heights: Sequence[int] | None | False = None, |
| 6643 | open_ids: Iterator[str] | None = None, |
| 6644 | safety: bool = True, |
| 6645 | ncols: int | None = None, |
| 6646 | lower: bool = False, |
| 6647 | include_iid_column: bool = True, |
| 6648 | include_parent_column: bool = True, |
| 6649 | include_text_column: bool = True, |
| 6650 | ) -> Sheet |
| 6651 | ``` |
| 6652 | Parameters: |
| 6653 | |
| 6654 | - `data` a list of lists, one column must be an iid column, another must be a parent iid column. |
| 6655 | - `text_column` if an `int` is used then the values in that column will populate the row index. |
| 6656 | - `push_ops` when `True` the newly inserted rows will push all existing sheet options such as highlights downwards. |
| 6657 | - `row_heights` a `list` of `int`s can be used to provide the displayed row heights in pixels (does not include hidden items). Only use if you know what you're doing here. |
| 6658 | - `open_ids` a list of iids which will be opened. |
| 6659 | - `safety` when `True` checks for infinite loops, empty iid cells and duplicate iids. No error or warning will be generated. |
| 6660 | - In the case of infinite loops the parent iid cell will be cleared. |
| 6661 | - In the case of empty iid cells the row will be ignored. |
| 6662 | - In the case of duplicate iids they will be renamed and `"DUPLICATED_<number>"` will be attached to the end. |
| 6663 | - `ncols` is like maximum columns, an `int` which limits the number of columns that are included in the loaded data. |
| 6664 | - `lower` makes all item ids - iids lower case. |
| 6665 | - `include_iid_column` when `False` excludes the iid column from the inserted rows. |
| 6666 | - `include_parent_column` when `False` excludes the parent column from the inserted rows. |
| 6667 | - `include_text_column` when the `text_column` is an `int` setting this to `False` excludes that column from the treeview. |
| 6668 | |
| 6669 | Notes: |
| 6670 | |
| 6671 | - Returns the `Sheet` object. |
| 6672 | |
| 6673 | Example: |
| 6674 | ```python |
| 6675 | data = [ |
| 6676 | ["id1", "", "id1 val"], |
| 6677 | ["id2", "id1", "id2 val"], |
| 6678 | ] |
| 6679 | sheet.tree_build( |
| 6680 | data=data, |
| 6681 | iid_column=0, |
| 6682 | parent_column=1, |
| 6683 | include_iid_column=False, |
| 6684 | include_parent_column=False, |
| 6685 | ) |
| 6686 | ``` |
| 6687 | |
| 6688 | ___ |
| 6689 | |
| 6690 | #### **Reset the treeview** |
| 6691 | |
| 6692 | ```python |
| 6693 | tree_reset() -> Sheet |
| 6694 | ``` |
| 6695 | |
| 6696 | ___ |
| 6697 | |
| 6698 | #### **Get treeview iids that are open** |
| 6699 | |
| 6700 | ```python |
| 6701 | tree_get_open() -> set[str] |
| 6702 | ``` |
| 6703 | |
| 6704 | ___ |
| 6705 | |
| 6706 | #### **Set the open treeview iids** |
| 6707 | |
| 6708 | ```python |
| 6709 | tree_set_open(open_ids: Iterator[str]) -> Sheet |
| 6710 | ``` |
| 6711 | - Any other iids are closed as a result. |
| 6712 | |
| 6713 | ___ |
| 6714 | |
| 6715 | #### **Open treeview iids** |
| 6716 | |
| 6717 | ```python |
| 6718 | tree_open(*items, redraw: bool = True) -> Sheet |
| 6719 | ``` |
| 6720 | - Opens all given iids. |
| 6721 | |
| 6722 | ___ |
| 6723 | |
| 6724 | #### **Close treeview iids** |
| 6725 | |
| 6726 | ```python |
| 6727 | tree_close(*items, redraw: bool = True) -> Sheet |
| 6728 | ``` |
| 6729 | - Closes all given iids. |
| 6730 | |
| 6731 | ___ |
| 6732 | |
| 6733 | #### **Set or get an iids attributes** |
| 6734 | |
| 6735 | ```python |
| 6736 | item( |
| 6737 | item: str, |
| 6738 | iid: str | None = None, |
| 6739 | text: str | None = None, |
| 6740 | values: list | None = None, |
| 6741 | open_: bool | None = None, |
| 6742 | undo: bool = True, |
| 6743 | emit_event: bool = True, |
| 6744 | redraw: bool = True, |
| 6745 | ) -> DotDict | Sheet |
| 6746 | ``` |
| 6747 | Parameters: |
| 6748 | |
| 6749 | - `item` iid, required argument. |
| 6750 | - `iid` use a `str` to rename the iid. |
| 6751 | - `text` use a `str` to get the iid new display text in the row index. |
| 6752 | - `values` use a `list` of values to give the item a new row of values (does not include row index). |
| 6753 | - `open_` use a `bool` to set the item as open or closed. `False` is closed. |
| 6754 | - `undo` adds any changes to the undo stack. |
| 6755 | - `emit_event` emits a `<<SheetModified>>` event if changes were made. |
| 6756 | |
| 6757 | Notes: |
| 6758 | - If no arguments are given a `DotDict` is returned with the item attributes. |
| 6759 | |
| 6760 | ```python |
| 6761 | { |
| 6762 | "text": ..., |
| 6763 | "values": ..., |
| 6764 | "open_": ..., |
| 6765 | } |
| 6766 | ``` |
| 6767 | |
| 6768 | ___ |
| 6769 | |
| 6770 | #### **Get an iids row number** |
| 6771 | |
| 6772 | ```python |
| 6773 | itemrow(item: str) -> int |
| 6774 | ``` |
| 6775 | - Includes hidden rows in counting row numbers. |
| 6776 | |
| 6777 | ___ |
| 6778 | |
| 6779 | #### **Get a row numbers item** |
| 6780 | |
| 6781 | ```python |
| 6782 | rowitem(row: int, data_index: bool = False) -> str | None |
| 6783 | ``` |
| 6784 | - Includes hidden rows in counting row numbers. See [here](#hiding-rows) for more information. |
| 6785 | |
| 6786 | ___ |
| 6787 | |
| 6788 | #### **Get treeview children** |
| 6789 | |
| 6790 | ```python |
| 6791 | get_children(item: None | str = None) -> Generator[str] |
| 6792 | ``` |
| 6793 | - `item`: |
| 6794 | - When left as `None` will return all iids currently in treeview, including hidden rows. |
| 6795 | - Use an empty `str` (`""`) to get all top level iids in the treeview. |
| 6796 | - Use an iid to get the children for that particular iid. Does not include all descendants. |
| 6797 | |
| 6798 | ```python |
| 6799 | tree_traverse(item: None | str = None) -> Generator[str]: |
| 6800 | ``` |
| 6801 | - Exactly the same as above but instead of retrieving iids from the index list it gets top iids and uses traversal to iterate through descendants. |
| 6802 | |
| 6803 | ___ |
| 6804 | |
| 6805 | #### **Get item descendants** |
| 6806 | |
| 6807 | ```python |
| 6808 | descendants(item: str, check_open: bool = False) -> Generator[str]: |
| 6809 | ``` |
| 6810 | - Returns a generator which yields item ids in depth first order. |
| 6811 | |
| 6812 | ___ |
| 6813 | |
| 6814 | #### **Get the currently selected treeview item** |
| 6815 | |
| 6816 | ```python |
| 6817 | @property |
| 6818 | tree_selected() -> str | None: |
| 6819 | ``` |
| 6820 | - Returns the item id of the currently selected box row. If nothing is selected returns `None`. |
| 6821 | |
| 6822 | ___ |
| 6823 | |
| 6824 | #### **Delete treeview items** |
| 6825 | |
| 6826 | ```python |
| 6827 | del_items(*items) -> Sheet |
| 6828 | ``` |
| 6829 | - `*items` the iids of items to delete. |
| 6830 | - Also deletes all item descendants. |
| 6831 | |
| 6832 | ___ |
| 6833 | |
| 6834 | #### **Move items to a new parent** |
| 6835 | |
| 6836 | ```python |
| 6837 | set_children(parent: str, *newchildren) -> Sheet |
| 6838 | ``` |
| 6839 | - `parent` the new parent for the items. |
| 6840 | - `*newchildren` the items to move. |
| 6841 | |
| 6842 | ___ |
| 6843 | |
| 6844 | #### **Move an item to a new parent** |
| 6845 | |
| 6846 | ```python |
| 6847 | move( |
| 6848 | item: str, |
| 6849 | parent: str, |
| 6850 | index: int | None = None, |
| 6851 | select: bool = True, |
| 6852 | undo: bool = True, |
| 6853 | emit_event: bool = False, |
| 6854 | ) -> tuple[dict[int, int], dict[int, int], EventDataDict] |
| 6855 | ``` |
| 6856 | Parameters: |
| 6857 | |
| 6858 | - `item` is the iid to move. |
| 6859 | - `parent` is the new parent for the item. |
| 6860 | - Use an empty `str` (`""`) to move the item to the top. |
| 6861 | - `index`: |
| 6862 | - Leave as `None` to move to item to the end of the top/children. |
| 6863 | - Use an `int` to move the item to an index within its parents children (or within top level items if moving to the top). |
| 6864 | - `select` when `True` selects the moved rows. |
| 6865 | - `undo` when `True` adds the change to the undo stack. |
| 6866 | - `emit_event` when `True` emits events related to sheet modification. |
| 6867 | |
| 6868 | Notes: |
| 6869 | |
| 6870 | - Also moves all of the items descendants. |
| 6871 | - The `reattach()` function is exactly the same as `move()`. |
| 6872 | - Returns `dict[int, int]` of `{old index: new index, ...}` for data and displayed rows separately and also an `EventDataDict`. |
| 6873 | |
| 6874 | ___ |
| 6875 | |
| 6876 | #### **Check an item exists** |
| 6877 | |
| 6878 | ```python |
| 6879 | exists(item: str) -> bool |
| 6880 | ``` |
| 6881 | - `item` - a treeview iid. |
| 6882 | |
| 6883 | ___ |
| 6884 | |
| 6885 | #### **Get an items parent** |
| 6886 | |
| 6887 | ```python |
| 6888 | parent(item: str) -> str |
| 6889 | ``` |
| 6890 | - `item` - a treeview iid. |
| 6891 | |
| 6892 | ___ |
| 6893 | |
| 6894 | #### **Get an items index** |
| 6895 | |
| 6896 | ```python |
| 6897 | index(item: str) -> int |
| 6898 | ``` |
| 6899 | - `item` - a treeview iid. |
| 6900 | |
| 6901 | ___ |
| 6902 | |
| 6903 | #### **Check if an item is currently displayed** |
| 6904 | |
| 6905 | ```python |
| 6906 | item_displayed(item: str) -> bool |
| 6907 | ``` |
| 6908 | - `item` - a treeview iid. |
| 6909 | |
| 6910 | ___ |
| 6911 | |
| 6912 | #### **Display an item** |
| 6913 | |
| 6914 | Make sure an items parents are all open, does **not** scroll to the item. |
| 6915 | |
| 6916 | ```python |
| 6917 | display_item(item: str, redraw: bool = False) -> Sheet |
| 6918 | ``` |
| 6919 | - `item` - a treeview iid. |
| 6920 | |
| 6921 | ___ |
| 6922 | |
| 6923 | #### **Scroll to an item** |
| 6924 | |
| 6925 | - Make sure an items parents are all open and scrolls to the item. |
| 6926 | |
| 6927 | ```python |
| 6928 | scroll_to_item(item: str, redraw: bool = False) -> Sheet |
| 6929 | ``` |
| 6930 | - `item` - a treeview iid. |
| 6931 | |
| 6932 | ___ |
| 6933 | |
| 6934 | #### **Get currently selected items** |
| 6935 | |
| 6936 | ```python |
| 6937 | selection(cells: bool = False) -> list[str] |
| 6938 | ``` |
| 6939 | Notes: |
| 6940 | - Returns a list of selected iids (selected rows but as iids). |
| 6941 | |
| 6942 | Parameters: |
| 6943 | - `cells` when `True` any selected cells will also qualify as selected items. |
| 6944 | |
| 6945 | ___ |
| 6946 | |
| 6947 | #### **Set selected items** |
| 6948 | |
| 6949 | ```python |
| 6950 | selection_set(*items, run_binding: bool = True, redraw: bool = True) -> Sheet |
| 6951 | ``` |
| 6952 | - Sets selected rows (items). |
| 6953 | |
| 6954 | ___ |
| 6955 | |
| 6956 | #### **Add selected items** |
| 6957 | |
| 6958 | ```python |
| 6959 | selection_add(*items, run_binding: bool = True, redraw: bool = True) -> Sheet |
| 6960 | ``` |
| 6961 | |
| 6962 | ___ |
| 6963 | |
| 6964 | #### **Remove selections** |
| 6965 | |
| 6966 | ```python |
| 6967 | selection_remove(*items, redraw: bool = True) -> Sheet |
| 6968 | ``` |
| 6969 | |
| 6970 | ___ |
| 6971 | |
| 6972 | #### **Toggle selections** |
| 6973 | |
| 6974 | ```python |
| 6975 | selection_toggle(*items, redraw: bool = True) -> Sheet |
| 6976 | ``` |
| 6977 | |
| 6978 | --- |
| 6979 | # **Progress Bars** |
| 6980 | |
| 6981 | Progress bars can be created for individual cells. They will only update when tkinter updates. |
| 6982 | |
| 6983 | #### **Create a progress bar** |
| 6984 | |
| 6985 | ```python |
| 6986 | create_progress_bar( |
| 6987 | row: int, |
| 6988 | column: int, |
| 6989 | bg: str, |
| 6990 | fg: str, |
| 6991 | name: Hashable, |
| 6992 | percent: int = 0, |
| 6993 | del_when_done: bool = False, |
| 6994 | ) -> Sheet |
| 6995 | ``` |
| 6996 | - `row` the row coordinate to create the bar at. |
| 6997 | - `column` the column coordinate to create the bar at. |
| 6998 | - `bg` the background color for the bar. |
| 6999 | - `fg` the text color for the bar. |
| 7000 | - `name` a name is required for easy referral to the bar later on. |
| 7001 | - Names can be re-used for multiple bars. |
| 7002 | - `percent` the starting progress of the bar as an `int` either `0`, `100` or a number in between. |
| 7003 | - `del_when_done` if `True` the `Sheet` will automatically delete the progress bar once it is modified with a percent of `100` or more. |
| 7004 | |
| 7005 | ___ |
| 7006 | |
| 7007 | #### **Modify progress bars** |
| 7008 | |
| 7009 | ```python |
| 7010 | progress_bar( |
| 7011 | name: Hashable | None = None, |
| 7012 | cell: tuple[int, int] | None = None, |
| 7013 | percent: int | None = None, |
| 7014 | bg: str | None = None, |
| 7015 | fg: str | None = None, |
| 7016 | ) -> Sheet |
| 7017 | ``` |
| 7018 | Either `name` or `cell` can be used to refer to existing progress bars: |
| 7019 | |
| 7020 | - `name` the name given to a progress bar, or multiple progress bars. |
| 7021 | - If this parameter is used then `cell` will not be used. |
| 7022 | - Will modify all progress bars with the given name. |
| 7023 | - `cell` (`tuple[int, int]`) a tuple of two `int`s representing the progress bars location, `(row, column)`. |
| 7024 | - Can only refer to one progress bar. |
| 7025 | |
| 7026 | Values that can be modified: |
| 7027 | |
| 7028 | - `bg` the background color for the bar, leave as `None` for no change. |
| 7029 | - `fg` the text color for the bar, leave as `None` for no change. |
| 7030 | - `percent` the progress of the bar as an `int` either `0`, `100` or a number in between, leave as `None` for no change. |
| 7031 | |
| 7032 | ___ |
| 7033 | |
| 7034 | #### **Delete progress bars** |
| 7035 | |
| 7036 | Note that this will delete the progress bars data from the Sheet as well. |
| 7037 | |
| 7038 | ```python |
| 7039 | del_progress_bar( |
| 7040 | name: Hashable | None = None, |
| 7041 | cell: tuple[int, int] | None = None, |
| 7042 | ) -> Sheet |
| 7043 | ``` |
| 7044 | Either `name` or `cell` can be used to refer to existing progress bars: |
| 7045 | |
| 7046 | - `name` the name given to a progress bar, or multiple progress bars. |
| 7047 | - Will delete all progress bars with the given name. |
| 7048 | - If this parameter is used then `cell` will not be used. |
| 7049 | - `cell` (`tuple[int, int]`) a tuple of two `int`s representing the progress bars location, `(row, column)`. |
| 7050 | - Can only refer to one progress bar. |
| 7051 | |
| 7052 | --- |
| 7053 | # **Tags** |
| 7054 | |
| 7055 | Tags can be used to keep track of specific cells, rows and columns wherever they move. Note that: |
| 7056 | |
| 7057 | - If rows/columns are deleted the the associated tags will be also. |
| 7058 | - There is no equivalent `tag_bind` functionality at this time. |
| 7059 | - All tagging functions use data indexes (not displayed indexes) - this is only relevant when there are hidden rows/columns. |
| 7060 | |
| 7061 | #### **Tag a specific cell** |
| 7062 | |
| 7063 | ```python |
| 7064 | tag_cell( |
| 7065 | cell: tuple[int, int], |
| 7066 | *tags, |
| 7067 | ) -> Sheet |
| 7068 | ``` |
| 7069 | Example: |
| 7070 | ```python |
| 7071 | sheet.tag_cell((0, 0), "tag a1", "tag a1 no.2") |
| 7072 | ``` |
| 7073 | |
| 7074 | ___ |
| 7075 | |
| 7076 | #### **Tag specific rows** |
| 7077 | |
| 7078 | ```python |
| 7079 | tag_rows( |
| 7080 | rows: int | Iterator[int], |
| 7081 | *tags, |
| 7082 | ) -> Sheet |
| 7083 | ``` |
| 7084 | |
| 7085 | ___ |
| 7086 | |
| 7087 | #### **Tag specific columns** |
| 7088 | |
| 7089 | ```python |
| 7090 | tag_columns( |
| 7091 | columns: int | Iterator[int], |
| 7092 | *tags, |
| 7093 | ) -> Sheet |
| 7094 | ``` |
| 7095 | |
| 7096 | ___ |
| 7097 | |
| 7098 | #### **Tag using a span** |
| 7099 | |
| 7100 | ```python |
| 7101 | tag( |
| 7102 | *key: CreateSpanTypes, |
| 7103 | tags: Iterator[str] | str = "", |
| 7104 | ) -> Sheet |
| 7105 | ``` |
| 7106 | |
| 7107 | ___ |
| 7108 | |
| 7109 | #### **Untag** |
| 7110 | |
| 7111 | ```python |
| 7112 | untag( |
| 7113 | cell: tuple[int, int] | None = None, |
| 7114 | rows: int | Iterator[int] | None = None, |
| 7115 | columns: int | Iterator[int] | None = None, |
| 7116 | ) -> Sheet |
| 7117 | ``` |
| 7118 | - This removes all tags from the cell, rows or columns provided. |
| 7119 | |
| 7120 | ___ |
| 7121 | |
| 7122 | #### **Delete tags** |
| 7123 | |
| 7124 | ```python |
| 7125 | tag_del( |
| 7126 | *tags, |
| 7127 | cells: bool = True, |
| 7128 | rows: bool = True, |
| 7129 | columns: bool = True, |
| 7130 | ) -> Sheet |
| 7131 | ``` |
| 7132 | - This deletes the provided tags from all `cells` if `True`, `rows` if `True` and `columns` if `True`. |
| 7133 | |
| 7134 | ___ |
| 7135 | |
| 7136 | #### **Get cells, rows or columns associated with tags** |
| 7137 | |
| 7138 | ```python |
| 7139 | tag_has( |
| 7140 | *tags, |
| 7141 | ) -> DotDict |
| 7142 | ``` |
| 7143 | Notes: |
| 7144 | |
| 7145 | - Returns all cells, rows and columns associated with **any** of the provided tags in the form of a `dict` with dot notation accessbility which has the following keys: |
| 7146 | - `"cells"` - with a value of `set[tuple[int, int]]` where the `tuple`s are cell coordinates - `(row, column)`. |
| 7147 | - `"rows"` - with a value of `set[int]` where the `int`s are rows. |
| 7148 | - `"columns"` - with a value of `set[int]` where the `int`s are columns. |
| 7149 | - Returns data indexes. |
| 7150 | - This function **updates** the `set`s with any cells/rows/columns associated with each tag, it does **not** return cells/rows/columns that have all the provided tags. |
| 7151 | |
| 7152 | Example: |
| 7153 | ```python |
| 7154 | sheet.tag_rows((0, 1), "row tag a", "row tag b") |
| 7155 | sheet.tag_rows(4, "row tag b") |
| 7156 | sheet.tag_rows(5, "row tag c") |
| 7157 | sheet.tag_rows(6, "row tag d") |
| 7158 | with_tags = sheet.tag_has("row tag b", "row tag c") |
| 7159 | |
| 7160 | print (with_tags.rows) |
| 7161 | # prints {0, 1, 4, 5} |
| 7162 | ``` |
| 7163 | |
| 7164 | --- |
| 7165 | # **Example Loading Data from Excel** |
| 7166 | |
| 7167 | Using `pandas` library, requires additional libraries: |
| 7168 | - `pandas` |
| 7169 | - `openpyxl` |
| 7170 | ```python |
| 7171 | from tksheet import Sheet |
| 7172 | import tkinter as tk |
| 7173 | import pandas as pd |
| 7174 | |
| 7175 | |
| 7176 | class demo(tk.Tk): |
| 7177 | def __init__(self): |
| 7178 | tk.Tk.__init__(self) |
| 7179 | self.grid_columnconfigure(0, weight = 1) |
| 7180 | self.grid_rowconfigure(0, weight = 1) |
| 7181 | self.frame = tk.Frame(self) |
| 7182 | self.frame.grid_columnconfigure(0, weight = 1) |
| 7183 | self.frame.grid_rowconfigure(0, weight = 1) |
| 7184 | self.sheet = Sheet(self.frame, |
| 7185 | data = pd.read_excel("excel_file.xlsx", # filepath here |
| 7186 | #sheet_name = "sheet1", # optional sheet name here |
| 7187 | engine = "openpyxl", |
| 7188 | header = None).values.tolist()) |
| 7189 | self.sheet.enable_bindings() |
| 7190 | self.frame.grid(row = 0, column = 0, sticky = "nswe") |
| 7191 | self.sheet.grid(row = 0, column = 0, sticky = "nswe") |
| 7192 | |
| 7193 | |
| 7194 | app = demo() |
| 7195 | app.mainloop() |
| 7196 | ``` |
| 7197 | |
| 7198 | --- |
| 7199 | # **Example Custom Right Click and Text Editor Validation** |
| 7200 | |
| 7201 | This is to demonstrate: |
| 7202 | |
| 7203 | - Adding your own commands to the in-built right click popup menu (or how you might start making your own right click menu functionality) |
| 7204 | - Validating text editor input; in this demonstration the validation removes spaces from user input. |
| 7205 | |
| 7206 | ```python |
| 7207 | from tksheet import Sheet |
| 7208 | import tkinter as tk |
| 7209 | |
| 7210 | |
| 7211 | class demo(tk.Tk): |
| 7212 | def __init__(self): |
| 7213 | tk.Tk.__init__(self) |
| 7214 | self.grid_columnconfigure(0, weight=1) |
| 7215 | self.grid_rowconfigure(0, weight=1) |
| 7216 | self.frame = tk.Frame(self) |
| 7217 | self.frame.grid_columnconfigure(0, weight=1) |
| 7218 | self.frame.grid_rowconfigure(0, weight=1) |
| 7219 | self.sheet = Sheet(self.frame, |
| 7220 | data=[[f"Row {r}, Column {c}\nnewline1\nnewline2" for c in range(50)] for r in range(500)]) |
| 7221 | self.sheet.enable_bindings( |
| 7222 | "single_select", |
| 7223 | "drag_select", |
| 7224 | "edit_cell", |
| 7225 | "paste", |
| 7226 | "cut", |
| 7227 | "copy", |
| 7228 | "delete", |
| 7229 | "select_all", |
| 7230 | "column_select", |
| 7231 | "row_select", |
| 7232 | "column_width_resize", |
| 7233 | "double_click_column_resize", |
| 7234 | "arrowkeys", |
| 7235 | "row_height_resize", |
| 7236 | "double_click_row_resize", |
| 7237 | "right_click_popup_menu", |
| 7238 | "rc_select", |
| 7239 | ) |
| 7240 | self.sheet.extra_bindings("begin_edit_cell", self.begin_edit_cell) |
| 7241 | self.sheet.edit_validation(self.validate_edits) |
| 7242 | self.sheet.popup_menu_add_command("Say Hello", self.new_right_click_button) |
| 7243 | self.frame.grid(row=0, column=0, sticky="nswe") |
| 7244 | self.sheet.grid(row=0, column=0, sticky="nswe") |
| 7245 | |
| 7246 | def new_right_click_button(self, event=None): |
| 7247 | print ("Hello World!") |
| 7248 | |
| 7249 | def begin_edit_cell(self, event=None): |
| 7250 | return event.value |
| 7251 | |
| 7252 | def validate_edits(self, event): |
| 7253 | # remove spaces from any cell edits, including paste |
| 7254 | if isinstance(event.value, str) and event.value: |
| 7255 | return event.value.replace(" ", "") |
| 7256 | |
| 7257 | |
| 7258 | app = demo() |
| 7259 | app.mainloop() |
| 7260 | ``` |
| 7261 | - If you want a totally new right click menu you can use `self.sheet.bind("<3>", <function>)` with a `tk.Menu` of your own design (right click is `<2>` on MacOS) and don't use `"right_click_popup_menu"` with `enable_bindings()`. |
| 7262 | |
| 7263 | --- |
| 7264 | # **Example Displaying Selections** |
| 7265 | |
| 7266 | ```python |
| 7267 | from tksheet import ( |
| 7268 | Sheet, |
| 7269 | num2alpha, |
| 7270 | ) |
| 7271 | import tkinter as tk |
| 7272 | |
| 7273 | |
| 7274 | class demo(tk.Tk): |
| 7275 | def __init__(self): |
| 7276 | tk.Tk.__init__(self) |
| 7277 | self.grid_columnconfigure(0, weight = 1) |
| 7278 | self.grid_rowconfigure(0, weight = 1) |
| 7279 | self.frame = tk.Frame(self) |
| 7280 | self.frame.grid_columnconfigure(0, weight = 1) |
| 7281 | self.frame.grid_rowconfigure(0, weight = 1) |
| 7282 | self.sheet = Sheet(self.frame, |
| 7283 | data = [[f"Row {r}, Column {c}\nnewline1\nnewline2" for c in range(50)] for r in range(500)]) |
| 7284 | self.sheet.enable_bindings("all", "ctrl_select") |
| 7285 | self.sheet.extra_bindings([("all_select_events", self.sheet_select_event)]) |
| 7286 | self.show_selections = tk.Label(self) |
| 7287 | self.frame.grid(row = 0, column = 0, sticky = "nswe") |
| 7288 | self.sheet.grid(row = 0, column = 0, sticky = "nswe") |
| 7289 | self.show_selections.grid(row = 1, column = 0, sticky = "nsw") |
| 7290 | |
| 7291 | def sheet_select_event(self, event = None): |
| 7292 | if event.eventname == "select" and event.selection_boxes and event.selected: |
| 7293 | # get the most recently selected box in case there are multiple |
| 7294 | box = next(reversed(event.selection_boxes)) |
| 7295 | type_ = event.selection_boxes[box] |
| 7296 | if type_ == "cells": |
| 7297 | self.show_selections.config(text=f"{type_.capitalize()}: {box.from_r + 1},{box.from_c + 1} : {box.upto_r},{box.upto_c}") |
| 7298 | elif type_ == "rows": |
| 7299 | self.show_selections.config(text=f"{type_.capitalize()}: {box.from_r + 1} : {box.upto_r}") |
| 7300 | elif type_ == "columns": |
| 7301 | self.show_selections.config(text=f"{type_.capitalize()}: {num2alpha(box.from_c)} : {num2alpha(box.upto_c - 1)}") |
| 7302 | else: |
| 7303 | self.show_selections.config(text="") |
| 7304 | |
| 7305 | |
| 7306 | app = demo() |
| 7307 | app.mainloop() |
| 7308 | ``` |
| 7309 | |
| 7310 | --- |
| 7311 | # **Example List Box** |
| 7312 | |
| 7313 | This is to demonstrate some simple customization to make a different sort of widget (a list box). |
| 7314 | |
| 7315 | ```python |
| 7316 | from tksheet import Sheet |
| 7317 | import tkinter as tk |
| 7318 | |
| 7319 | class Sheet_Listbox(Sheet): |
| 7320 | def __init__(self, |
| 7321 | parent, |
| 7322 | values = []): |
| 7323 | Sheet.__init__(self, |
| 7324 | parent = parent, |
| 7325 | show_horizontal_grid = False, |
| 7326 | show_vertical_grid = False, |
| 7327 | show_header = False, |
| 7328 | show_row_index = False, |
| 7329 | show_top_left = False, |
| 7330 | empty_horizontal = 0, |
| 7331 | empty_vertical = 0) |
| 7332 | if values: |
| 7333 | self.values(values) |
| 7334 | |
| 7335 | def values(self, values = []): |
| 7336 | self.set_sheet_data([[v] for v in values], |
| 7337 | reset_col_positions = False, |
| 7338 | reset_row_positions = False, |
| 7339 | redraw = False, |
| 7340 | verify = False) |
| 7341 | self.set_all_cell_sizes_to_text() |
| 7342 | |
| 7343 | |
| 7344 | class demo(tk.Tk): |
| 7345 | def __init__(self): |
| 7346 | tk.Tk.__init__(self) |
| 7347 | self.grid_columnconfigure(0, |
| 7348 | weight = 1) |
| 7349 | self.grid_rowconfigure(0, |
| 7350 | weight = 1) |
| 7351 | self.listbox = Sheet_Listbox(self, |
| 7352 | values = [f"_________ Item {i} _________" for i in range(2000)]) |
| 7353 | self.listbox.grid(row = 0, |
| 7354 | column = 0, |
| 7355 | sticky = "nswe") |
| 7356 | #self.listbox.values([f"new values {i}" for i in range(50)]) set values |
| 7357 | |
| 7358 | |
| 7359 | app = demo() |
| 7360 | app.mainloop() |
| 7361 | ``` |
| 7362 | |
| 7363 | --- |
| 7364 | # **Example Header Dropdown Boxes and Row Filtering** |
| 7365 | |
| 7366 | A very simple demonstration of row filtering using header dropdown boxes. |
| 7367 | |
| 7368 | ```python |
| 7369 | from tksheet import ( |
| 7370 | Sheet, |
| 7371 | num2alpha as n2a, |
| 7372 | ) |
| 7373 | import tkinter as tk |
| 7374 | |
| 7375 | |
| 7376 | class demo(tk.Tk): |
| 7377 | def __init__(self): |
| 7378 | tk.Tk.__init__(self) |
| 7379 | self.grid_columnconfigure(0, weight=1) |
| 7380 | self.grid_rowconfigure(0, weight=1) |
| 7381 | self.frame = tk.Frame(self) |
| 7382 | self.frame.grid_columnconfigure(0, weight=1) |
| 7383 | self.frame.grid_rowconfigure(0, weight=1) |
| 7384 | self.data = [ |
| 7385 | ["3", "c", "z"], |
| 7386 | ["1", "a", "x"], |
| 7387 | ["1", "b", "y"], |
| 7388 | ["2", "b", "y"], |
| 7389 | ["2", "c", "z"], |
| 7390 | ] |
| 7391 | self.sheet = Sheet( |
| 7392 | self.frame, |
| 7393 | data=self.data, |
| 7394 | column_width=180, |
| 7395 | theme="dark", |
| 7396 | height=700, |
| 7397 | width=1100, |
| 7398 | ) |
| 7399 | self.sheet.enable_bindings( |
| 7400 | "copy", |
| 7401 | "rc_select", |
| 7402 | "arrowkeys", |
| 7403 | "double_click_column_resize", |
| 7404 | "column_width_resize", |
| 7405 | "column_select", |
| 7406 | "row_select", |
| 7407 | "drag_select", |
| 7408 | "single_select", |
| 7409 | "select_all", |
| 7410 | ) |
| 7411 | self.frame.grid(row=0, column=0, sticky="nswe") |
| 7412 | self.sheet.grid(row=0, column=0, sticky="nswe") |
| 7413 | |
| 7414 | self.sheet.dropdown( |
| 7415 | self.sheet.span(n2a(0), header=True, table=False), |
| 7416 | values=["all", "1", "2", "3"], |
| 7417 | set_value="all", |
| 7418 | selection_function=self.header_dropdown_selected, |
| 7419 | text="Header A Name", |
| 7420 | ) |
| 7421 | self.sheet.dropdown( |
| 7422 | self.sheet.span(n2a(1), header=True, table=False), |
| 7423 | values=["all", "a", "b", "c"], |
| 7424 | set_value="all", |
| 7425 | selection_function=self.header_dropdown_selected, |
| 7426 | text="Header B Name", |
| 7427 | ) |
| 7428 | self.sheet.dropdown( |
| 7429 | self.sheet.span(n2a(2), header=True, table=False), |
| 7430 | values=["all", "x", "y", "z"], |
| 7431 | set_value="all", |
| 7432 | selection_function=self.header_dropdown_selected, |
| 7433 | text="Header C Name", |
| 7434 | ) |
| 7435 | |
| 7436 | def header_dropdown_selected(self, event=None): |
| 7437 | hdrs = self.sheet.headers() |
| 7438 | # this function is run before header cell data is set by dropdown selection |
| 7439 | # so we have to get the new value from the event |
| 7440 | hdrs[event.loc] = event.value |
| 7441 | if all(dd == "all" for dd in hdrs): |
| 7442 | self.sheet.display_rows("all") |
| 7443 | else: |
| 7444 | rows = [ |
| 7445 | rn for rn, row in enumerate(self.data) if all(row[c] == e or e == "all" for c, e in enumerate(hdrs)) |
| 7446 | ] |
| 7447 | self.sheet.display_rows(rows=rows, all_displayed=False) |
| 7448 | self.sheet.redraw() |
| 7449 | |
| 7450 | |
| 7451 | app = demo() |
| 7452 | app.mainloop() |
| 7453 | ``` |
| 7454 | |
| 7455 | --- |
| 7456 | # **Example Readme Screenshot Code** |
| 7457 | |
| 7458 | The code used to make a screenshot for the readme file. |
| 7459 | |
| 7460 | ```python |
| 7461 | from tksheet import ( |
| 7462 | Sheet, |
| 7463 | num2alpha as n2a, |
| 7464 | ) |
| 7465 | import tkinter as tk |
| 7466 | |
| 7467 | |
| 7468 | class demo(tk.Tk): |
| 7469 | def __init__(self): |
| 7470 | tk.Tk.__init__(self) |
| 7471 | self.grid_columnconfigure(0, weight=1) |
| 7472 | self.grid_rowconfigure(0, weight=1) |
| 7473 | self.frame = tk.Frame(self) |
| 7474 | self.frame.grid_columnconfigure(0, weight=1) |
| 7475 | self.frame.grid_rowconfigure(0, weight=1) |
| 7476 | self.sheet = Sheet( |
| 7477 | self.frame, |
| 7478 | empty_horizontal=0, |
| 7479 | empty_vertical=0, |
| 7480 | paste_can_expand_x=True, |
| 7481 | paste_can_expand_y=True, |
| 7482 | align="w", |
| 7483 | header_align="c", |
| 7484 | data=[[f"Row {r}, Column {c}\nnewline 1\nnewline 2" for c in range(6)] for r in range(21)], |
| 7485 | headers=[ |
| 7486 | "Dropdown Column", |
| 7487 | "Checkbox Column", |
| 7488 | "Center Aligned Column", |
| 7489 | "East Aligned Column", |
| 7490 | "", |
| 7491 | "", |
| 7492 | ], |
| 7493 | theme="dark", |
| 7494 | height=520, |
| 7495 | width=930, |
| 7496 | ) |
| 7497 | self.sheet.enable_bindings("all", "edit_index", "edit_header") |
| 7498 | self.sheet.popup_menu_add_command( |
| 7499 | "Hide Rows", |
| 7500 | self.hide_rows, |
| 7501 | table_menu=False, |
| 7502 | header_menu=False, |
| 7503 | empty_space_menu=False, |
| 7504 | ) |
| 7505 | self.sheet.popup_menu_add_command( |
| 7506 | "Show All Rows", |
| 7507 | self.show_rows, |
| 7508 | table_menu=False, |
| 7509 | header_menu=False, |
| 7510 | empty_space_menu=False, |
| 7511 | ) |
| 7512 | self.sheet.popup_menu_add_command( |
| 7513 | "Hide Columns", |
| 7514 | self.hide_columns, |
| 7515 | table_menu=False, |
| 7516 | index_menu=False, |
| 7517 | empty_space_menu=False, |
| 7518 | ) |
| 7519 | self.sheet.popup_menu_add_command( |
| 7520 | "Show All Columns", |
| 7521 | self.show_columns, |
| 7522 | table_menu=False, |
| 7523 | index_menu=False, |
| 7524 | empty_space_menu=False, |
| 7525 | ) |
| 7526 | self.frame.grid(row=0, column=0, sticky="nswe") |
| 7527 | self.sheet.grid(row=0, column=0, sticky="nswe") |
| 7528 | colors = ( |
| 7529 | "#509f56", |
| 7530 | "#64a85b", |
| 7531 | "#78b160", |
| 7532 | "#8cba66", |
| 7533 | "#a0c36c", |
| 7534 | "#b4cc71", |
| 7535 | "#c8d576", |
| 7536 | "#dcde7c", |
| 7537 | "#f0e782", |
| 7538 | "#ffec87", |
| 7539 | "#ffe182", |
| 7540 | "#ffdc7d", |
| 7541 | "#ffd77b", |
| 7542 | "#ffc873", |
| 7543 | "#ffb469", |
| 7544 | "#fea05f", |
| 7545 | "#fc8c55", |
| 7546 | "#fb784b", |
| 7547 | "#fa6441", |
| 7548 | "#f85037", |
| 7549 | ) |
| 7550 | self.sheet.align_columns(columns=2, align="c") |
| 7551 | self.sheet.align_columns(columns=3, align="e") |
| 7552 | self.sheet.create_index_dropdown(r=0, values=["Dropdown"] + [f"{i}" for i in range(15)]) |
| 7553 | self.sheet.create_index_checkbox(r=3, checked=True, text="Checkbox") |
| 7554 | self.sheet.create_dropdown(r="all", c=0, values=["Dropdown"] + [f"{i}" for i in range(15)]) |
| 7555 | self.sheet.create_checkbox(r="all", c=1, checked=True, text="Checkbox") |
| 7556 | self.sheet.create_header_dropdown(c=0, values=["Header Dropdown"] + [f"{i}" for i in range(15)]) |
| 7557 | self.sheet.create_header_checkbox(c=1, checked=True, text="Header Checkbox") |
| 7558 | self.sheet.align_cells(5, 0, align="c") |
| 7559 | self.sheet.highlight_cells(5, 0, bg="gray50", fg="blue") |
| 7560 | self.sheet.highlight_cells(17, canvas="index", bg="yellow", fg="black") |
| 7561 | self.sheet.highlight_cells(12, 1, bg="gray90", fg="purple") |
| 7562 | for r in range(len(colors)): |
| 7563 | self.sheet.highlight_cells(row=r, column=3, fg=colors[r]) |
| 7564 | self.sheet.highlight_cells(row=r, column=4, bg=colors[r], fg="black") |
| 7565 | self.sheet.highlight_cells(row=r, column=5, bg=colors[r], fg="purple") |
| 7566 | self.sheet.highlight_cells(column=5, canvas="header", bg="white", fg="purple") |
| 7567 | self.sheet.align(n2a(2), align="c") |
| 7568 | self.sheet.align(n2a(3), align="e") |
| 7569 | self.sheet.dropdown( |
| 7570 | self.sheet.span("A", header=True), |
| 7571 | values=["Dropdown"] + [f"{i}" for i in range(15)], |
| 7572 | ) |
| 7573 | self.sheet.checkbox( |
| 7574 | self.sheet.span("B", header=True), |
| 7575 | checked=True, |
| 7576 | text="Checkbox", |
| 7577 | ) |
| 7578 | self.sheet.align(5, 0, align="c") |
| 7579 | self.sheet.highlight(5, 0, bg="gray50", fg="blue") |
| 7580 | self.sheet.highlight( |
| 7581 | self.sheet.span(17, index=True, table=False), |
| 7582 | bg="yellow", |
| 7583 | fg="black", |
| 7584 | ) |
| 7585 | self.sheet.highlight(12, 1, bg="gray90", fg="purple") |
| 7586 | for r in range(len(colors)): |
| 7587 | self.sheet.highlight(r, 3, fg=colors[r]) |
| 7588 | self.sheet.highlight(r, 4, bg=colors[r], fg="black") |
| 7589 | self.sheet.highlight(r, 5, bg=colors[r], fg="purple") |
| 7590 | self.sheet.highlight( |
| 7591 | self.sheet.span(n2a(5), header=True, table=False), |
| 7592 | bg="white", |
| 7593 | fg="purple", |
| 7594 | ) |
| 7595 | self.sheet.set_all_column_widths() |
| 7596 | self.sheet.extra_bindings("all", self.all_extra_bindings) |
| 7597 | |
| 7598 | def hide_rows(self, event=None): |
| 7599 | rows = self.sheet.get_selected_rows() |
| 7600 | if rows: |
| 7601 | self.sheet.hide_rows(rows) |
| 7602 | |
| 7603 | def show_rows(self, event=None): |
| 7604 | self.sheet.display_rows("all", redraw=True) |
| 7605 | |
| 7606 | def hide_columns(self, event=None): |
| 7607 | columns = self.sheet.get_selected_columns() |
| 7608 | if columns: |
| 7609 | self.sheet.hide_columns(columns) |
| 7610 | |
| 7611 | def show_columns(self, event=None): |
| 7612 | self.sheet.display_columns("all", redraw=True) |
| 7613 | |
| 7614 | def all_extra_bindings(self, event=None): |
| 7615 | return event.value |
| 7616 | |
| 7617 | |
| 7618 | app = demo() |
| 7619 | app.mainloop() |
| 7620 | ``` |
| 7621 | |
| 7622 | --- |
| 7623 | # **Example Saving tksheet as a csv File** |
| 7624 | |
| 7625 | To both load a csv file and save tksheet data as a csv file not including headers and index. |
| 7626 | |
| 7627 | ```python |
| 7628 | from tksheet import Sheet |
| 7629 | import tkinter as tk |
| 7630 | from tkinter import filedialog |
| 7631 | import csv |
| 7632 | from os.path import normpath |
| 7633 | import io |
| 7634 | |
| 7635 | |
| 7636 | class demo(tk.Tk): |
| 7637 | def __init__(self): |
| 7638 | tk.Tk.__init__(self) |
| 7639 | self.withdraw() |
| 7640 | self.title("tksheet") |
| 7641 | self.grid_columnconfigure(0, weight=1) |
| 7642 | self.grid_rowconfigure(0, weight=1) |
| 7643 | self.frame = tk.Frame(self) |
| 7644 | self.frame.grid_columnconfigure(0, weight=1) |
| 7645 | self.frame.grid_rowconfigure(0, weight=1) |
| 7646 | self.sheet = Sheet(self.frame, data=[[f"Row {r}, Column {c}" for c in range(6)] for r in range(21)]) |
| 7647 | self.sheet.enable_bindings("all", "edit_header", "edit_index") |
| 7648 | self.frame.grid(row=0, column=0, sticky="nswe") |
| 7649 | self.sheet.grid(row=0, column=0, sticky="nswe") |
| 7650 | self.sheet.popup_menu_add_command("Open csv", self.open_csv) |
| 7651 | self.sheet.popup_menu_add_command("Save sheet", self.save_sheet) |
| 7652 | self.sheet.set_all_cell_sizes_to_text() |
| 7653 | self.sheet.change_theme("light green") |
| 7654 | |
| 7655 | # create a span which encompasses the table, header and index |
| 7656 | # all data values, no displayed values |
| 7657 | self.sheet_span = self.sheet.span( |
| 7658 | header=True, |
| 7659 | index=True, |
| 7660 | hdisp=False, |
| 7661 | idisp=False, |
| 7662 | ) |
| 7663 | |
| 7664 | # center the window and unhide |
| 7665 | self.update_idletasks() |
| 7666 | w = self.winfo_screenwidth() - 20 |
| 7667 | h = self.winfo_screenheight() - 70 |
| 7668 | size = (900, 500) |
| 7669 | self.geometry("%dx%d+%d+%d" % (size + ((w / 2 - size[0] / 2), h / 2 - size[1] / 2))) |
| 7670 | self.deiconify() |
| 7671 | |
| 7672 | def save_sheet(self): |
| 7673 | filepath = filedialog.asksaveasfilename( |
| 7674 | parent=self, |
| 7675 | title="Save sheet as", |
| 7676 | filetypes=[("CSV File", ".csv"), ("TSV File", ".tsv")], |
| 7677 | defaultextension=".csv", |
| 7678 | confirmoverwrite=True, |
| 7679 | ) |
| 7680 | if not filepath or not filepath.lower().endswith((".csv", ".tsv")): |
| 7681 | return |
| 7682 | try: |
| 7683 | with open(normpath(filepath), "w", newline="", encoding="utf-8") as fh: |
| 7684 | writer = csv.writer( |
| 7685 | fh, |
| 7686 | dialect=csv.excel if filepath.lower().endswith(".csv") else csv.excel_tab, |
| 7687 | lineterminator="\n", |
| 7688 | ) |
| 7689 | writer.writerows(self.sheet_span.data) |
| 7690 | except Exception as error: |
| 7691 | print(error) |
| 7692 | return |
| 7693 | |
| 7694 | def open_csv(self): |
| 7695 | filepath = filedialog.askopenfilename(parent=self, title="Select a csv file") |
| 7696 | if not filepath or not filepath.lower().endswith((".csv", ".tsv")): |
| 7697 | return |
| 7698 | try: |
| 7699 | with open(normpath(filepath), "r") as filehandle: |
| 7700 | filedata = filehandle.read() |
| 7701 | self.sheet.reset() |
| 7702 | self.sheet_span.data = [ |
| 7703 | r |
| 7704 | for r in csv.reader( |
| 7705 | io.StringIO(filedata), |
| 7706 | dialect=csv.Sniffer().sniff(filedata), |
| 7707 | skipinitialspace=False, |
| 7708 | ) |
| 7709 | ] |
| 7710 | except Exception as error: |
| 7711 | print(error) |
| 7712 | return |
| 7713 | |
| 7714 | |
| 7715 | app = demo() |
| 7716 | app.mainloop() |
| 7717 | |
| 7718 | ``` |
| 7719 | |
| 7720 | --- |
| 7721 | # **Example Using and Creating Formatters** |
| 7722 | |
| 7723 | ```python |
| 7724 | from tksheet import ( |
| 7725 | Sheet, |
| 7726 | formatter, |
| 7727 | float_formatter, |
| 7728 | int_formatter, |
| 7729 | percentage_formatter, |
| 7730 | bool_formatter, |
| 7731 | truthy, |
| 7732 | falsy, |
| 7733 | num2alpha, |
| 7734 | ) |
| 7735 | import tkinter as tk |
| 7736 | from datetime import datetime, date |
| 7737 | from dateutil import parser, tz |
| 7738 | from math import ceil |
| 7739 | import re |
| 7740 | |
| 7741 | date_replace = re.compile("|".join(re.escape(char) for char in "()[]<>")) |
| 7742 | |
| 7743 | |
| 7744 | # Custom formatter methods |
| 7745 | def round_up(x): |
| 7746 | try: # might not be a number if empty |
| 7747 | return float(ceil(x)) |
| 7748 | except Exception: |
| 7749 | return x |
| 7750 | |
| 7751 | |
| 7752 | def only_numeric(s): |
| 7753 | return "".join(n for n in f"{s}" if n.isnumeric() or n == ".") |
| 7754 | |
| 7755 | |
| 7756 | def convert_to_local_datetime(dt: str, **kwargs): |
| 7757 | if isinstance(dt, datetime): |
| 7758 | pass |
| 7759 | elif isinstance(dt, date): |
| 7760 | dt = datetime(dt.year, dt.month, dt.day) |
| 7761 | else: |
| 7762 | if isinstance(dt, str): |
| 7763 | dt = date_replace.sub("", dt) |
| 7764 | try: |
| 7765 | dt = parser.parse(dt) |
| 7766 | except Exception: |
| 7767 | raise ValueError(f"Could not parse {dt} as a datetime") |
| 7768 | if dt.tzinfo is None: |
| 7769 | dt.replace(tzinfo=tz.tzlocal()) |
| 7770 | dt = dt.astimezone(tz.tzlocal()) |
| 7771 | return dt.replace(tzinfo=None) |
| 7772 | |
| 7773 | |
| 7774 | def datetime_to_string(dt: datetime, **kwargs): |
| 7775 | return dt.strftime("%d %b, %Y, %H:%M:%S") |
| 7776 | |
| 7777 | |
| 7778 | # Custom Formatter with additional kwargs |
| 7779 | |
| 7780 | |
| 7781 | def custom_datetime_to_str(dt: datetime, **kwargs): |
| 7782 | return dt.strftime(kwargs["format"]) |
| 7783 | |
| 7784 | |
| 7785 | class demo(tk.Tk): |
| 7786 | def __init__(self): |
| 7787 | tk.Tk.__init__(self) |
| 7788 | self.grid_columnconfigure(0, weight=1) |
| 7789 | self.grid_rowconfigure(0, weight=1) |
| 7790 | self.frame = tk.Frame(self) |
| 7791 | self.frame.grid_columnconfigure(0, weight=1) |
| 7792 | self.frame.grid_rowconfigure(0, weight=1) |
| 7793 | self.sheet = Sheet(self.frame, empty_vertical=0, empty_horizontal=0, data=[[f"{r}"] * 11 for r in range(20)]) |
| 7794 | self.sheet.enable_bindings() |
| 7795 | self.frame.grid(row=0, column=0, sticky="nswe") |
| 7796 | self.sheet.grid(row=0, column=0, sticky="nswe") |
| 7797 | self.sheet.headers( |
| 7798 | [ |
| 7799 | "Non-Nullable Float Cell\n1 decimals places", |
| 7800 | "Float Cell", |
| 7801 | "Int Cell", |
| 7802 | "Bool Cell", |
| 7803 | "Percentage Cell\n0 decimal places", |
| 7804 | "Custom Datetime Cell", |
| 7805 | "Custom Datetime Cell\nCustom Format String", |
| 7806 | "Float Cell that\nrounds up", |
| 7807 | "Float cell that\n strips non-numeric", |
| 7808 | "Dropdown Over Nullable\nPercentage Cell", |
| 7809 | "Percentage Cell\n2 decimal places", |
| 7810 | ] |
| 7811 | ) |
| 7812 | |
| 7813 | # num2alpha converts column integer to letter |
| 7814 | |
| 7815 | # Some examples of data formatting |
| 7816 | self.sheet[num2alpha(0)].format(float_formatter(nullable=False)) |
| 7817 | self.sheet[num2alpha(1)].format(float_formatter()) |
| 7818 | self.sheet[num2alpha(2)].format(int_formatter()) |
| 7819 | self.sheet[num2alpha(3)].format(bool_formatter(truthy=truthy | {"nah yeah"}, falsy=falsy | {"yeah nah"})) |
| 7820 | self.sheet[num2alpha(4)].format(percentage_formatter()) |
| 7821 | |
| 7822 | # Custom Formatters |
| 7823 | # Custom using generic formatter interface |
| 7824 | self.sheet[num2alpha(5)].format( |
| 7825 | formatter( |
| 7826 | datatypes=datetime, |
| 7827 | format_function=convert_to_local_datetime, |
| 7828 | to_str_function=datetime_to_string, |
| 7829 | nullable=False, |
| 7830 | invalid_value="NaT", |
| 7831 | ) |
| 7832 | ) |
| 7833 | |
| 7834 | # Custom format |
| 7835 | self.sheet[num2alpha(6)].format( |
| 7836 | datatypes=datetime, |
| 7837 | format_function=convert_to_local_datetime, |
| 7838 | to_str_function=custom_datetime_to_str, |
| 7839 | nullable=True, |
| 7840 | invalid_value="NaT", |
| 7841 | format="(%Y-%m-%d) %H:%M %p", |
| 7842 | ) |
| 7843 | |
| 7844 | # Unique cell behaviour using the post_conversion_function |
| 7845 | self.sheet[num2alpha(7)].format(float_formatter(post_format_function=round_up)) |
| 7846 | self.sheet[num2alpha(8)].format(float_formatter(), pre_format_function=only_numeric) |
| 7847 | self.sheet[num2alpha(9)].dropdown(values=["", "104%", 0.24, "300%", "not a number"], set_value=1,) |
| 7848 | self.sheet[num2alpha(9)].format(percentage_formatter(), decimals=0) |
| 7849 | self.sheet[num2alpha(10)].format(percentage_formatter(decimals=5)) |
| 7850 | |
| 7851 | |
| 7852 | app = demo() |
| 7853 | app.mainloop() |
| 7854 | ``` |
| 7855 | |
| 7856 | --- |
| 7857 | # **Contributing** |
| 7858 | |
| 7859 | Welcome and thank you for your interest in `tksheet`! |
| 7860 | |
| 7861 | ### **tksheet Goals** |
| 7862 | |
| 7863 | - **Adaptable rather than comprehensive**: Prioritizes adaptability over comprehensiveness, providing essential features that can be easily extended or customized based on specific needs. This approach allows for flexibility in integrating tksheet into different projects and workflows. |
| 7864 | |
| 7865 | - **Lightweight and performant**: Aims to provide a lightweight solution for creating spreadsheet-like functionality in tkinter applications, without additional dependencies and with a focus on efficiency and performance. |
| 7866 | |
| 7867 | ### **Dependencies** |
| 7868 | |
| 7869 | tksheet is designed to only use built-in Python libraries (without third-party dependencies). Please ensure that your contributions do not introduce any new dependencies outside of Python's built-in libraries. |
| 7870 | |
| 7871 | ### **License** |
| 7872 | |
| 7873 | tksheet is released under the MIT License. You can find the full text of the license [here](https://github.com/ragardner/tksheet/blob/master/LICENSE.txt). |
| 7874 | |
| 7875 | By contributing to the tksheet project, you agree to license your contributions under the same MIT License. Please make sure to read and understand the terms and conditions of the license before contributing. |
| 7876 | |
| 7877 | ### **Contributing Code** |
| 7878 | |
| 7879 | To contribute, please follow these steps: |
| 7880 | |
| 7881 | 1. Fork the tksheet repository. |
| 7882 | 2. If you are working on a new feature, create a new branch for your contribution. Use a descriptive name for the branch that reflects the feature you're working on. |
| 7883 | 3. Make your changes in your local branch, following the code style and conventions established in the project. |
| 7884 | 4. Test your changes thoroughly to ensure they do not introduce any new bugs or issues. |
| 7885 | 5. Submit a pull request to the `main` branch of the tksheet repository, including a clear title and detailed description of your changes. Pull requests ideally should include a small but comprehensive demonstration of the feature you are adding. |
| 7886 | 6. Don't forget to update the documentation! |
| 7887 | |
| 7888 | ***Note:*** If you're submitting a bugfix, it's generally preferred to submit it directly to the relevant branch, rather than creating a separate branch. |
| 7889 | |
| 7890 | ### **Asking Questions** |
| 7891 | |
| 7892 | Got a question that hasn't been answered in the closed issues or is missing from the documentation? please follow these guidelines: |
| 7893 | |
| 7894 | - Submit your question as an issue in the [Issues tab](https://github.com/ragardner/tksheet/issues). |
| 7895 | - Provide a clear and concise description of your question, including any relevant details or examples that can help us understand your query better. |
| 7896 | |
| 7897 | ### **Issues** |
| 7898 | |
| 7899 | Please use the [Issues tab](https://github.com/ragardner/tksheet/issues) to report any issues or ask for assistance. |
| 7900 | |
| 7901 | When submitting an issue, please follow these guidelines: |
| 7902 | |
| 7903 | - Check the existing issues to see if a similar bug or question has already been reported or discussed. |
| 7904 | - If reporting a bug, provide a minimal example that can reproduce the issue, including any relevant code, error messages, and steps to reproduce. |
| 7905 | - If asking a question or seeking help, provide a clear and concise description of your question or issue, including any relevant details or examples that can help people understand your query better. |
| 7906 | - Include any relevant screenshots or gifs that can visually illustrate the issue or your question. |
| 7907 | |
| 7908 | ### **Enhancements or Suggestions** |
| 7909 | |
| 7910 | If you have an idea for a new feature, improvement or change, please follow these guidelines: |
| 7911 | |
| 7912 | - Submit your suggestion as an issue in the [Issues tab](https://github.com/ragardner/tksheet/issues). |
| 7913 | - Include a clear and concise description of your idea, including any relevant details, screenshots, or mock-ups that can help contributors understand your suggestion better. |
| 7914 | - You're also welcome to become a contributor yourself and help implement your idea! |
| 7915 | |
| 7916 | ### **Contributors and Special Thanks** |
| 7917 | |
| 7918 | A special thank you to: |
| 7919 | |
| 7920 | - @CalJaDav for the very helpful ideas/pull requests, guidance in implementing them and helping me become a better developer. |
| 7921 | - @demberto for providing pull requests and guidance to modernize and improve the project. |
| 7922 | - All [contributors](https://github.com/ragardner/tksheet/graphs/contributors). |
| 7923 | - Everyone who has reported an issue and helped me fix it. |