# EarthAndMoonQt.py (c) Kari Laitinen # http://www.naturalprogramming.com # 2009-11-03 File created. # 2009-11-03 Last modification. # This program shows, among other things, how texts can be drawn # with different fonts. import sys from PyQt4 import QtGui, QtCore class EarthAndMoonWindow( QtGui.QWidget ) : def __init__(self, parent=None ) : QtGui.QWidget.__init__( self, parent ) self.setGeometry( 300, 300, 600, 400 ) self.setWindowTitle( "BALLS DEPICTING EARTH AND MOON" ) self.large_font = QtGui.QFont( "Serif", 20, QtGui.QFont.Bold ) # 'True' as last parameter specifies that the font is italic. self.italic_font = QtGui.QFont( "Serif", 12, QtGui.QFont.Normal, True ) self.bold_italic_font = QtGui.QFont( "Serif", 10, QtGui.QFont.Bold, True ) def paintEvent( self, event ) : painter = QtGui.QPainter() painter.begin( self ) # We must set a brush in order to fill an ellipse with a color. painter.setBrush( QtCore.Qt.green ) painter.drawEllipse( 30, 30, 212, 212 ) # Earth painter.setBrush( QtCore.Qt.blue ) painter.drawEllipse( 400, 100, 58, 58 ) # Moon painter.setFont( self.large_font ) painter.drawText( 100, 270, "Earth" ) painter.drawText( 400, 190, "Moon" ) painter.setFont( self.italic_font ) painter.drawText( 30, 300, "Equatorial radius: 6380 km, 3960 miles" ) painter.drawText( 350, 220, "Radius: 1740 km, 1080 miles" ) painter.setFont( self.bold_italic_font ) ; painter.drawText( 200, 330, "Mean distance between Earth and Moon is" ) painter.drawText( 200, 345, "384 400 kilometers, 238 860 miles" ) painter.end() this_application = QtGui.QApplication( sys.argv ) application_window = EarthAndMoonWindow() application_window.show() this_application.exec_()