Rotate Text (PySide)

vegaseat 2 Tallied Votes 2K Views Share

Just the very basics on how to draw a rotated text using the Python GUI toolkit PySide (public PyQt). Please experiment and embellish.

draw a rotated text with PySide's QPainter class

download PySide (LGPL-licensed version of PyQT) from:
or Windows binary from:

tested with Python27/34 and PySide122   by  vegaseat  26oct2014

from PySide.QtCore import *
from PySide.QtGui import *

class MyWindow(QWidget):

    def __init__(self, text, degrees):
        # QWidget will be self
        # setGeometry(x_pos, y_pos, width, height)
        # upper left corner coordinates (x_pos, y_pos)
        self.setGeometry(300, 100, 520, 520)
        self.setWindowTitle('Testing text rotation ...')
        self.text = text
        self.degrees = degrees

    def paintEvent(self, event):
        the method paintEvent() is called automatically
        the QPainter class does the low-level painting
        between its methods begin() and end()
        qp = QPainter()
        # set text color, default is black
        # QFont(family, size)
        qp.setFont(QFont('Decorative', 12))
        # start text at point (x, y)
        x = 30
        y = 30
        qp.translate(x, y)
        qp.drawText(0, 0, self.text)

text = '''\
You pass an elementary school playground and the
children are all busy with their cellphones.

# for command line replace [] with sys.argv
app = QApplication([])
# degrees of text rotation (clockwise)
degrees = 45
win = MyWindow(text, degrees)
# run the application event loop
sneekula 969 Nearly a Posting Maven

Works well, wonder why you need function translate()?

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Simply try it without translate.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.