Browse Source

TTkTable: Added the examples explaining the basic usage and the custom table model

pull/272/head
Eugenio Parodi 1 year ago
parent
commit
af57bc2c1a
  1. 24
      TermTk/TTkAbstract/abstracttablemodel.py
  2. 7
      docs/source/info/installing.rst
  3. 19
      tutorial/000-examples.rst
  4. 71
      tutorial/examples/TTkTable/table.01.basic.py
  5. 111
      tutorial/examples/TTkTable/table.02.custom.model.01.py
  6. 128
      tutorial/examples/TTkTable/table.02.custom.model.02.py
  7. 176
      tutorial/examples/TTkTable/table.02.custom.model.03.py
  8. 75
      tutorial/examples/TTkTable/table.03.theming.py

24
TermTk/TTkAbstract/abstracttablemodel.py

@ -20,7 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
__all__ = ['TTkAbstractTableModel']
__all__ = ['TTkAbstractTableModel','TTkModelIndex']
from TermTk.TTkCore.constant import TTkK
from TermTk.TTkCore.string import TTkString
@ -75,6 +75,26 @@ class TTkModelIndex():
'''
pass
class _TTkModelIndexList(TTkModelIndex):
__slots__ = ('_col','_row','_model')
def __init__(self, row:int, col:list, model) -> None:
self._col = col
self._row = row
self._model = model
super().__init__()
def row(self) -> int:
return self._row
def col(self) -> int:
return self._col
def data(self) -> object:
return self._model.data(self._row,self._col)
def setData(self, data: object) -> None:
self._model.setData(self._row,self._col,data)
class TTkAbstractTableModel():
'''
:class:`TTkAbstractTableModel` provides a standard interface for
@ -152,7 +172,7 @@ class TTkAbstractTableModel():
:return: :class:`~TermTk.TTkAbstract.abstracttablemodel.TTkModelIndex`
'''
return TTkModelIndex()
return _TTkModelIndexList(row,col,self)
def data(self, row:int, col:int) -> object:
'''

7
docs/source/info/installing.rst

@ -46,6 +46,13 @@ User Install
# Clear/Erase/GetRidOf the venv
rm -rf .venv
Windows10 Install
~~~~~~~~~~~~~~~~~
.. raw:: html
<iframe width="600" height="450" src="https://www.youtube.com/embed/1nQ3P3bgKNY?si=aagKoOBKtMi8MT_1&cc_load_policy=1" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
.. _install-copy:

19
tutorial/000-examples.rst

@ -54,6 +54,25 @@ TTkTerminal
`TTkTerminal/TerminalTab.04.KodeTab.close.py <https://github.com/ceccopierangiolieugenio/pyTermTk/blob/main/tutorial/examples/TTkTerminal/TerminalTab.04.KodeTab.close.py>`_
TTkTable
========
`TTkTable/table.01.basic.py <https://github.com/ceccopierangiolieugenio/pyTermTk/blob/main/tutorial/examples/TTkTable/table.01.basic.py>`_
(`tryItOnline <https://ceccopierangiolieugenio.github.io/pyTermTk/sandbox/sandbox.html?filePath=tutorial/examples/TTkTable/table.01.basic.py>`_):
`TTkTable/table.02.custom.model.01.py <https://github.com/ceccopierangiolieugenio/pyTermTk/blob/main/tutorial/examples/TTkTable/table.02.custom.model.01.py>`_
(`tryItOnline <https://ceccopierangiolieugenio.github.io/pyTermTk/sandbox/sandbox.html?filePath=tutorial/examples/TTkTable/table.02.custom.model.01.py>`_):
`TTkTable/table.02.custom.model.02.py <https://github.com/ceccopierangiolieugenio/pyTermTk/blob/main/tutorial/examples/TTkTable/table.02.custom.model.02.py>`_
(`tryItOnline <https://ceccopierangiolieugenio.github.io/pyTermTk/sandbox/sandbox.html?filePath=tutorial/examples/TTkTable/table.02.custom.model.02.py>`_):
`TTkTable/table.02.custom.model.03.py <https://github.com/ceccopierangiolieugenio/pyTermTk/blob/main/tutorial/examples/TTkTable/table.02.custom.model.03.py>`_
(`tryItOnline <https://ceccopierangiolieugenio.github.io/pyTermTk/sandbox/sandbox.html?filePath=tutorial/examples/TTkTable/table.02.custom.model.03.py>`_):
`TTkTable/table.03.theming.py <https://github.com/ceccopierangiolieugenio/pyTermTk/blob/main/tutorial/examples/TTkTable/table.03.theming.py>`_
(`tryItOnline <https://ceccopierangiolieugenio.github.io/pyTermTk/sandbox/sandbox.html?filePath=tutorial/examples/TTkTable/table.03.theming.py>`_):
TTkTextEdit
===========

