Skip to content

Commit

Permalink
Added a few helpful widgets.
Browse files Browse the repository at this point in the history
  • Loading branch information
justengel committed Apr 10, 2020
1 parent 635a3a0 commit a7e80b0
Show file tree
Hide file tree
Showing 4 changed files with 590 additions and 4 deletions.
140 changes: 136 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ The ThreadUpdater offers several utilities to help with updating a widget's valu

* This will not be accurate. Accuracy can be improved by lowering the timeout to increase how often the timer runs.

ThreadUpdater Examples
======================

Below are some examples of how the ThreadUpdater would normally be used.

Simple Thread Example
=====================
~~~~~~~~~~~~~~~~~~~~~

The example below tells the update to run lbl.setText in the main thread with the latest value.

Expand Down Expand Up @@ -69,7 +74,7 @@ The example below tells the update to run lbl.setText in the main thread with th
Continuous Update Example
=========================
~~~~~~~~~~~~~~~~~~~~~~~~~

The example below continuously runs the update function every time `ThreadUpdater.update()` is called from the timer.
This may be inefficient if there is no new data to update the label with.
Expand Down Expand Up @@ -110,7 +115,7 @@ This may be inefficient if there is no new data to update the label with.
Call In Main Example
====================
~~~~~~~~~~~~~~~~~~~~

The example below calls the append function every time. It may not be efficient.

Expand Down Expand Up @@ -147,7 +152,7 @@ The example below calls the append function every time. It may not be efficient.
Delay Example
====================
~~~~~~~~~~~~~

The example below calls the append function after X number of seconds has passed. The delay function will not be
accurate, but guarantees that the function is called after X number of seconds. To increase accuracy give the
Expand Down Expand Up @@ -183,3 +188,130 @@ accurate, but guarantees that the function is called after X number of seconds.
get_updater().delay(3, update_text, 3)
app.exec_()
Widgets
=======

I've decdied to include a couple of useful Qt Widgets with this library.

* QuickPlainTextEdit - Used to display fast streams of data
* QuickTextEdit - Display fast streams of data with color.


QuickPlainTextEdit
~~~~~~~~~~~~~~~~~~

Quickly display data from a separate thread.

.. code-block:: python
import time
import threading
from qtpy import QtWidgets
from qt_thread_updater.widgets.quick_text_edit import QuickPlainTextEdit
app = QtWidgets.QApplication.instance() or QtWidgets.QApplication([])
text_edit = QuickPlainTextEdit()
text_edit.resize(300, 300)
text_edit.show()
data = {'counter': 0}
def run(is_alive):
is_alive.set()
while is_alive.is_set():
text = 'Main Count: {}\n'.format(data['counter'])
text_edit.write(text)
data['counter'] += 1
time.sleep(0.0001) # Some delay is usually required to let the Qt event loop run (not needed if IO used)
alive = threading.Event()
th = threading.Thread(target=run, args=(alive,))
th.start()
app.exec_()
alive.clear()
QuickTextEdit
~~~~~~~~~~~~~

Quickly display data from a separate thread using color.

.. code-block:: python
import time
import threading
from qtpy import QtWidgets
from qt_thread_updater.widgets.quick_text_edit import QuickTextEdit
app = QtWidgets.QApplication.instance() or QtWidgets.QApplication([])
text_edit = QuickTextEdit()
text_edit.resize(300, 300)
text_edit.show()
data = {'counter': 0}
def run(is_alive):
is_alive.set()
while is_alive.is_set():
text = 'Main Count: {}\n'.format(data['counter'])
text_edit.write(text, 'blue')
data['counter'] += 1
time.sleep(0.0001) # Some delay is usually required to let the Qt event loop run (not needed if IO used)
alive = threading.Event()
th = threading.Thread(target=run, args=(alive,))
th.start()
app.exec_()
alive.clear()
QuickTextEdit Redirect
~~~~~~~~~~~~~~~~~~~~~~

Display print (stdout and stderr) in a QTextEdit with color.

.. code-block:: python
import sys
import time
import threading
from qtpy import QtWidgets
from qt_thread_updater.widgets.quick_text_edit import QuickTextEdit
app = QtWidgets.QApplication.instance() or QtWidgets.QApplication([])
text_edit = QuickTextEdit()
text_edit.resize(300, 300)
text_edit.show()
sys.stdout = text_edit.redirect(color='blue', iostream=sys.__stdout__)
sys.stderr = text_edit.redirect(color='red', iostream=sys.__stderr__)
data = {'counter': 0}
def run(is_alive):
is_alive.set()
while is_alive.is_set():
stdout_text = 'Main Count: {}'.format(data['counter']) # Print gives \n automatically
error_text = 'Error Count: {}'.format(data['counter']) # Print gives \n automatically
# Print automatically give '\n' with the "end" keyword argument.
print(stdout_text) # Print will write to sys.stdout where the rediect will write to text_edit and stdout
print(error_text, file=sys.stderr) # Print to sys.stderr. Rediect will write to text_edit and stderr
data['counter'] += 1
# Some delay is usually desired. print/sys.__stdout__ uses IO which gives time for Qt's event loop.
# time.sleep(0.0001)
alive = threading.Event()
th = threading.Thread(target=run, args=(alive,))
th.start()
app.exec_()
alive.clear()
2 changes: 2 additions & 0 deletions qt_thread_updater/widgets/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

from .quick_text_edit import clipboard, QuickPlainTextEdit, QuickTextEdit, StreamWrite
Loading

0 comments on commit a7e80b0

Please sign in to comment.