Browse Source

Add signals/slots tutorial

pull/5/head
Eugenio Parodi 5 years ago
parent
commit
f4947faea4
  1. 6
      README.md
  2. 71
      demo/demo.py
  3. 104
      tutorial/003-signalslots.md
  4. 2
      tutorial/004-logging.md
  5. 2
      tutorial/logging/example4.ttklogviewer.py
  6. 58
      tutorial/signalslots/example1.basic.signalslots.py
  7. 42
      tutorial/signalslots/example2.widgets.signalslots.py

6
README.md

@ -34,17 +34,17 @@ clone git@github.com:ceccopierangiolieugenio/pyTermTk.git
cd pyTermTk
```
#### Run Basic input test
#### Run Basic (non ui) input test
```shell
python3 tests/test.input.py
```
#### Run Terminal resize test
#### Run demo
```shell
# Press CTRL-C to exit
# the logs are written to "session.log"
make runDemo
# or
# or
python3 demo/demo.py -f
# Try gittk

71
demo/demo.py

@ -41,6 +41,12 @@ from showcase.scrollarea import demoScrollArea
from showcase.list import demoList
from showcase.menubar import demoMenuBar
words = ["Lorem", "ipsum", "dolor", "sit", "amet,", "consectetur", "adipiscing", "elit,", "sed", "do", "eiusmod", "tempor", "incididunt", "ut", "labore", "et", "dolore", "magna", "aliqua.", "Ut", "enim", "ad", "minim", "veniam,", "quis", "nostrud", "exercitation", "ullamco", "laboris", "nisi", "ut", "aliquip", "ex", "ea", "commodo", "consequat.", "Duis", "aute", "irure", "dolor", "in", "reprehenderit", "in", "voluptate", "velit", "esse", "cillum", "dolore", "eu", "fugiat", "nulla", "pariatur.", "Excepteur", "sint", "occaecat", "cupidatat", "non", "proident,", "sunt", "in", "culpa", "qui", "officia", "deserunt", "mollit", "anim", "id", "est", "laborum."]
def getWord():
return random.choice(words)
def getSentence(a,b):
return " ".join([getWord() for i in range(0,random.randint(a,b))])
def demoShowcase(root= None, border=True):
tabWidget1 = ttk.TTkTabWidget(parent=root, border=border)
tabWidget1.addTab(ttk.TTkTestWidgetSizes(border=True, title="Frame1.1"), " Label 1.1 ")
@ -57,6 +63,8 @@ def demoShowcase(root= None, border=True):
tabWidget1.addTab(demoWindows(), " Windows Test ")
tabWidget1.addTab(demoTab(), " Tab Test ")
tabWidget1.addTab(demoScrollArea(), " Scroll Area ")
return tabWidget1
def main():
@ -75,6 +83,69 @@ def main():
winTabbed1 = ttk.TTkWindow(parent=root,pos=(0,0), size=(120,40), title="Test Tab", border=True, layout=ttk.TTkGridLayout())
border = True
demoShowcase(winTabbed1, border)
win_table1 = ttk.TTkWindow(parent=root,pos = (3,3), size=(150,40), title="Test Table 1", layout=ttk.TTkHBoxLayout(), border=True)
table1 = ttk.TTkTable(parent=win_table1, selectColor=ttk.TTkColor.bg('#882200'))
win_table2 = ttk.TTkWindow(parent=root,pos = (15,5), size=(100,30), title="Test Table 2 Default", layout=ttk.TTkHBoxLayout(), border=True)
table2 = ttk.TTkTable(parent=win_table2)
win_table3 = ttk.TTkWindow(parent=root,pos = (15,5), size=(130,40), title="Test Table 2 Default", layout=ttk.TTkHBoxLayout(), border=True)
table3 = ttk.TTkTable(parent=win_table3, showHeader=False)
table1.setColumnSize((5,10,-1,10,20))
table1.setAlignment((
ttk.TTkK.LEFT_ALIGN,
ttk.TTkK.RIGHT_ALIGN,
ttk.TTkK.LEFT_ALIGN,
ttk.TTkK.LEFT_ALIGN,
ttk.TTkK.CENTER_ALIGN
))
table1.setHeader(("id","Name","Sentence","Word","center"))
table1.setColumnColors((
ttk.TTkColor.fg('#888800', modifier=ttk.TTkColorGradient(increment=6)),
ttk.TTkColor.RST,
ttk.TTkColor.fg('#00dddd', modifier=ttk.TTkColorGradient(increment=-4)),
ttk.TTkColor.RST,
ttk.TTkColor.fg('#cccccc', modifier=ttk.TTkColorGradient(increment=-2))
))
table2.setColumnSize((5,10,-1,10,20))
table2.setHeader(("id","Name","Sentence","Word",""))
table3.setColumnSize((5,10,-1,10,20))
table3.setAlignment((
ttk.TTkK.LEFT_ALIGN,
ttk.TTkK.RIGHT_ALIGN,
ttk.TTkK.LEFT_ALIGN,
ttk.TTkK.LEFT_ALIGN,
ttk.TTkK.CENTER_ALIGN
))
table3.setHeader(("id","Name","Sentence","Word","center"))
table3.setColumnColors((
ttk.TTkColor.fg('#ffff00', modifier=ttk.TTkColorGradient(increment=6)),
ttk.TTkColor.fg('#ff0000', modifier=ttk.TTkColorGradient(increment=6)),
ttk.TTkColor.fg('#00ffff', modifier=ttk.TTkColorGradient(increment=-8)),
ttk.TTkColor.fg('#00ff00', modifier=ttk.TTkColorGradient(increment=-4)),
ttk.TTkColor.fg('#cccccc', modifier=ttk.TTkColorGradient(increment=-2))
))
table3.appendItem((" - ","","You see it's all clear, You were meant to be here, From the beginning","",""))
for i in range(0, 5):
table1.appendItem((str(i), getWord(), getSentence(8,30), getWord(), getWord()))
for i in range(0, 5):
table2.appendItem((str(i), getWord(), getSentence(8,30), getWord(), getWord()))
for i in range(0, 35):
table3.appendItem((str(i), getWord(), getSentence(8,30), getWord(), getWord()))
table3.appendItem((" - ","This is the end", "Beautiful friend, This is the end My only friend", "the end", "..."))
root.mainloop()
if __name__ == "__main__":