71
tutorial/examples/TTkTable/table.01.basic.py

@ -0,0 +1,71 @@
#!/usr/bin/env python3
# MIT License
#
# Copyright (c) 2024 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Those 2 lines are required to use the TermTk library in the main folder
import sys, os
sys.path.append(os.path.join(sys.path[0],'../../..'))
import TermTk as ttk
# root
# The root (TTk) widget is the main element of any pyTermTk apps
# It is responsible of any terminal routine, the terminal update
# and the events forwarded to any child widgets
#
# root initialization params used:
#
# layout = GridLayout
# I am using a grid layout in order to align/resize the table
# to the root widget (the terminal area)
#
# mouseTrack = True (optional)
# I am enabling the mouseTrack because the table can handle
# the mouse move events and highlight the cells under the cursor
root = ttk.TTk(layout=ttk.TTkGridLayout(), mouseTrack=True)
# The TTkTable is the widget responsible of handling and displaying a table model
# There are several table models available, in this example I am using
# TTkTableModelList which require a simple 2d List with the table data
# I am initializing a simple list where any cell is a string representing its position (row,col) inside the list
dataList = [[f"{(row,col)}" for col in range(10)] for row in range(101)]
basicTableModel = ttk.TTkTableModelList(data=dataList)
# Table initialization:
# parent = root
# In this example the root terminal (TTk) has this table as the only child
# since it is using a gridlayout, the child will be resized to occupy the entire
# area available
# tableModel = basicTableModel
# I guess this is self explainatory
table = ttk.TTkTable(parent=root, tableModel=basicTableModel)
# Adding a little touch, resizing the cols to its content
table.resizeColumnsToContents()
# Start the main thread
root.mainloop()

111
tutorial/examples/TTkTable/table.02.custom.model.01.py

@ -0,0 +1,111 @@
#!/usr/bin/env python3
# MIT License
#
# Copyright (c) 2024 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Those 2 lines are required to use the TermTk library in the main folder
import sys, os
sys.path.append(os.path.join(sys.path[0],'../../..'))
import TermTk as ttk
# Any table model is an extension of the TTkAbstractTableMode interface
# Those methods must be defined in order to have a basic implamentation:
# rowCount(self) -> int
# columnCount(self) -> int
# data(self, row:int, col:int) -> object
class MyTableModel(ttk.TTkAbstractTableModel):
def __init__(self):
# For testing purposes, this model include a 2d array of strings
self._mylist = [[f"Cell:{(row,col)}" for col in range(8)] for row in range(50)]
# I am adding also an int, float, multiline string, multiline Coloured TTkString
self._mylist[3][5] = 1234
self._mylist[4][5] = 1234.1234
self._mylist[5][5] = "Line1\nLine2"
self._mylist[6][5] = ttk.TTkString("Line1\nLine2\nLine3",ttk.TTkColor.YELLOW)
super().__init__()
def rowCount(self) -> int:
return len(self._mylist)
def columnCount(self) -> int:
return len(self._mylist[0])
def data(self, row:int, col:int) -> None:
return self._mylist[row][col]
# Header Data is not a mandatory field,
# I am overriding it with a custom value displayed
# in the top and left headers
def headerData(self, pos:int, orientation:ttk.TTkK.Direction) -> ttk.TTkString:
if orientation == ttk.TTkK.HORIZONTAL:
return f"H:0x{pos:02x}"
if orientation == ttk.TTkK.VERTICAL:
return f"V:0x{pos:02x}"
return super().headerData(pos, orientation)
# By default any cell is enabled and selectable
# I am overriding this function to disable the selectable field from
# the colummn n.1 (the second column)
def flags(self, row: int, col: int) -> ttk.TTkConstant.ItemFlag:
if col==1:
return ttk.TTkK.ItemFlag.ItemIsEnabled
return super().flags(row,col)
# root
# The root (TTk) widget is the main element of any pyTermTk apps
# It is responsible of any terminal routine, the terminal update
# and the events forwarded to any child widgets
#
# root initialization params used:
#
# layout = GridLayout
# I am using a grid layout in order to align/resize the table
# to the root widget (the terminal area)
#
# mouseTrack = True (optional)
# I am enabling the mouseTrack because the table can handle
# the mouse move events and highlight the cells under the cursor
root = ttk.TTk(layout=ttk.TTkGridLayout(), mouseTrack=True)
# The TTkTable is the widget responsible of handling and displaying a table model
# There are several table models available, in this example I am using
# TTkTableModelList which require a simple 2d List with the table data
# Table initialization:
# parent = root
# In this example the root terminal (TTk) has this table as the only child
# since it is using a gridlayout, the child will be resized to occupy the entire
# area available
# tableModel = MyTableModel
# I am using an instance of the custom table model
table = ttk.TTkTable(parent=root, tableModel=MyTableModel())
# Adding a little touch, resizing the cols and the rows to their content
table.resizeColumnsToContents()
table.resizeRowsToContents()
# Start the main thread
root.mainloop()

