#!/usr/bin/python # -*- coding: utf-8 -*- """Put some text on the screen with Qt5. Mostly copied from /usr/share/doc/python-qt4-doc/examples/painting/painterpaths.py, but reduced to a minimum. The Qt4 version is in the version history. This runs in both Python 2.7 and Python 3.5. """ import sys # Supposedly this is necessary for Python 2? # import sip # sip.setapi("QVariant", 2) from PyQt5 import QtGui, Qt, QtWidgets def main(argv): app = QtWidgets.QApplication(argv) window = Window() window.show() return app.exec_() class Window(Qt.QWidget): def __init__(self): super(Window, self).__init__() self.text = QtGui.QPainterPath() self.text.addText(3, 70, QtGui.QFont("Times", 50), "&Qt") self.setLayout(Qt.QGridLayout()) # so the window resizes properly self.setWindowTitle("hello qt") self.setMinimumSize(128, 128) def paintEvent(self, event): painter = QtGui.QPainter(self) self.clear(painter) painter.setRenderHint(QtGui.QPainter.Antialiasing) # not blocky painter.setBrush(QtGui.QBrush(QtGui.QColor('black'))) # not hollow painter.drawPath(self.text) def clear(self, painter): """Doesn’t really work.""" background = QtGui.QPainterPath() background.moveTo(0, 0) background.lineTo(self.width(), 0) background.lineTo(self.width(), self.height()) background.lineTo(0, self.height()) background.closeSubpath() painter.setBrush(QtGui.QBrush(QtGui.QColor('white'))) painter.drawPath(background) if __name__ == '__main__': sys.exit(main(sys.argv))