104
tutorial/003-signalslots.md

@ -0,0 +1,104 @@
# [pyTermTk](https://github.com/ceccopierangiolieugenio/pyTermTk) - Signal & Slots
## Intro
The [TermTk Signal&Slots](https://ceccopierangiolieugenio.github.io/pyTermTk/TTkCore/signal.html) and more than heavily inspired by [Qt5 Signal&Slots](https://www.riverbankcomputing.com/static/Docs/PyQt5/signals_slots.html)
https://doc.qt.io/qt-5/signalsandslots.html
## Example 1 - basic signal slots
From [example1.basic.signalslots.py](signalslots/example1.basic.signalslots.py)
```python
import TermTk as ttk
ttk.TTkLog.use_default_stdout_logging()
# define 2 signals with different signatures
signal = ttk.pyTTkSignal()
otherSignal = ttk.pyTTkSignal(int)
# Define a slot with no input as signature
@ttk.pyTTkSlot()
def slot():
ttk.TTkLog.debug("Received a simple signal")
# Define 2 slots with "int" as input signature
@ttk.pyTTkSlot(int)
def otherSlot(val):
ttk.TTkLog.debug(f"[otherSlot] Received a valued signal, val:{val}")
@ttk.pyTTkSlot(int)
def anotherSlot(val):
ttk.TTkLog.debug(f"[anootherSlot] Received a valued signal, val:{val}")
# connect the signals to the proper slot
signal.connect(slot)
otherSignal.connect(otherSlot)
otherSignal.connect(anotherSlot)
# Test the signals
ttk.TTkLog.debug("Emit a simple signal")
signal.emit()
ttk.TTkLog.debug("Emit a valued signal")
otherSignal.emit(123)
```
The above code produces the following output
```text
$ tutorial/signalslots/example1.basic.signalslots.py
DEBUG:(MainThread) tutorial/signalslots/example1.basic.signalslots.py:54 Emit a simple signal
DEBUG:(MainThread) tutorial/signalslots/example1.basic.signalslots.py:37 Received a simple signal
DEBUG:(MainThread) tutorial/signalslots/example1.basic.signalslots.py:56 Emit a valued signal
DEBUG:(MainThread) tutorial/signalslots/example1.basic.signalslots.py:42 [otherSlot] Received a valued signal, val:123
DEBUG:(MainThread) tutorial/signalslots/example1.basic.signalslots.py:45 [anootherSlot] Received a valued signal, val:123
```
## Example 2 - Use widgets signals and slots
From [example2.widgets.signalslots.py](signalslots/example2.widgets.signalslots.py)
```python
import TermTk as ttk
root = ttk.TTk()
# Create a window with a logviewer
logWin = ttk.TTkWindow(parent=root,pos = (10,2), size=(80,20), title="LogViewer Window", border=True, layout=ttk.TTkVBoxLayout())
ttk.TTkLogViewer(parent=logWin)
# Create 2 buttons
btnShow = ttk.TTkButton(parent=root, text="Show", pos=(0,0), size=(10,3), border=True)
btnHide = ttk.TTkButton(parent=root, text="Hide", pos=(0,3), size=(10,3), border=True)
# Connect the btnShow's "clicked" signal with the window's "show" slot
btnShow.clicked.connect(logWin.show)
# Connect the btnHide's "clicked" signal with the window's "hide" slot
btnHide.clicked.connect(logWin.hide)
root.mainloop()
```
It is totally useless for this example but the above code produces the following output
```text
┌────────┐
│ Show │
╘════════╛╔══════════════════════════════════════════════════════════════════════════════╗
┌────────┐║ LogViewer Window ║
│ Hide │╟──────────────────────────────────────────────────────────────────────────────╢
╘════════╛║ ║
║DEBUG: _/.venv/lib/python3.8/site-packages/TermTk/TTkCore/ttk.py:70 Starting M║
║DEBUG: _/.venv/lib/python3.8/site-packages/TermTk/TTkCore/ttk.py:80 Signal Eve║
║DEBUG: _/.venv/lib/python3.8/site-packages/TermTk/TTkCore/ttk.py:65 fps: 33 ║
║DEBUG: _/.venv/lib/python3.8/site-packages/TermTk/TTkCore/ttk.py:65 fps: 34 ║
║DEBUG: _/.venv/lib/python3.8/site-packages/TermTk/TTkCore/ttk.py:65 fps: 34 ║
║ ║
║ ║
║ ║
║ ║
║ ║
║ ║
║ ║
║ ║
║ ║
║◀▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓┄┄┄┄┄┄┄┄┄┄┄▶║
╚══════════════════════════════════════════════════════════════════════════════╝
```

2
tutorial/004-logging.md

@ -72,7 +72,7 @@ root = ttk.TTk()
# Create a window and attach it to the root (parent=root)
logWin = ttk.TTkWindow(parent=root,pos = (1,1), size=(80,20), title="LogViewer Window", border=True, layout=ttk.TTkVBoxLayout())
# Define the Label and attach it to the window (parent=helloWin)
# Attach the logViewer widget to the window
ttk.TTkLogViewer(parent=logWin)
ttk.TTkLog.info( "Test Info Messgae")

2
tutorial/logging/example4.ttklogviewer.py

@ -29,7 +29,7 @@ root = ttk.TTk()
# Create a window and attach it to the root (parent=root)
logWin = ttk.TTkWindow(parent=root,pos = (1,1), size=(80,20), title="LogViewer Window", border=True, layout=ttk.TTkVBoxLayout())
# Define the Label and attach it to the window (parent=helloWin)
# Attach the logViewer widget to the window
ttk.TTkLogViewer(parent=logWin)
ttk.TTkLog.info( "Test Info Messgae")

58
tutorial/signalslots/example1.basic.signalslots.py

@ -0,0 +1,58 @@
#!/usr/bin/env python3
# MIT License
#
# Copyright (c) 2021 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.
import TermTk as ttk
ttk.TTkLog.use_default_stdout_logging()
# define 2 signals with different signatures
signal = ttk.pyTTkSignal()
otherSignal = ttk.pyTTkSignal(int)
# Define a slot with no input as signature
@ttk.pyTTkSlot()
def slot():
ttk.TTkLog.debug("Received a simple signal")
# Define 2 slots with "int" as input signature
@ttk.pyTTkSlot(int)
def otherSlot(val):
ttk.TTkLog.debug(f"[otherSlot] Received a valued signal, val:{val}")
@ttk.pyTTkSlot(int)
def anotherSlot(val):
ttk.TTkLog.debug(f"[anootherSlot] Received a valued signal, val:{val}")
# connect the signals to the proper slot
signal.connect(slot)
otherSignal.connect(otherSlot)
otherSignal.connect(anotherSlot)
# Test the signals
ttk.TTkLog.debug("Emit a simple signal")
signal.emit()
ttk.TTkLog.debug("Emit a valued signal")
otherSignal.emit(123)

42
tutorial/signalslots/example2.widgets.signalslots.py

@ -0,0 +1,42 @@
#!/usr/bin/env python3
# MIT License
#
# Copyright (c) 2021 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.
import TermTk as ttk
root = ttk.TTk()
# Create a window with a logviewer
logWin = ttk.TTkWindow(parent=root,pos = (10,2), size=(80,20), title="LogViewer Window", border=True, layout=ttk.TTkVBoxLayout())
ttk.TTkLogViewer(parent=logWin)
# Create 2 buttons
btnShow = ttk.TTkButton(parent=root, text="Show", pos=(0,0), size=(10,3), border=True)
btnHide = ttk.TTkButton(parent=root, text="Hide", pos=(0,3), size=(10,3), border=True)
# Connect the btnShow's "clicked" signal with the window's "show" slot
btnShow.clicked.connect(logWin.show)
# Connect the btnHide's "clicked" signal with the window's "hide" slot
btnHide.clicked.connect(logWin.hide)
root.mainloop()
Loading…
Cancel
Save