128
tutorial/examples/TTkTable/table.02.custom.model.02.py

@ -0,0 +1,128 @@
#!/usr/bin/env python3
# MIT License
#
# Copyright (c) 2024 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Those 2 lines are required to use the TermTk library in the main folder
import sys, os
sys.path.append(os.path.join(sys.path[0],'../../..'))
import TermTk as ttk
# Any table model is an extension of the TTkAbstractTableMode interface
# Those methods must be defined in order to have a basic implamentation:
# rowCount(self) -> int
# columnCount(self) -> int
# data(self, row:int, col:int) -> object
class MyTableModel(ttk.TTkAbstractTableModel):
def __init__(self):
# For testing purposes, this model include a 2d array of strings
self._mylist = [[f"Cell:{(row,col)}" for col in range(8)] for row in range(50)]
# I am adding also an int, float, multiline string, multiline Coloured TTkString
self._mylist[3][5] = 1234
self._mylist[4][5] = 1234.1234
self._mylist[5][5] = "Line1\nLine2"
self._mylist[6][5] = ttk.TTkString("Line1\nLine2\nLine3",ttk.TTkColor.YELLOW)
super().__init__()
def rowCount(self) -> int:
return len(self._mylist)
def columnCount(self) -> int:
return len(self._mylist[0])
def data(self, row:int, col:int) -> None:
return self._mylist[row][col]
# I need to define the setData method if I want to include the editable
# feature for this custom table model
def setData(self, row: int, col: int, data: object) -> bool:
self._mylist[row][col] = data
return True
# Header Data is not a mandatory field,
# I am overriding it with a custom value displayed
# in the top and left headers
def headerData(self, pos:int, orientation:ttk.TTkK.Direction) -> ttk.TTkString:
if orientation == ttk.TTkK.HORIZONTAL:
return f"H:0x{pos:02x}"
if orientation == ttk.TTkK.VERTICAL:
return f"V:0x{pos:02x}"
return super().headerData(pos, orientation)
# By default any cell is enabled and selectable
# I am overriding this function to disable the selectable field from
# the colummn n.1 (the second column)
# and allow all the columns except the first one to be editable
def flags(self, row: int, col: int) -> ttk.TTkConstant.ItemFlag:
if col==0:
return (
ttk.TTkK.ItemFlag.ItemIsEnabled |
ttk.TTkK.ItemFlag.ItemIsSelectable )
if col==1:
return (
ttk.TTkK.ItemFlag.ItemIsEnabled |
ttk.TTkK.ItemFlag.ItemIsEditable )
return (
ttk.TTkK.ItemFlag.ItemIsEnabled |
ttk.TTkK.ItemFlag.ItemIsEditable |
ttk.TTkK.ItemFlag.ItemIsSelectable )
# root
# The root (TTk) widget is the main element of any pyTermTk apps
# It is responsible of any terminal routine, the terminal update
# and the events forwarded to any child widgets
#
# root initialization params used:
#
# layout = GridLayout
# I am using a grid layout in order to align/resize the table
# to the root widget (the terminal area)
#
# mouseTrack = True (optional)
# I am enabling the mouseTrack because the table can handle
# the mouse move events and highlight the cells under the cursor
root = ttk.TTk(layout=ttk.TTkGridLayout(), mouseTrack=True)
# The TTkTable is the widget responsible of handling and displaying a table model
# There are several table models available, in this example I am using
# TTkTableModelList which require a simple 2d List with the table data
# Table initialization:
# parent = root
# In this example the root terminal (TTk) has this table as the only child
# since it is using a gridlayout, the child will be resized to occupy the entire
# area available
# tableModel = MyTableModel
# I am using an instance of the custom table model
table = ttk.TTkTable(parent=root, tableModel=MyTableModel())
# Adding a little touch, resizing the cols to its content
table.resizeColumnsToContents()
table.resizeRowsToContents()
# Start the main thread
root.mainloop()

