Skip to content

Instantly share code, notes, and snippets.

@matteobertozzi
Created June 30, 2024 04:47
Show Gist options
  • Save matteobertozzi/7feb6f5f3e89515971bda32ac7052f79 to your computer and use it in GitHub Desktop.
Save matteobertozzi/7feb6f5f3e89515971bda32ac7052f79 to your computer and use it in GitHub Desktop.
Scroll Prompter
#!/usr/bin/env python3
# ======================================================================
# https://www.qt.io/
# https://www.riverbankcomputing.com/software/pyqt/
# ======================================================================
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from PyQt6.QtWidgets import QApplication, QMainWindow, QTextEdit, QSlider, QLabel, QVBoxLayout, QHBoxLayout, QWidget, QPushButton, QStyle
from PyQt6.QtGui import QIcon
from PyQt6.QtCore import Qt, QEvent, QTimer
import sys
class ScrollPrompterApp(QMainWindow):
def __init__(self):
super().__init__()
self.is_text_locked = False
self.init_ui()
def init_ui(self):
self.setWindowTitle("Scroll Prompter")
self.setGeometry(100, 100, 720, 520)
self.text_edit = QTextEdit(self)
self.text_edit.setFontPointSize(64)
self.text_edit.setPlainText("Add your text here...\n" * 100)
self.text_edit.installEventFilter(self)
self.speed_slider = QSlider(Qt.Orientation.Horizontal)
self.speed_slider.setRange(0, 94)
self.speed_slider.setValue(50)
self.speed_slider.valueChanged.connect(self.update_scroll_speed)
self.font_slider = QSlider(Qt.Orientation.Horizontal)
self.font_slider.setRange(20, 100)
self.font_slider.setValue(64)
self.font_slider.valueChanged.connect(self.update_font_size)
self.scroll_speed_label = QLabel()
self.font_size_label = QLabel()
layout = QVBoxLayout()
layout.addWidget(self.text_edit)
self.play_button = QPushButton(icon=QIcon(
self.style().standardIcon(QStyle.StandardPixmap.SP_MediaPlay)
))
self.play_button.clicked.connect(self.playback_update)
self.lock_text_button = QPushButton('Lock Text')
self.lock_text_button.clicked.connect(self.lock_text_button_update)
# Buttons
hlayout = QHBoxLayout()
hlayout.addWidget(self.play_button)
hlayout.addStretch(1)
hlayout.addWidget(self.lock_text_button)
layout.addLayout(hlayout)
# Font Slider
hlayout = QHBoxLayout()
hlayout.addWidget(QLabel('Font'))
hlayout.addWidget(self.font_slider)
hlayout.addWidget(self.font_size_label)
layout.addLayout(hlayout)
# Speed Slider
hlayout = QHBoxLayout()
hlayout.addWidget(QLabel('Speed'))
hlayout.addWidget(self.speed_slider)
hlayout.addWidget(self.scroll_speed_label)
layout.addLayout(hlayout)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
# Timer
self.scroll_timer = QTimer(self)
self.scroll_timer.timeout.connect(self.scroll_text)
self.update_scroll_speed(self.speed_slider.value())
self.update_font_size(self.font_slider.value())
def eventFilter(self, obj, event):
if event.type() == QEvent.Type.KeyPress and event.key() == Qt.Key.Key_Space:
if self.is_text_locked or self.scroll_timer.isActive():
self.playback_update()
return True
return super().eventFilter(obj, event)
def playback_update(self):
is_active = self.scroll_timer.isActive()
if is_active:
icon = QStyle.StandardPixmap.SP_MediaPlay
self.text_edit.setFocusPolicy(Qt.FocusPolicy.StrongFocus)
self.scroll_timer.stop()
else:
icon = QStyle.StandardPixmap.SP_MediaPause
self.text_edit.setFocusPolicy(Qt.FocusPolicy.NoFocus)
self.scroll_timer.start()
self.text_edit.setReadOnly(not is_active or self.is_text_locked)
self.play_button.setIcon(self.style().standardIcon(icon))
def lock_text_button_update(self):
self.is_text_locked = not self.is_text_locked
self.text_edit.setReadOnly(self.is_text_locked or self.scroll_timer.isActive())
if self.is_text_locked:
self.lock_text_button.setText('Unlock Text')
else:
self.lock_text_button.setText('Lock Text')
def scroll_text(self):
scroll_bar = self.text_edit.verticalScrollBar()
scroll_bar.setValue(scroll_bar.value() + 1) # Adjust the scroll step as needed
def update_font_size(self, value: int):
cursor = self.text_edit.textCursor()
self.text_edit.selectAll()
self.text_edit.setFontPointSize(value)
self.text_edit.setTextCursor(cursor)
self.font_size_label.setText('%d' % value)
def update_scroll_speed(self, value: int):
scroll_speed = (100 - value) * 1
#print('speed', self.scroll_speed, value)
self.scroll_speed_label.setText('%dms' % scroll_speed)
self.scroll_timer.setInterval(scroll_speed)
def main():
app = QApplication(sys.argv)
mainWindow = ScrollPrompterApp()
mainWindow.show()
sys.exit(app.exec())
if __name__ == '__main__':
main()
@matteobertozzi
Copy link
Author

Simple teleprompter
Screenshot 2024-06-30 at 06 47 58

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment