hedgehog/software/ SimpleHud


Display things like the bank number, or perhaps other information. All this example will demonstrate is showing a number. Though it is naturally possible to adapt to show arbitrary text in a similar fashion.

Written using PySide6.

import sys
from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *
from PySide6.QtNetwork import *

HUDTIME=2000
KILLTIME=4000
class Hud(QWidget):
  def __init__(self):
    super().__init__()

    # 200x200 rectangle at the lower left corner of primary display
    w = 200
    h = 200
    self.resize(w,h)
    screen = QGuiApplication.primaryScreen()
    rect = screen.geometry()
    x = rect.width()-w
    y = rect.height()-h
    self.move(x,y)

    # attributes and defaults
    self.number = "My"
    self.font = QFont("Optima",80)
    self.color = QColor(100,255,100)
    self.ocolor = Qt.black
    self.pen = QPen(self.ocolor,16)
    self.showtime = 5
    self.timer = QTimer(self)
    self.dtimer = QTimer(self)
    self.showing = False
    self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
    self.setAttribute(Qt.WA_TranslucentBackground)
  def paintEvent(self,event):
    with QPainter(self) as p:
      rect = QRect(0,25,200,150)
      path = QPainterPath()
      path.addText(0,0,self.font,str(self.number))
      p.translate(0,150)
      p.setPen(self.pen)
      p.drawPath(path) # text outline, necessary for readability
      p.setBrush(self.color)
      p.setPen(Qt.NoPen)
      p.drawPath(path) # text fill
  def show(self,x):
    # show new value for HUDTIME millis, resetting timer if necessary
    self.number = x
    if not self.showing:
      super().show()
      self.showing = True
      self.timer.stop()
    self.update()
    self.timer.singleShot(HUDTIME,self.hide)
  def hide(self):
    if self.showing:
      self.showing = False
      super().hide()
    self.timer.stop()
  def test(self):
    timer = QTimer(self)
    self.show("My")
    timer.singleShot(1000, lambda *t: self.show("Xy"))
    self.dtimer.singleShot(KILLTIME,self.finish)
  def finish(self):
    app.quit()

app = QApplication(sys.argv)
hud = Hud()
hud.test()
exit(app.exec())