176
tutorial/examples/TTkTable/table.02.custom.model.03.py

@ -0,0 +1,176 @@
#!/usr/bin/env python3
# MIT License
#
# Copyright (c) 2024 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Those 2 lines are required to use the TermTk library in the main folder
import sys, os
sys.path.append(os.path.join(sys.path[0],'../../..'))
from random import randint
import TermTk as ttk
# Any table model is an extension of the TTkAbstractTableMode interface
# Those methods must be defined in order to have a basic implamentation:
# rowCount(self) -> int
# columnCount(self) -> int
# data(self, row:int, col:int) -> object
class MyTableModel(ttk.TTkAbstractTableModel):
def __init__(self):
# For testing purposes, this model include a 2d array of strings
self._mylist = [[f"{randint(0,99):02}:{(row,col)}" for col in range(8)] for row in range(50)]
# I am adding also an int, float, multiline string, multiline Coloured TTkString
self._mylist[3][5] = 1234
self._mylist[4][5] = 1234.1234
self._mylist[5][5] = "Line1\nLine2"
self._mylist[6][5] = ttk.TTkString("Line1\nLine2\nLine3",ttk.TTkColor.YELLOW)
# To include sorting features I copy the original order in a separate variable
# this is use in case -1 is used in the "sort" method to restore the initial order
self._myListInitialOrder = self._mylist
super().__init__()
def rowCount(self) -> int:
return len(self._mylist)
def columnCount(self) -> int:
return len(self._mylist[0])
def data(self, row:int, col:int) -> None:
return self._mylist[row][col]
# I need to define the setData method if I want to include the editable
# feature for this custom table model
def setData(self, row: int, col: int, data: object) -> bool:
self._mylist[row][col] = data
return True
# Header Data is not a mandatory field,
# I am overriding it with a custom value displayed
# in the top and left headers
def headerData(self, pos:int, orientation:ttk.TTkK.Direction) -> ttk.TTkString:
if orientation == ttk.TTkK.HORIZONTAL:
return f"H:0x{pos:02x}"
if orientation == ttk.TTkK.VERTICAL:
return f"V:0x{pos:02x}"
return super().headerData(pos, orientation)
# By default any cell is enabled and selectable
# I am overriding this function to disable the selectable field from
# the colummn n.1 (the second column)
# and allow all the columns except the first one to be editable
def flags(self, row: int, col: int) -> ttk.TTkConstant.ItemFlag:
if col==0:
return (
ttk.TTkK.ItemFlag.ItemIsEnabled |
ttk.TTkK.ItemFlag.ItemIsSelectable )
if col==1:
return (
ttk.TTkK.ItemFlag.ItemIsEnabled |
ttk.TTkK.ItemFlag.ItemIsEditable )
return (
ttk.TTkK.ItemFlag.ItemIsEnabled |
ttk.TTkK.ItemFlag.ItemIsEditable |
ttk.TTkK.ItemFlag.ItemIsSelectable )
# To include sorting capabilities this method need to be included in the data model
# the implementation is highly dependant of the data structure used in this data model
def sort(self, column:int, order:ttk.TTkK.SortOrder) -> None:
if column == -1:
self._mylist = self._myListInitialOrder
else:
# This try/except is a little hack in case the column does not include elements of the same type
try:
self._mylist = sorted(self._myListInitialOrder, key=lambda x:x[column], reverse=order==ttk.TTkK.SortOrder.DescendingOrder)
except TypeError as _:
self._mylist = sorted(self._myListInitialOrder, key=lambda x:str(x[column]), reverse=order==ttk.TTkK.SortOrder.DescendingOrder)
# root
# The root (TTk) widget is the main element of any pyTermTk apps
# It is responsible of any terminal routine, the terminal update
# and the events forwarded to any child widgets
#
# root initialization params used:
#
# layout = GridLayout
# I am using a grid layout in order to align/resize the table
# to the root widget (the terminal area)
#
# mouseTrack = True (optional)
# I am enabling the mouseTrack because the table can handle
# the mouse move events and highlight the cells under the cursor
#
# sigmask = <various signals>
# This field configure pyTermTk to mask those shortcuts and allow
# the library to handle them internally, this is useful to handle
# the copy (CRTL-C), paste (CTRL-V) internally without triggering
# SIGINT to the current app and terminating it
# NOTE: to quit the app I added in this example a QUIT button attached
# to the quit method of the root widget
root = ttk.TTk(layout=ttk.TTkGridLayout(),
mouseTrack=True,
sigmask=(
ttk.TTkTerm.Sigmask.CTRL_Q |
ttk.TTkTerm.Sigmask.CTRL_S |
ttk.TTkTerm.Sigmask.CTRL_C |
ttk.TTkTerm.Sigmask.CTRL_Z ))
# The TTkTable is the widget responsible of handling and displaying a table model
# There are several table models available, in this example I am using
# TTkTableModelList which require a simple 2d List with the table data
# Table initialization:
# tableModel = MyTableModel
# I am using an instance of the custom table model
# sortingEnabled = True
# Enable the column sorting
table = ttk.TTkTable(tableModel=MyTableModel(), sortingEnabled=True)
# I am creating a QUIT button attaching its main event (clicked) to the quit routine
# Forcing it to a size (w:30,h:1) to avoid it being stretched
quitButton = ttk.TTkButton(text="---> QUIT - QUIT - QUIT <---", maxSize=(30,1))
quitButton.clicked.connect(root.quit)
# I am placing the button on the grid layout cell 0,0
# and the table below at the cell 1,0 with a colspan=2
# so that the button will not be stretched to be aligned
# to the table (or the table reduced to the button width)
#
# Col 0 Col 1
# ┌────────┬───────┐
# row 0 │ BUTTON │ │
# ├────────┴───────┤
# row 1 │ TABLE │
# └────────────────┘
root.layout().addWidget(quitButton,0,0)
root.layout().addWidget(table,1,0,1,2)
# Adding a little touch, resizing the cols to its content
table.resizeColumnsToContents()
table.resizeRowsToContents()
# Start the main thread
root.mainloop()

75
tutorial/examples/TTkTable/table.03.theming.py

@ -0,0 +1,75 @@
#!/usr/bin/env python3
# MIT License
#
# Copyright (c) 2024 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Those 2 lines are required to use the TermTk library in the main folder
import sys, os
sys.path.append(os.path.join(sys.path[0],'../../..'))
import TermTk as ttk
# root
# The root (TTk) widget is the main element of any pyTermTk apps
# It is responsible of any terminal routine, the terminal update
# and the events forwarded to any child widgets
#
# root initialization params used:
#
# layout = GridLayout
# I am using a grid layout in order to align/resize the table
# to the root widget (the terminal area)
#
# mouseTrack = True (optional)
# I am enabling the mouseTrack because the table can handle
# the mouse move events and highlight the cells under the cursor
root = ttk.TTk(layout=ttk.TTkGridLayout(), mouseTrack=True)
# The TTkTable is the widget responsible of handling and displaying a table model
# There are several table models available, in this example I am using
# TTkTableModelList which require a simple 2d List with the table data
# I am initializing a simple list where any cell is a string representing its position (row,col) inside the list
dataList = [[f"{(row,col)}" for col in range(10)] for row in range(101)]
basicTableModel = ttk.TTkTableModelList(data=dataList)
# Table initialization:
# parent = root
# In this example the root terminal (TTk) has this table as the only child
# since it is using a gridlayout, the child will be resized to occupy the entire
# area available
# tableModel = basicTableModel
# I guess this is self explainatory
table = ttk.TTkTable(tableModel=basicTableModel)
root.layout().addWidget(table,1,0,1,2)
quitButton = ttk.TTkButton(text="QUIT", maxSize=(10,3))
root.layout().addWidget(quitButton,0,0)
# Adding a little touch, resizing the cols to its content
table.resizeColumnsToContents()
# Start the main thread
root.mainloop()
Loading…
Cancel
Save