100% found this document useful (2 votes)
44 views

Rapid GUI Programming with Python and Qt 1st Edition Mark Summerfield pdf download

The document provides information about various ebooks available for download, including 'Rapid GUI Programming with Python and Qt' by Mark Summerfield. It includes links to other programming and historical books, along with details about the content and structure of the 'Rapid GUI Programming' book. Additionally, it mentions the publisher's contact information and sales options.

Uploaded by

wuriedutchq3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
44 views

Rapid GUI Programming with Python and Qt 1st Edition Mark Summerfield pdf download

The document provides information about various ebooks available for download, including 'Rapid GUI Programming with Python and Qt' by Mark Summerfield. It includes links to other programming and historical books, along with details about the content and structure of the 'Rapid GUI Programming' book. Additionally, it mentions the publisher's contact information and sales options.

Uploaded by

wuriedutchq3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Rapid GUI Programming with Python and Qt 1st

Edition Mark Summerfield download

https://ebookname.com/product/rapid-gui-programming-with-python-
and-qt-1st-edition-mark-summerfield/

Get Instant Ebook Downloads – Browse at https://ebookname.com


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

GUI Programming with Python QT Edition Boudewijn Rempt

https://ebookname.com/product/gui-programming-with-python-qt-
edition-boudewijn-rempt/

Programming in Python 3 A Complete Introduction to the


Python Language 2nd Edition Summerfield

https://ebookname.com/product/programming-in-python-3-a-complete-
introduction-to-the-python-language-2nd-edition-summerfield/

Programming Python 4th Edition Mark Lutz

https://ebookname.com/product/programming-python-4th-edition-
mark-lutz/

The Making of a Human Bomb An Ethnography of


Palestinian Resistance Nasser Abufarha

https://ebookname.com/product/the-making-of-a-human-bomb-an-
ethnography-of-palestinian-resistance-nasser-abufarha/
Medicine Creek Seventy Years of Archaeological
Investigations 1st Edition Donna C. Roper

https://ebookname.com/product/medicine-creek-seventy-years-of-
archaeological-investigations-1st-edition-donna-c-roper/

Swaziland Bradt Travel Guides 1st Edition Mike Unwin

https://ebookname.com/product/swaziland-bradt-travel-guides-1st-
edition-mike-unwin/

Voices from the Vietnam War Xiaobing Li

https://ebookname.com/product/voices-from-the-vietnam-war-
xiaobing-li/

Marx in Movement Operaismo in Context 1st Edition


Antonio Negri

https://ebookname.com/product/marx-in-movement-operaismo-in-
context-1st-edition-antonio-negri/

Encyclopedia of CAMPS AND GHETTOS 1933 1945 Vol 1 Part


B Early Camps Youth Camps and Concentration Camps and
Subcamps under the SS Business Administration Main
Office WVHA 1st Edition Geoffrey P. Megargee
https://ebookname.com/product/encyclopedia-of-camps-and-
ghettos-1933-1945-vol-1-part-b-early-camps-youth-camps-and-
concentration-camps-and-subcamps-under-the-ss-business-
Trade Unions and Migrant Workers New Contexts and
Challenges in Europe 1st Edition Stefania Marino

https://ebookname.com/product/trade-unions-and-migrant-workers-
new-contexts-and-challenges-in-europe-1st-edition-stefania-
marino/
Rapid GUI Programming
with Python and Qt
The Definitive Guide to PyQt Programming

Mark Summerfield

Upper Saddle River, NJ · Boston · Indianapolis · San Francisco


New York · Toronto · Montreal · London · Munich · Paris · Madrid
Capetown · Sydney · Tokyo · Singapore · Mexico City
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and the publisher was aware of a trademark
claim, the designations have been printed with initial capital letters or in all capitals.
The author and publisher have taken care in the preparation of this book, but make no expressed or implied
warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for
incidental or consequential damages in connection with or arising out of the use of the information or
programs contained herein.
The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or
special sales, which may include electronic versions and/or custom covers and content particular to your
business, training goals, marketing focus, and branding interests. For more information, please contact:
U.S. Corporate and Government Sales
(800) 382-3419
corpsales@pearsontechgroup.com
For sales outside the United States, please contact:
International Sales
international@pearsoned.com
Visit us on the Web: www.prenhallprofessional.com

Library of Congress Cataloging-in-Publication Data


Summerfield, Mark
Rapid GUI programming with Python and Qt : the definitive guide to PyQt programming / Mark
Summerfield.
p. cm.
Includes bibliographical references and index.
ISBN 978-0-13-235418-9 (hardcover : alk. paper)
1. Qt (Electronic resource) 2. Graphical user interfaces (Computer systems) 3. Python (Computer
program language) I. Title.
QA76.9.U83S89 2007
005.1’2—dc22
2007034852
Copyright © 2008 Pearson Education, Inc.
All rights reserved. Printed in the United States of America.
Trolltech®, Qt® and the Trolltech logo are registered trademarks of Trolltech ASA.
ISBN-13: 978-0-13-235418-9
ISBN-10: 0-13-235418-7
Text printed in the United States on recycled paper at Edwards Brothers in Ann Arbor, Michigan.
First printing, October 2007
This book is dedicated to
Andrea Summerfield
This page intentionally left blank
Contents

Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Part I: Python Programming

Chapter 1. Data Types and Data Structures . . . . . . . . . . . . . . . . . . . . . 9


Executing Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Variables and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Numbers and Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Integers and Long Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Floats and Decimals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Bytestrings, Unicode Strings, and QStrings . . . . . . . . . . . . . . . . . . 20
Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

Chapter 2. Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45


Conditional Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Looping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
List Comprehensions and Generators . . . . . . . . . . . . . . . . . . . . . . . 53
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Generator Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Using Keyword Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Lambda Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Dynamic Function Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Partial Function Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

vii
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

Chapter 3. Classes and Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75


Creating Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Methods and Special Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Static Data, and Static Methods and Decorators . . . . . . . . . . . . . 85
Example: The Length Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Collection Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Example: The OrderedDict Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Inheritance and Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Modules and Multifile Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Using the doctest Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108

Part II: Basic GUI Programming

Chapter 4. Introduction to GUI Programming . . . . . . . . . . . . . . . . . . 111


A Pop-Up Alert in 25 Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
An Expression Evaluator in 30 Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
A Currency Converter in 70 Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Signals and Slots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137

Chapter 5. Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139


Dumb Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Standard Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Modal OK/Cancel-Style Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Smart Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
Modeless Apply/Close-Style Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . 155
Modeless “Live” Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

Chapter 6. Main Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165


Creating a Main Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166
Actions and Key Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
Resource Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172

viii
Creating and Using Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Restoring and Saving the Main Window’s State . . . . . . . . . . . . . . 181
Handling User Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Handling File Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Handling Edit Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Handling Help Actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202

Chapter 7. Using Qt Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205


Designing User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Implementing Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Testing Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224

Chapter 8. Data Handling and Custom File Formats . . . . . . . . . . . . 227


Main Window Responsibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
Data Container Responsibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Saving and Loading Binary Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Writing and Reading Using QDataStream . . . . . . . . . . . . . . . . . . . 240
Writing and Reading Using the pickle Module . . . . . . . . . . . . . . . 246
Saving and Loading Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Writing and Reading Using QTextStream . . . . . . . . . . . . . . . . . . . 250
Writing and Reading Using the codecs Module . . . . . . . . . . . . . . . 255
Saving and Loading XML Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Writing XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Reading and Parsing XML with PyQt’s DOM Classes . . . . . . . . 259
Reading and Parsing XML with PyQt’s SAX Classes . . . . . . . . . 262
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266

Part III: Intermediate GUI Programming

Chapter 9. Layouts and Multiple Documents . . . . . . . . . . . . . . . . . . . . 269


Layout Policies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Tab Widgets and Stacked Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Extension Dialogs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276
Splitters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280
Single Document Interface (SDI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283

ix
Multiple Document Interface (MDI) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301

Chapter 10. Events, the Clipboard, and Drag and Drop . . . . . . . . . 303
The Event-Handling Mechanism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Reimplementing Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Using the Clipboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
Drag and Drop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Handling Custom Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318

Chapter 11. Custom Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321


Using Widget Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Creating Composite Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Subclassing Built-in Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Subclassing QWidget . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Example: A Fraction Slider . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331
Example: A Flow-Mixing Widget . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346

Chapter 12. Item-Based Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349


Custom and Interactive Graphics Items . . . . . . . . . . . . . . . . . . . . . . . . . 351
Animation and Complex Shapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379

Chapter 13. Rich Text and Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381


Rich Text Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
Using QSyntaxHighlighter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
A Rich Text Line Edit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389
Printing Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Printing Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400
Printing Documents Using HTML and QTextDocument . . . . . . 401
Printing Documents Using QTextCursor and QTextDocument 403
Printing Documents Using QPainter . . . . . . . . . . . . . . . . . . . . . . . . 407
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412

x
Chapter 14. Model/View Programming . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Using the Convenience Item Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Creating Custom Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Implementing the View Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
Implementing the Custom Model . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Creating Custom Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443

Chapter 15. Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445


Connecting to the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
Executing SQL Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
Using Database Form Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Using Database Table Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471

Part IV: Advanced GUI Programming

Chapter 16. Advanced Model/View Programming . . . . . . . . . . . . . . . 475


Custom Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
Generic Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
Representing Tabular Data in Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505

Chapter 17. Online Help and Internationalization . . . . . . . . . . . . . . 509


Online Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 510
Internationalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 520

Chapter 18. Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521


Creating a TCP Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Creating a TCP Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 534

xi
Chapter 19. Multithreading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
Creating a Threaded Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 539
Creating and Managing Secondary Threads . . . . . . . . . . . . . . . . . . . . . 544
Implementing a Secondary Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 552
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 557
Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 558
This Is Not Quite the End . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 559

Appendix A. Installing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561


Installing on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 561
Installing on Mac OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566
Installing on Linux and Unix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 570

Appendix B. Selected PyQt Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 575

Appendix C. Selected PyQt Class Hierarchies . . . . . . . . . . . . . . . . . . . 581

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 585

xii
Foreword
As PyQt’s creator, I’m delighted to see that this book has been written. Al-
though I served as one of the book’s technical reviewers, I’m happy to confess
that I learned a few things myself.
The PyQt documentation covers the APIs of all the PyQt classes. This book
shows you how to use all those classes, how to combine them to create dialogs,
main windows, and entire applications—all of which look good and work well,
with no arbitrary limits, and using a programming language that is a joy
to use.
What I particularly like about the book is that the examples aren’t trivial ones
designed to illustrate a simple point, but are potentially useful in their own
right. The way that different approaches are considered will reward the reader
who wants to develop a deeper understanding of how to apply PyQt to the
development of large scale, production quality applications.
I began the PyQt story back in the late 1990s. I had been using Tcl/Tk for
some time, but I felt that Tk applications looked ugly, especially when I saw
what had been achieved with the first version of KDE. I had wanted to switch
to Python, and so I thought I would combine the change of language with a
change of GUI library.
Initially I used some wrappers that had been written using SWIG, but I con-
cluded that I could produce a more suitable wrapper tool myself. I set to work
creating SIP, and released PyQt 0.1 supporting Qt 1.41 in November 1998.
Development has continued regularly ever since, both to keep up with new re-
leases of Qt and to broaden the scope of PyQt with, for example, the addition
of support tools and improved documentation. By 2000, PyQt 2.0 supported
Qt 2.2 on both Linux and Windows. Qt 3 support appeared in 2001, and
Mac OS X support in 2002. The PyQt4 series began with PyQt 4.0 in June 2006
with support for Qt 4.
My primary goal has always been to allow Python and Qt to work together in
a way that feels natural to Python programmers, while allowing them to do
anything they want in Python that can be done in C++. The key to achieving
this was the development of SIP. This gave me a specialized code generator
over which I had complete control and ensures that Python and Qt will always
fit snugly together.
The essential process of developing and maintaining PyQt is now well estab-
lished. Much of the work is now automated, which means that keeping up with

xiii
new releases of Qt from Trolltech is no longer the problem it once was, and en-
surs that PyQt will continue for years to come.
It’s been very gratifying to watch the growth of the PyQt community over the
years. If this book is part of your introduction to PyQt, then welcome!

— Phil Thompson
Wimborne, Dorset, U.K.
August 25, 2007

xiv
Introduction
This book teaches how to write GUI applications using the Python program-
ming language and the Qt application development framework. The only
essential prior knowledge is that you can program in some object-oriented pro-
gramming language, such as C++, C#, Java, or of course, Python itself. For the
rich text chapter, some familiarity with HTML and with regular expressions is
assumed, and the databases and threading chapters assume some basic knowl-
edge of those topics. A knowledge of GUI programming is not required, since
all the key concepts are covered.
The book will be useful to people who program professionally as part of their
job, whether as full-time software developers, or those from other disciplines,
including scientists and engineers, who need to do some programming in sup-
port of their work. It is also suitable for undergraduate and post-graduate stu-
dents, particularly those doing courses or research that includes a substantial
computing element. The exercises (with solutions) are provided especially to
help students.
Python is probably the easiest to learn and nicest scripting language in
widespread use, and Qt is probably the best library for developing GUI applica-
tions. The combination of Python and Qt, “PyQt”, makes it possible to develop
applications on any supported platform and run them unchanged on all the
supported platforms—for example, all modern versions of Windows, Linux,
Mac OS X, and most Unix-based systems. No compilation is required thanks
to Python being interpreted, and no source code changes to adapt to different
operating systems are required thanks to Qt abstracting away the platform-
specific details. We only have to copy the source file or files to a target machine
that has both Python and PyQt installed and the application will run.
If you are new to Python: Welcome! You are about to discover a language that
is clear to read and write, and that is concise without being cryptic. Python
supports many programming paradigms, but because our focus is on GUI
programming, we will take an object-oriented approach everywhere except in
the very early chapters.
Python is a very expressive language, which means that we can usually write
far fewer lines of Python code than would be required for an equivalent appli-
cation written in, say, C++ or Java. This makes it possible to show some small
but complete examples throughout the text, and makes PyQt an ideal tool for
rapidly and easily developing GUI applications, whether for prototyping or for
production use.

1
2 Introduction

Figure 1 The Eric4 IDE—a PyQt4 application

Since the emphasis of the book is on GUI programming, Part I provides a


fast-paced Python tutorial as well as some PyQt coverage. This material is
clearly marked (just like this paragraph, with “Qt” in the margin) to make
Qt it easy for experienced Python programmers to skip the Python they already
know. Parts II, III, and IV of the book are all PyQt-specific and assume that
readers can already program in Python, whether from previous experience or
from reading Part I.
Quite often in programming we reach decision points when there are several
possible approaches we could take. Reference books and the online documen-
tation identify what classes, methods, and functions are available, and in some
cases provide examples, but such documents rarely provide a broader context.
This book gives the necessary context, highlighting the key decision points for
GUI programming and offering insights into the pros and cons so that you can
decide for yourself what the right policy is for your particular circumstances.
For example, when you create a dialog, should it be modal or modeless? (See
Chapter 5 for an explanation and policy recommendations on this issue.)
PyQt is used to write all kinds of GUI applications, from accounting appli-
cations, to visualization tools used by scientists and engineers. Figure 1, for
example, shows Eric4, a powerful integrated development environment that is
written in PyQt. It is possible to write PyQt applications that are just tens of
lines long, and medium-size projects of 1 000 to 10 000 lines are very common.
Some commercial companies have built 100 000-line PyQt applications, with
Introduction 3

programming teams varying in size from just one person to more than a dozen
people. Many in-house tools are written using PyQt, but because these are of-
ten used to gain competitive advantage, the companies involved generally do
not permit their use of PyQt to be made public. PyQt is also widely used in the
open source world, with games, utilities, visualization tools, and IDEs all writ-
ten using it.
This book is specifically about PyQt4, the Python bindings for the Qt 4 C++
application development framework.★ PyQt4 is provided in the form of ten
Python modules which between them contain around 400 classes and about
6 000 methods and functions. All the example programs have been tested on
Windows, Linux, and Mac OS X, using Python 2.5, Qt 4.2, and PyQt 4.2, and
additionally on Windows and Linux using Qt 4.3 and PyQt 4.3. Backporting to
earlier versions is possible in some cases, but we recommend using the most
up-to-date versions of Python, Qt, and PyQt.
Python, PyQt, and Qt can be used free of charge for noncommercial purposes,
but the license used by Python is different from that used by PyQt and Qt.
Python is available with a very liberal license that allows it to be used to de-
velop both commercial and noncommercial applications. Both PyQt and Qt are
dual-licensed: This essentially allows them to be used to develop noncommer-
cial applications—which must in turn be licensed using an acceptable open
source license such as the GNU General Public License (GPL); or to be used to
develop commercial applications—in this case, a commercial PyQt license and
a commercial Qt license must be purchased.

The Structure of the Book

The book is divided into four parts. Part I is primarily a rapid conversion course
aimed at non-Python programmers who are familiar with an object-oriented
language, although it also has some (clearly marked) PyQt content. Because
the core Python language is mostly simple and is quite small, these chapters
can teach the basics of Python to a sufficient extent that real Python applica-
tions can be written.
If you think that you can pick up the Python syntax simply through reading
it, you might be tempted to skip Part I and dive straight into the GUI pro-
gramming that begins in Part II. The early chapters in Part II include back-
references to the relevant pages in Part I to support readers who choose this
approach. However, even for readers familiar with Python, we recommend
reading about QString in Chapter 1. If you are unfamiliar with partial function
application (currying), it is important to read the subsection that covers this in
Chapter 2, since this technique is sometimes used in GUI programming.


There are also Python bindings for the older Qt 3 library, but there is no reason to use that library
for new projects, especially since Qt 4 offers far more functionality and is easier to use.
4 Introduction

Part II begins by showing three tiny PyQt GUI applications to give an initial
impression of what PyQt programming is like. It also explains some of the
fundamental concepts involved in GUI programming, including PyQt’s high-
level signals and slots communication mechanism. Chapter 5 shows how to
create dialogs and how to create and lay out widgets (“controls” in Windows-
speak—the graphical elements that make up a user interface such as buttons,
listboxes, and such) in a dialog. Dialogs are central to GUI programming: Most
GUI applications have a single main window, and dozens or scores of dialogs,
so this topic is covered in depth.
After the dialogs chapter comes Chapter 6, which covers main windows,
including menus, toolbars, dock windows, and keyboard shortcuts, as well as
loading and saving application settings. Part II’s final chapters show how to
create dialogs using Qt Designer, Qt’s visual design tool, and how to save data
in binary, text, and XML formats.
Part III gives deeper coverage of some of the topics covered in Part II, and in-
troduces many new topics. Chapter 9 shows how to lay out widgets in quite
sophisticated ways, and how to handle multiple documents. Chapter 10 covers
low-level event handlers, and how to use the clipboard as well as drag and drop,
text, HTML, and binary data. Chapter 11 shows how to modify and subclass
existing widgets, and how to create entirely new widgets from scratch, with
complete control over their appearance and behavior. This chapter also shows
how to do basic graphics. Chapter 12 shows how to use Qt 4.2’s new graphics
view architecture, which is particularly suited to handling large numbers of in-
dependent graphical objects. Qt’s HTML-capable rich text engine is covered in
Chapter 13. This chapter also covers printing both to paper and to PDF files.
Part III concludes with two chapters on model/view programming: Chapter 14
introduces the subject and shows how to use Qt’s built-in views and how to
create custom data models and custom delegates, and Chapter 15 shows how
to use the model/view architecture to perform database programming.
Part IV continues the model/view theme, with coverage of three different
advanced model/view topics in Chapter 16. The first section of Chapter 17
describes the techniques that can be used for providing online help, and the
second section explains how to internationalize an application, including how
to use Qt’s translation tools to create translation files. The Python standard
library provides its own classes for networking and for threading, but in the
last two chapters of Part IV we show how to do networking and threading us-
ing PyQt’s classes.
Appendix A explains where Python, PyQt, and Qt can be obtained, and how to
install them on Windows, Mac OS X, and Linux. PyQt is much easier to learn
if you install it and try out some of the exercises, and if you inspect some of
the example code. Appendix B presents screenshots and brief descriptions
of selected PyQt widgets; this is helpful for those new to GUI programming.
Appendix C presents diagrams of some of PyQt’s key class hierarchies; this
Introduction 5

is useful for getting to know what classes PyQt has to offer and how they
are related.
If you have never used Python before, you should begin by reading Chapters
1–6 in order. If you already know Python, at least read the string policy (in
bullet points on page 28), and skim the material in Chapter 2 (apart from the
first section, which you’ll know well). Make sure that you are comfortable with
lambda and partial function application, both of which are covered in Chapter 2.
It is probably also worth skimming Chapter 3 as well. Then read Chapters 4,
5, and 6 in order.
Once you have covered the first six chapters, you have covered the essentials
of Python and the fundamentals of PyQt.
Chapter 7 is useful if you want to know how to create dialogs using a visual
design tool rather than purely by hand coding, something that can save a lot
of time. For file handling, at least read the first three sections of Chapter 8. If
you plan to write and read text files, also read Chapter 8’s fourth section, and
similarly the fifth section if you are going to use XML files.
For Part III, at the least read Chapter 10’s first section, on event handling, and
all of Chapter 11. Chapter 12 and the first section of Chapter 13 assume that
you have read about PyQt’s event handling, and that you have read Chapter 11.
Chapters 9 and 14 can be read stand-alone in this part, but Chapter 15 assumes
that you have read Chapter 14.
In Part IV, Chapter 16 assumes that you have read Chapters 14 and 15, but the
other chapters can be read independently.
If you find errors in the text or in the examples, or have other comments,
please write to mark@qtrac.eu quoting “PyQt book” in the subject line. The
book’s home page, where any corrections will be published, and from where the
examples and exercise solutions can be downloaded, is http://www.qtrac.eu/
pyqtbook.html.
If you want to participate in the PyQt community, it is worthwhile joining the
mailing list. Go to http://www.riverbankcomputing.com/mailman/listinfo/pyqt to
find a link to the archive, so that you can see what the mailing list is like, and
also for a form for joining. Python also has mailing lists and other community
activities. For these, go to http://www.python.org/community.

Acknowledgments

I have many people to thank, and I will begin with those who have been
intimately involved with the book.
Jasmin Blanchette is a senior software developer at Trolltech, a Qt expert, and
a fine editor and writer in his own right. I have cowritten two C++/Qt books
with him. Jasmin has made a huge number of suggestions and criticisms that
have immensely improved the quality of this book.
6 Introduction

David Boddie, Trolltech’s documentation manager, is an active PyQt open


source developer who has made many contributions to PyQt itself. His input
has helped ensure that I have covered everything necessary, and done so in a
sensible order.
Richard Chamberlain is cofounder and chief technology officer of Jadu Ltd., a
content management company. His feedback and insights have helped ensure
that the book is as broadly accessible as possible. He has also helped refine and
improve the code used in the examples and exercises.
Trenton Schulz is a Trolltech developer who has been a valuable reviewer of
my previous books. For this book, he has brought his Python and Qt knowledge
to bear, giving considerable feedback on the manuscript. Along with Richard,
he also ensured that Mac OS X users were never forgotten. In addition, he
spotted many subtle errors that I had missed.
Phil Thompson is PyQt’s creator and maintainer. He has been supportive of
the book from the start, even adding features and improvements to PyQt as
a direct result of discussions we have had regarding the book. He has made
numerous suggestions for the book’s improvement, and corrected many
mistakes and misunderstandings.
Special thanks to Samuel Rolland, who let me loose on his Mac laptop, to install
PyQt, test the examples, and take screenshots.
Thanks are also due to Guido van Rossum, creator of Python, as well as to the
wider Python community who have contributed so much to make Python, and
especially its libraries, so useful and enjoyable to use.
Thanks also to Trolltech, for developing and maintaining Qt, and in particular
to the Trolltech developers both past and present, many of whom I have had
the pleasure of working with, and who ensure that Qt is the best cross-platform
GUI development framework in existence.
Particular thanks to Jeff Kingston, creator of the Lout typesetting language.
I use Lout for all my books and for most of my other writing projects. Over
the years, Jeff has made many improvements and added numerous features to
Lout in response to feedback from users, including many that I have asked for
myself. Thanks also to James Cloos who created the condensed version of the
DejaVu Sans Mono font (itself derived from Jim Lyles’ Vera font) from which
this book’s monospaced font is derived.
The publisher, in the person of Editor-in-Chief Karen Gettman, was supportive
of this book from the very beginning. And special thanks to my editor, Debra
Williams-Cauley, for her support, and for making the entire process as smooth
as possible. Thanks also to Lara Wysong who managed the production process
so well, and to the proofreader, Audrey Doyle, who did such fine work.
Last but not least, I want to acknowledge my wife, Andrea. Her love, loyalty,
and support always give me strength and hope.
Part I

Python Programming
This page intentionally left blank
1
● Executing Python Code
● Variables and Objects
● Numbers and Strings
● Collections
● Built-in Functions

Data Types and Data Structures


In this chapter, we begin a Python conversion course that shows non-Python
programmers how to program with Python. We introduce some fundamental
data types and data structures, as well as some of Python’s procedural syntax.
The approach taken throughout is to emphasize realistic code like that used
in practice, rather than giving the formal definitions and explanations that
are already available in the documentation that is supplied with Python and
available online at http://www.python.org.

Figure 1.1 The IDLE Python Shell window

If you have not already installed Python and PyQt, it would be a good idea to
do so: That way you will be able to try out the examples that accompany this
book (downloadable from http://www.qtrac.eu/pyqtbook.html). See Appendix A
for installation details. One advantage of installing the software is that the
IDLE integrated development environment is installed along with Python.

9
10 Chapter 1. Data Types and Data Structures
Introduction

The IDLE Development Environment


The full installation of Python includes IDLE, a basic but very use-
ful integrated development environment. When IDLE is launched (click
Start→All Programs→Python 2.x→IDLE on Windows, or click Finder→Applica-
tions→MacPython 2.x→IDLE on Mac OS X, or run idle & in a console on Linux),
it presents its Python Shell window.
As the screenshot in Figure 1.1 shows, IDLE has a rather retro Windows 95
look. This is because it is written in Tkinter rather than in PyQt. We’ve
chosen to use IDLE because IDLE comes as standard with Python and is
very simple to learn and use. If you want to use a much more powerful and
modern-looking IDE, you might prefer Eric4 which is written in PyQt, or
one of the other Python IDEs that are available. However, if you are new to
Python, we recommend that you start out with the simpler IDLE, and once
you are more experienced with PyQt, then trying the other IDEs to see if you
prefer one of them. And of course, you could simply use a plain text editor
and debug using print statements and not use an IDE at all.
IDLE provides three key facilities: the ability to enter Python expressions
and code and to see the results directly in the Python Shell; a code editor
that provides Python-specific color syntax highlighting; and a debugger
that can be used to step through code to help identify and kill bugs. The
Python Shell is especially useful for trying out simple algorithms, snippets
of code, and regular expressions, and can also be used as a very powerful and
flexible calculator.

Executing Python Code


Before we can really explore the Python language we need to know how to
execute Python code. We will show this by reviewing a tiny example program
that is just one line long.
We must use a plain text editor for working with Python files.★ On Windows it
is possible to use Notepad, but IDLE includes a suitable Python editor designed
specifically for editing Python code: Simply start IDLE and then click File→New
Window.

We will type the following line into a file, called hello.py:

print "Hello World"

Note that no semicolon is necessary: In Python newline acts as a statement


separator. Also, we do not need a newline, “\n”, in the string, since print
automatically adds a newline unless we suppress it with a trailing comma.


The programs in this book are written using ASCII characters, with escape sequences where Uni-
code is required. It is possible to use Latin-1, UTF-8, or other encodings for strings and comments
in Python programs, as explained in the documentation under “Encoding declarations”.
Executing Python Code 11

Assuming that we have saved the code in the file hello.py (in the directory
C:\pyqt\chap01 if using Windows), we can start up a console (click Start→All Pro-
grams→Accessories→Console on Windows XP—sometimes Console is called Com-
mand Prompt; or run Terminal.app from /Applications/Utilities on Mac OS X),
change to that directory, and execute the program like this:

C:\>cd c:\pyqt\chap01
C:\pyqt\chap01>hello.py

As long as Python is correctly installed, Windows will recognize the .py file
extension and give the file to python.exe to execute. The program will print
“Hello World” on the console as we would expect.★
On Mac OS X and Linux we must explicitly run the interpreter by typing its
name and the file’s name at the console’s prompt, like this:

% python hello.py

This will work providing that Python is installed and in your PATH. Alternative-
ly, for Linux and Mac OS X, we can add an additional “shebang” (shell execute)
comment line which tells the operating system to use a Python interpreter,
making the hello.py file two lines long:

#!/usr/bin/env python
print "Hello World"

For this to work on Mac OS X and Linux, the file’s permissions must be set
correctly. For example, at the console prompt in the same directory as the file,
enter chmod +x hello.py to make the file executable.
Python comments start with “#” and continue until the end of the line. This
means that it is perfectly safe to add the “shebang” line to all Python programs,
since the comment is ignored on Windows but on Linux it tells the operating
system to execute the file using a Python interpreter. Appendix A shows how
to associate the Python interpreter with .py and .pyw files on Mac OS X.
When we speak of executing a Python program, what happens behind the
scenes is that Python reads the .py (or .pyw) file into memory, and parses it, to
get a bytecode program that it then goes on to execute. For each module that is
imported by the program, Python first checks to see whether there is a precom-
piled bytecode version (in a .pyo or .pyc file) that has a timestamp which corre-
sponds to its .py file. If there is, Python uses the bytecode version; otherwise, it
parses the module’s .py file, saves it into a .pyc file, and uses the bytecode it just
generated. So, unlike Java, we don’t have to explicitly bytecode-compile any
modules, whether they are supplied with Python or are ones we have written
ourselves. And in most Python installations, the supplied modules are com-


Mac OS X users note that whenever we refer to a console, this is the same as a Mac Terminal.
12 Chapter 1. Data Types and Data Structures

piled as part of the installation process so as to avoid having to compile them


whenever a Python application that uses them is run.

Variables and Objects


In most programming languages, including C++ and Java, we must declare
each variable, specifying its type, before it can be used. This is called static
typing, because the compiler knows at compile time what type each variable is.
Python, like most very high level languages, uses a different approach: Vari-
ables have no type restrictions (dynamic typing), and they don’t need to be
declared.
We could learn about Python’s variables and identifiers by creating and
executing a file, as we did with hello.py in the preceding section. But for trying
out small code snippets we don’t need to create a file at all. We can just enter
the lines directly in the IDLE Python Shell window at the >>> prompt:

>>> x = 71
>>> y = "Dove"

The whitespace around the assignment operator = is optional but is included


because it makes the code easier to read. As a matter of style we will always
put one space before and after binary operators. On the other hand, it is
important that each statement occupies its own line and has no extraneous
leading whitespace. This is because Python uses indentation and line breaks
to signify its block structure, rather than the braces and semicolons used by
many other programming languages.
Now we are ready to review what the two lines actually do. The first line
creates an object of type int and binds the name x to it.★ The second line creates
an object of type str (an 8-bit string type) and binds the name y to it.
Some Python programmers refer to names (such as the x and y used earlier),
as object references since they refer to objects rather than being objects in their
own right. For basic data types like int and str it makes no difference whether
we see their variables as “objects” or as “object references”; they behave in the
same way as they do in other programming languages:

>>> x = 82
>>> x += 7
>>> x
89

Later on we will see cases where the fact that Python variables are object Lists
references makes a difference. ☞ 31


This is similar to the Java assignment, Integer x = new Integer(71); for C++ a near-equivalent
would be int xd = 71; int &x = xd;.
Discovering Diverse Content Through
Random Scribd Documents
accident some time since; the King, Lord Mayor and Sheriffs, being
there with great banquet.
11th June, 1666. Trinity Monday, after a sermon, applied to the
remeeting of the Corporation of the Trinity-House, after the late
raging and wasting pestilence: I dined with them in their new room
in Deptford, the first time since it was rebuilt.
15th June, 1666. I went to Chatham.—16th. In the Jemmy yacht
(an incomparable sailer) to sea, arrived by noon at the fleet at the
Buoy at the Nore, dined with Prince Rupert and the General.
17th June, 1666. Came his Majesty, the Duke, and many
Noblemen. After Council, we went to prayers. My business being
dispatched, I returned to Chatham, having lain but one night in the
Royal Charles; we had a tempestuous sea. I went on shore at
Sheerness, where they were building an arsenal for the fleet, and
designing a royal fort with a receptacle for great ships to ride at
anchor; but here I beheld the sad spectacle, more than half that
gallant bulwark of the kingdom miserably shattered, hardly a vessel
entire, but appearing rather so many wrecks and hulls, so cruelly
had the Dutch mangled us. The loss of the Prince, that gallant
vessel, had been a loss to be universally deplored, none knowing for
what reason we first engaged in this ungrateful war; we lost besides
nine or ten more, and near 600 men slain and 1,100 wounded,
2,000 prisoners; to balance which, perhaps we might destroy
eighteen or twenty of the enemy's ships, and 700 or 800 poor men.
18th June, 1666. Weary of this sad sight, I returned home.
2d July, 1666. Came Sir John Duncomb and Mr. Thomas
Chicheley, both Privy Councillors and Commissioners of His Majesty's
Ordnance, to visit me, and let me know that his Majesty had in
Council, nominated me to be one of the Commissioners for
regulating the farming and making of saltpetre through the whole
kingdom, and that we were to sit in the Tower the next day. When
they were gone, came to see me Sir John Cotton, heir to the famous
antiquary, Sir Robert Cotton: a pretended great Grecian, but had by
no means the parts, or genius of his grandfather.
3d July, 1666. I went to sit with the Commissioners at the Tower,
where our commission being read, we made some progress in
business, our Secretary being Sir George Wharton, that famous
mathematician who wrote the yearly Almanac during his Majesty's
troubles. Thence, to Painters' Hall, to our other commission, and
dined at my Lord Mayor's.
4th July, 1666. The solemn Fast-day. Dr. Meggot preached an
excellent discourse before the King on the terrors of God's
judgments. After sermon, I waited on my Lord Archbishop of
Canterbury and Bishop of Winchester, where the Dean of
Westminster spoke to me about putting into my hands the disposal
of fifty pounds, which the charitable people of Oxford had sent to be
distributed among the sick and wounded seamen since the battle.
Hence, I went to the Lord Chancellor's to joy him of his Royal
Highness's second son, now born at St. James's; and to desire the
use of the Star-chamber for our Commissioners to meet in, Painters'
Hall not being so convenient.
12th July, 1666. We sat the first time in the Star-chamber. There
was now added to our commission Sir George Downing (one that
had been a great ... against his Majesty, but now insinuated into his
favor; and, from a pedagogue and fanatic preacher, not worth a
groat, had become excessively rich), to inspect the hospitals and
treat about prisons.
14th July, 1666. Sat at the Tower with Sir J. Duncomb and Lord
Berkeley, to sign deputations for undertakers to furnish their
proportions of saltpetre.
17th July, 1666. To London, to prepare for the
LONDON
next engagement of the fleets, now gotten to sea
again.
22d July, 1666. Our parish still infected with the contagion.
25th July, 1666. The fleets engaged. I dined at Lord Berkeley's, at
St. James's, where dined my Lady Harrietta Hyde, Lord Arlington,
and Sir John Duncomb.
29th July, 1666. The pestilence now fresh increasing in our parish,
I forbore going to church. In the afternoon came tidings of our
victory over the Dutch, sinking some, and driving others aground,
and into their ports.
1st August, 1666. I went to Dr. Keffler, who married the daughter
of the famous chemist, Drebbell,4 inventor of the bodied scarlet. I
went to see his iron ovens, made portable (formerly) for the Prince
of Orange's army: supped at the Rhenish Wine-House with divers
Scots gentlemen.
6th August, 1666. Dined with Mr. Povey, and then went with him
to see a country house he had bought near Brentford; returning by
Kensington; which house stands to a very graceful avenue of trees,
but it is an ordinary building, especially one part.
8th August, 1666. Dined at Sir Stephen Fox's with several friends
and, on the 10th, with Mr. Odart, Secretary of the Latin tongue.
17th August, 1666. Dined with the Lord Chancellor, whom I
entreated to visit the Hospital of the Savoy, and reduce it (after the
great abuse that had been continued) to its original institution for
the benefit of the poor, which he promised to do.
25th August, 1666. Waited on Sir William D'Oyly, now recovered,
as it were, miraculously. In the afternoon, visited the Savoy Hospital,
where I stayed to see the miserably dismembered and wounded
men dressed, and gave some necessary orders. Then to my Lord
Chancellor, who had, with the Bishop of London and others in the
commission, chosen me one of the three surveyors of the repairs of
Paul's, and to consider of a model for the new building, or, if it might
be, repairing of the steeple, which was most decayed.
26th August, 1666. The contagion still continuing, we had the
Church service at home.
27th August, 1666. I went to St. Paul's church, where, with Dr.
Wren, Mr. Pratt, Mr. May, Mr. Thomas Chicheley, Mr. Slingsby, the
Bishop of London, the Dean of St. Paul's, and several expert
workmen, we went about to survey the general decays of that
ancient and venerable church, and to set down in writing the
particulars of what was fit to be done, with the charge thereof,
giving our opinion from article to article. Finding the main building to
recede outward it was the opinion of Chicheley and Mr. Pratt that it
had been so built ab origine for an effect in perspective, in regard of
the height; but I was, with Dr. Wren, quite of another judgment, and
so we entered it; we plumbed the uprights in several places. When
we came to the steeple, it was deliberated whether it were not well
enough to repair it only on its old foundation, with reservation to the
four pillars; this Mr. Chicheley and Mr. Pratt were also for, but we
totally rejected it, and persisted that it required a new foundation,
not only in regard of the necessity, but for that the shape of what
stood was very mean, and we had a mind to build it with a noble
cupola, a form of church-building not as yet known in England, but
of wonderful grace. For this purpose, we offered to bring in a plan
and estimate, which after much contest, was at last assented to, and
that we should nominate a committee of able workmen to examine
the present foundation. This concluded, we drew all up in writing,
and so went with my Lord Bishop to the Dean's.
28th August, 1666. Sat at the Star-chamber. Next day, to the
Royal Society, where one Mercator, an excellent mathematician,
produced his rare clock and new motion to perform the equations,
and Mr. Rooke, his new pendulum.
2d September, 1666. This fatal night, about ten,
LONDON
began the deplorable fire, near Fish street, in
London.
3d September, 1666. I had public prayers at home. The fire
continuing, after dinner, I took coach with my wife and son, and
went to the Bankside in Southwark, where we beheld that dismal
spectacle, the whole city in dreadful flames near the waterside; all
the houses from the Bridge, all Thames street, and upward toward
Cheapside, down to the Three Cranes, were now consumed; and so
returned, exceedingly astonished what would become of the rest.
The fire having continued all this night (if I may call that night
which was light as day for ten miles round about, after a dreadful
manner), when conspiring with a fierce eastern wind in a very dry
season, I went on foot to the same place; and saw the whole south
part of the city burning from Cheapside to the Thames, and all along
Cornhill (for it likewise kindled back against the wind as well as
forward), Tower street, Fenchurch street, Gracious street, and so
along to Baynard's Castle, and was now taking hold of St. Paul's
church, to which the scaffolds contributed exceedingly. The
conflagration was so universal, and the people so astonished, that,
from the beginning, I know not by what despondency, or fate, they
hardly stirred to quench it; so that there was nothing heard, or seen,
but crying out and lamentation, running about like distracted
creatures, without at all attempting to save even their goods; such a
strange consternation there was upon them, so as it burned both in
breadth and length, the churches, public halls, Exchange, hospitals,
monuments, and ornaments; leaping after a prodigious manner,
from house to house, and street to street, at great distances one
from the other. For the heat, with a long set of fair and warm
weather, had even ignited the air, and prepared the materials to
conceive the fire, which devoured, after an incredible manner,
houses, furniture, and every thing. Here, we saw the Thames
covered with goods floating, all the barges and boats laden with
what some had time and courage to save, as, on the other side, the
carts, etc., carrying out to the fields, which for many miles were
strewn with movables of all sorts, and tents erecting to shelter both
people and what goods they could get away. Oh, the miserable and
calamitous spectacle! such as haply the world had not seen since the
foundation of it, nor can be outdone till the universal conflagration
thereof. All the sky was of a fiery aspect, like the top of a burning
oven, and the light seen above forty miles round about for many
nights. God grant mine eyes may never behold the like, who now
saw above 10,000 houses all in one flame! The noise and cracking
and thunder of the impetuous flames, the shrieking of women and
children, the hurry of people, the fall of towers, houses, and
churches, was like a hideous storm; and the air all about so hot and
inflamed, that at the last one was not able to approach it, so that
they were forced to stand still, and let the flames burn on, which
they did, for near two miles in length and one in breadth. The clouds
also of smoke were dismal, and reached, upon computation, near
fifty miles in length. Thus, I left it this afternoon burning, a
resemblance of Sodom, or the last day. It forcibly called to my mind
that passage—"non enim hic habemus stabilem civitatem"; the ruins
resembling the picture of Troy. London was, but is no more! Thus, I
returned.
4th September, 1666. The burning still rages, and it is now gotten
as far as the Inner Temple. All Fleet street, the Old Bailey, Ludgate
hill, Warwick lane, Newgate, Paul's chain, Watling street, now
flaming, and most of it reduced to ashes; the stones of Paul's flew
like grenados, the melting lead running down the streets in a
stream, and the very pavements glowing with fiery redness, so as no
horse, nor man, was able to tread on them, and the demolition had
stopped all the passages, so that no help could be applied. The
eastern wind still more impetuously driving the flames forward.
Nothing but the Almighty power of God was able to stop them; for
vain was the help of man.
5th September, 1666. It crossed toward Whitehall; but oh! the
confusion there was then at that Court! It pleased his Majesty to
command me, among the rest, to look after the quenching of Fetter-
lane end, to preserve (if possible) that part of Holborn, while the
rest of the gentlemen took their several posts, some at one part, and
some at another (for now they began to bestir themselves, and not
till now, who hitherto had stood as men intoxicated, with their hands
across), and began to consider that nothing was likely to put a stop
but the blowing up of so many houses as might make a wider gap
than any had yet been made by the ordinary method of pulling them
down with engines. This some stout seamen proposed early enough
to have saved near the whole city, but this some tenacious and
avaricious men, aldermen, etc., would not permit, because their
houses must have been of the first. It was, therefore, now
commended to be practiced; and my concern being particularly for
the Hospital of St. Bartholomew, near Smithfield, where I had many
wounded and sick men, made me the more diligent to promote it;
nor was my care for the Savoy less. It now pleased God, by abating
the wind, and by the industry of the people, when almost all was
lost infusing a new spirit into them, that the fury of it began sensibly
to abate about noon, so as it came no farther than the Temple
westward, nor than the entrance of Smithfield, north: but continued
all this day and night so impetuous toward Cripplegate and the
Tower, as made us all despair. It also broke out again in the temple;
but the courage of the multitude persisting, and many houses being
blown up, such gaps and desolations were soon made, as, with the
former three days' consumption, the back fire did not so vehemently
urge upon the rest as formerly. There was yet no standing near the
burning and glowing ruins by near a furlong's space.
The coal and wood wharfs, and magazines of oil, rosin, etc., did
infinite mischief, so as the invective which a little before I had
dedicated to his Majesty and published,5 giving warning what
probably might be the issue of suffering those shops to be in the city
was looked upon as a prophecy.
The poor inhabitants were dispersed about St. George's Fields,
and Moorfields, as far as Highgate, and several miles in circle, some
under tents, some under miserable huts and hovels, many without a
rag, or any necessary utensils, bed or board, who from delicateness,
riches, and easy accommodations in stately and well-furnished
houses, were now reduced to extreme misery and poverty.
In this calamitous condition, I returned with a sad heart to my
house, blessing and adoring the distinguishing mercy of God to me
and mine, who, in the midst of all this ruin, was like Lot, in my little
Zoar, safe and sound.
6th September, 1666. Thursday. I represented to his Majesty the
case of the French prisoners at war in my custody, and besought him
that there might be still the same care of watching at all places
contiguous to unseized houses. It is not indeed imaginable how
extraordinary the vigilance and activity of the King and the Duke
was, even laboring in person, and being present to command, order,
reward, or encourage workmen; by which he showed his affection to
his people, and gained theirs. Having, then, disposed of some under
cure at the Savoy, I returned to Whitehall, where I dined at Mr.
Offley's, the groom-porter, who was my relation.
7th September, 1666. I went this morning on
LONDON
foot from Whitehall as far as London Bridge,
through the late Fleet street, Ludgate hill by St. Paul's, Cheapside,
Exchange, Bishops-gate, Aldersgate, and out to Moorfields, thence
through Cornhill, etc., with extraordinary difficulty, clambering over
heaps of yet smoking rubbish, and frequently mistaking where I
was; the ground under my feet so hot, that it even burnt the soles of
my shoes. In the meantime, his Majesty got to the Tower by water,
to demolish the houses about the graff, which, being built entirely
about it, had they taken fire and attacked the White Tower, where
the magazine of powder lay, would undoubtedly not only have
beaten down and destroyed all the bridge, but sunk and torn the
vessels in the river, and rendered the demolition beyond all
expression for several miles about the country.
At my return, I was infinitely concerned to find that goodly
Church, St. Paul's—now a sad ruin, and that beautiful portico (for
structure comparable to any in Europe, as not long before repaired
by the late King) now rent in pieces, flakes of large stones split
asunder, and nothing remaining entire but the inscription in the
architrave showing by whom it was built, which had not one letter of
it defaced! It was astonishing to see what immense stones the heat
had in a manner calcined, so that all the ornaments, columns,
friezes, capitals, and projectures of massy Portland stone, flew off,
even to the very roof, where a sheet of lead covering a great space
(no less than six acres by measure) was totally melted. The ruins of
the vaulted roof falling, broke into St. Faith's, which being filled with
the magazines of books belonging to the Stationers, and carried
thither for safety, they were all consumed, burning for a week
following. It is also observable that the lead over the altar at the
east end was untouched, and among the divers monuments the
body of one bishop remained entire. Thus lay in ashes that most
venerable church, one of the most ancient pieces of early piety in
the Christian world, besides near one hundred more. The lead,
ironwork, bells, plate, etc., melted, the exquisitely wrought Mercers'
Chapel, the sumptuous Exchange, the august fabric of Christ Church,
all the rest of the Companies' Halls, splendid buildings, arches,
entries, all in dust; the fountains dried up and ruined, while the very
waters remained boiling; the voragos of subterranean cellars, wells,
and dungeons, formerly warehouses, still burning in stench and dark
clouds of smoke; so that in five or six miles traversing about I did
not see one load of timber unconsumed, nor many stones but what
were calcined white as snow.
The people, who now walked about the ruins, appeared like men
in some dismal desert, or rather, in some great city laid waste by a
cruel enemy; to which was added the stench that came from some
poor creatures' bodies, beds, and other combustible goods. Sir
Thomas Gresham's statue, though fallen from its niche in the Royal
Exchange, remained entire, when all those of the Kings since the
Conquest were broken to pieces. Also the standard in Cornhill, and
Queen Elizabeth's effigies, with some arms on Ludgate, continued
with but little detriment, while the vast iron chains of the city streets,
hinges, bars, and gates of prisons, were many of them melted and
reduced to cinders by the vehement heat. Nor was I yet able to pass
through any of the narrow streets, but kept the widest; the ground
and air, smoke and fiery vapor, continued so intense, that my hair
was almost singed, and my feet insufferably surbated. The by-lanes
and narrow streets were quite filled up with rubbish; nor could one
have possibly known where he was, but by the ruins of some
Church, or Hall, that had some remarkable tower, or pinnacle
remaining.
I then went towards Islington and Highgate, where one might
have seen 200,000 people of all ranks and degrees dispersed, and
lying along by their heaps of what they could save from the fire,
deploring their loss; and, though ready to perish for hunger and
destitution, yet not asking one penny for relief, which to me
appeared a stranger sight than any I had yet beheld. His Majesty
and Council indeed took all imaginable care for their relief, by
proclamation for the country to come in, and refresh them with
provisions.
In the midst of all this calamity and confusion, there was, I know
not how, an alarm begun that the French and Dutch, with whom we
were now in hostility, were not only landed, but even entering the
city. There was, in truth, some days before, great suspicion of those
two nations joining; and now that they had been the occasion of
firing the town. This report did so terrify, that on a sudden there was
such an uproar and tumult that they ran from their goods, and,
taking what weapons they could come at, they could not be stopped
from falling on some of those nations whom they casually met,
without sense or reason. The clamor and peril grew so excessive,
that it made the whole Court amazed, and they did with infinite
pains and great difficulty, reduce and appease the people, sending
troops of soldiers and guards, to cause them to retire into the fields
again, where they were watched all this night. I left them pretty
quiet, and came home sufficiently weary and broken. Their spirits
thus a little calmed, and the affright abated, they now began to
repair into the suburbs about the city, where such as had friends, or
opportunity, got shelter for the present to which his Majesty's
proclamation also invited them.
Still, the plague continuing in our parish, I could not, without
danger, adventure to our church.
10th September, 1666. I went again to the ruins; for it was now
no longer a city.
13th September, 1666. I presented his Majesty with a survey of
the ruins, and a plot for a new city, with a discourse on it;
whereupon, after dinner, his Majesty sent for me into the Queen's
bed-chamber, her Majesty and the Duke only being present. They
examined each particular, and discoursed on them for near an hour,
seeming to be extremely pleased with what I had so early thought
on. The Queen was now in her cavalier riding-habit, hat and feather,
and horseman's coat, going to take the air.
16th September, 1666. I went to Greenwich Church, where Mr.
Plume preached very well from this text: "Seeing, then, all these
things shall be dissolved," etc.: taking occasion from the late
unparalleled conflagration to remind us how we ought to walk more
holy in all manner of conversation.
27th September, 1666. Dined at Sir William D'Oyly's, with that
worthy gentleman, Sir John Holland, of Suffolk.
10th October, 1666. This day was ordered a general Fast through
the Nation, to humble us on the late dreadful conflagration, added to
the plague and war, the most dismal judgments that could be
inflicted; but which indeed we highly deserved for our prodigious
ingratitude, burning lusts, dissolute court, profane and abominable
lives, under such dispensations of God's continued favor in restoring
Church, Prince, and People from our late intestine calamities, of
which we were altogether unmindful, even to astonishment. This
made me resolve to go to our parish assembly, where our Doctor
preached on Luke xix. 41: piously applying it to the occasion. After
which, was a collection for the distressed losers in the late fire.
18th October, 1666. To Court. It being the first time his Majesty
put himself solemnly into the Eastern fashion of vest, changing
doublet, stiff collar, bands and cloak, into a comely dress, after the
Persian mode, with girdles or straps, and shoestrings and garters
into buckles, of which some were set with precious stones6 resolving
never to alter it, and to leave the French mode, which had hitherto
obtained to our great expense and reproach. Upon which, divers
courtiers and gentlemen gave his Majesty gold by way of wager that
he would not persist in this resolution. I had sometime before
presented an invective against that unconstancy, and our so much
affecting the French fashion, to his Majesty; in which I took occasion
to describe the comeliness and usefulness of the Persian clothing, in
the very same manner his Majesty now clad himself. This pamphlet I
entitled "Tyrannus, or the Mode," and gave it to the King to read. I
do not impute to this discourse the change which soon happened,
but it was an identity that I could not but take notice of.
This night was acted my Lord Broghill's tragedy, called
"Mustapha," before their Majesties at Court, at which I was present;
very seldom going to the public theatres for many reasons now, as
they were abused to an atheistical liberty; foul and indecent women
now (and never till now) permitted to appear and act, who inflaming
several young noblemen and gallants, became their misses, and to
some, their wives. Witness the Earl of Oxford, Sir R. Howard, Prince
Rupert, the Earl of Dorset, and another greater person than any of
them, who fell into their snares, to the reproach of their noble
families, and ruin of both body and soul.7 I was invited by my Lord
Chamberlain to see this tragedy, exceedingly well written, though in
my mind I did not approve of any such pastime in a time of such
judgments and calamities.
21st October, 1666. This season, after so long and extraordinary a
drought in August and September, as if preparatory for the dreadful
fire, was so very wet and rainy as many feared an ensuing famine.
28th October, 1666. The pestilence, through God's mercy, began
now to abate considerably in our town.
30th October, 1666. To London to our office, and
LONDON
now had I on the vest and surcoat, or tunic, as it
was called, after his Majesty had brought the whole court to it. It
was a comely and manly habit, too good to hold, it being impossible
for us in good earnest to leave the Monsieurs' vanities long.
31st October, 1666. I heard the signal cause of my Lord Cleveland
pleaded before the House of Lords; and was this day forty-six years
of age, wonderfully protected by the mercies of God, for which I
render him immortal thanks.
14th November, 1666. I went my winter circle through my district,
Rochester and other places, where I had men quartered, and in
custody.
15th November, 1666. To Leeds Castle.
16th November, 1666. I mustered the prisoners, being about 600
Dutch and French, ordered their proportion of bread to be
augmented and provided clothes and fuel. Monsieur Colbert,
Ambassador at the Court of England, this day sent money from his
master, the French King, to every prisoner of that nation under my
guard.
17th November, 1666. I returned to Chatham, my chariot
overturning on the steep of Bexley Hill, wounded me in two places
on the head; my son, Jack, being with me, was like to have been
worse cut by the glass; but I thank God we both escaped without
much hurt, though not without exceeding danger.
18th November, 1666. At Rochester.
19th November, 1666. Returned home.
23d November, 1666. At London, I heard an extraordinary case
before a Committee of the whole House of Commons, in the
Commons' House of Parliament, between one Captain Taylor and my
Lord Viscount Mordaunt, where, after the lawyers had pleaded and
the witnesses been examined, such foul and dishonorable things
were produced against his Lordship, of tyranny during his
government of Windsor Castle, of which he was Constable,
incontinence, and suborning witnesses (of which last, one Sir
Richard Breames was most concerned), that I was exceedingly
interested for his Lordship, who was my special friend, and husband
of the most virtuous lady in the world. We sat till near ten at night,
and yet but half the counsel had done on behalf of the plaintiff. The
question then was put for bringing in of lights to sit longer. This
lasted so long before it was determined, and raised such a confused
noise among the members, that a stranger would have been
astonished at it. I admire that there is not a rationale to regulate
such trifling accidents, which consume much time, and is a reproach
to the gravity of so great an assembly of sober men.
27th November, 1666. Sir Hugh Pollard, Comptroller of the
Household, died at Whitehall, and his Majesty conferred the white
staff on my brother Commissioner for sick and wounded, Sir Thomas
Clifford, a bold young gentleman, of a small fortune in Devon, but
advanced by Lord Arlington, Secretary of State, to the great
astonishment of all the Court. This gentleman was somewhat related
to me by the marriage of his mother to my nearest kinsman,
Gregory Coale, and was ever my noble friend, a valiant and daring
person, but by no means fit for a supple and flattering courtier.
28th November, 1666. Went to see Clarendon House, now almost
finished, a goodly pile to see, but had many defects as to the
architecture, yet placed most gracefully. After this, I waited on the
Lord Chancellor, who was now at Berkshire House, since the burning
of London.
2d December, 1666. Dined with me Monsieur Kiviet, a Dutch
gentleman-pensioner of Rotterdam, who came over for protection,
being of the Prince of Orange's party, now not welcome in Holland.
The King knighted him for some merit in the Prince's behalf. He
should, if caught, have been beheaded with Monsieur Buat, and was
brother-in-law to Van Tromp, the sea-general. With him came Mr.
Gabriel Sylvius, and Mr. Williamson, secretary to Lord Arlington;
M. Kiviet came to examine whether the soil about the river of
Thames would be proper to make clinker bricks, and to treat with
me about some accommodation in order to it.
9th January, 1666-67. To the Royal Society, which since the sad
conflagration were invited by Mr. Howard to sit at Arundel-House in
the Strand, who at my instigation likewise bestowed on the Society
that noble library which his grandfather especially, and his ancestors
had collected. This gentleman had so little inclination to books, that
it was the preservation of them from embezzlement.
24th January, 1667. Visited my Lord Clarendon, and presented my
son, John, to him, now preparing to go to Oxford, of which his
Lordship was Chancellor. This evening I heard rare Italian voices,
two eunuchs and one woman, in his Majesty's green chamber, next
his cabinet.
29th January, 1667. To London, in order to my
LONDON
son's Oxford journey, who, being very early
entered both in Latin and Greek, and prompt to learn beyond most
of his age, I was persuaded to trust him under the tutorage of Mr.
Bohun, Fellow of New College, who had been his preceptor in my
house some years before; but, at Oxford, under the inspection of Dr.
Bathurst, President of Trinity College, where I placed him, not as yet
thirteen years old. He was newly out of long coats.8
15th February, 1667. My little book, in answer to Sir George
Mackenzie on Solitude, was now published, entitled "Public
Employment, and an active Life with its Appanages, preferred to
Solitude."9
18th February, 1667. I was present at a magnificent ball, or
masque, in the theatre at the Court, where their Majesties and all
the great lords and ladies danced, infinitely gallant, the men in their
richly embroidered, most becoming vests.
19th February, 1667. I saw a comedy acted at Court. In the
afternoon, I witnessed a wrestling match for £1,000 in St. James's
Park, before his Majesty, a vast assemblage of lords and other
spectators, between the western and northern men, Mr. Secretary
Morice and Lord Gerard being the judges. The western men won.
Many great sums were betted.
6th March, 1667. I proposed to my Lord Chancellor, Monsieur
Kiviet's undertaking to wharf the whole river of Thames, or quay,
from the Temple to the Tower, as far as the fire destroyed, with
brick, without piles, both lasting and ornamental.—Great frosts,
snow and winds, prodigious at the vernal equinox; indeed it had
been a year of prodigies in this nation, plague, war, fire, rain,
tempest and comet.

14th March, 1667. Saw "The Virgin Queen,"10 a play written by


Mr. Dryden.
22d March, 1667. Dined at Mr. Secretary Morice's, who showed
me his library, which was a well chosen collection. This afternoon, I
had audience of his Majesty, concerning the proposal I had made of
building the quay.
26th March, 1667. Sir John Kiviet dined with me. We went to
search for brick-earth, in order to a great undertaking.
4th April, 1667. The cold so intense, that there was hardly a leaf
on a tree.
18th April, 1667. I went to make court to the Duke and Duchess
of Newcastle, at their house in Clerkenwell, being newly come out of
the north. They received me with great kindness, and I was much
pleased with the extraordinary fanciful habit, garb, and discourse of
the Duchess.
22d April, 1667. Saw the sumptuous supper in the banqueting-
house at Whitehall, on the eve of St. George's day, where were all
the companions of the Order of the Garter.
23d April, 1667. In the morning, his Majesty went to chapel with
the Knights of the Garter, all in their habits and robes, ushered by
the heralds; after the first service, they went in procession, the
youngest first, the Sovereign last, with the Prelate of the Order and
Dean, who had about his neck the book of the Statutes of the Order;
and then the Chancellor of the Order (old Sir Henry de Vic), who
wore the purse about his neck; then the Heralds and Garter King-at-
Arms, Clarencieux, Black Rod. But before the Prelate and Dean of
Windsor went the gentlemen of the chapel and choristers, singing as
they marched; behind them two doctors of music in damask robes;
this procession was about the courts at Whitehall. Then, returning to
their stalls and seats in the chapel, placed under each knight's coat-
armor and titles, the second service began. Then, the King offered at
the altar, an anthem was sung; then, the rest of the Knights offered,
and lastly proceeded to the banqueting-house to a great feast. The
King sat on an elevated throne at the upper end at a table alone; the
Knights at a table on the right hand, reaching all the length of the
room; over against them a cupboard of rich gilded plate; at the
lower end, the music; on the balusters above, wind music, trumpets,
and kettle-drums. The King was served by the lords and pensioners
who brought up the dishes. About the middle of the dinner, the
Knights drank the King's health, then the King, theirs, when the
trumpets and music played and sounded, the guns going off at the
Tower. At the Banquet, came in the Queen, and stood by the King's
left hand, but did not sit. Then was the banqueting-stuff flung about
the room profusely. In truth, the crowd was so great, that though I
stayed all the supper the day before, I now stayed no longer than
this sport began, for fear of disorder. The cheer was extraordinary,
each Knight having forty dishes to his mess, piled up five or six high;
the room hung with the richest tapestry.
25th April, 1667. Visited again the Duke of Newcastle, with whom
I had been acquainted long before in France, where the Duchess
had obligation to my wife's mother for her marriage there; she was
sister to Lord Lucas, and maid of honor then to the Queen-Mother;
married in our chapel at Paris. My wife being with me, the Duke and
Duchess both would needs bring her to the very Court.
26th April, 1667. My Lord Chancellor showed me all his newly
finished and furnished palace and library; then, we went to take the
air in Hyde-Park.
27th April, 1667. I had a great deal of discourse with his Majesty
at dinner. In the afternoon, I went again with my wife to the
Duchess of Newcastle, who received her in a kind of transport,
suitable to her extravagant humor and dress, which was very
singular.
8th May, 1667. Made up accounts with our Receiver, which
amounted to £33,936 1s. 4d. Dined at Lord Cornbury's, with Don
Francisco de Melos, Portugal Ambassador, and kindred to the Queen:
Of the party were Mr. Henry Jermyn and Sir Henry Capel. Afterward I
went to Arundel House, to salute Mr. Howard's sons, newly returned
out of France.
11th May, 1667. To London; dined with the Duke
LONDON
of Newcastle, and sat discoursing with her Grace in
her bedchamber after dinner, till my Lord Marquis of Dorchester, with
other company came in, when I went away.
30th May, 1667. To London, to wait on the Duchess of Newcastle
(who was a mighty pretender to learning, poetry, and philosophy,
and had in both published divers books) to the Royal Society,
whither she came in great pomp, and being received by our Lord
President at the door of our meeting-room, the mace, etc., carried
before him, had several experiments shown to her. I conducted her
Grace to her coach, and returned home.
1st June, 1667. I went to Greenwich, where his Majesty was
trying divers grenadoes shot out of cannon at the Castlehill, from the
house in the park; they broke not till they hit the mark, the forged
ones broke not at all, but the cast ones very well. The inventor was
a German there present. At the same time, a ring was shown to the
King, pretended to be a projection of mercury, and malleable, and
said by the gentlemen to be fixed by the juice of a plant.
8th June, 1667. To London, alarmed by the Dutch, who were
fallen on our fleet at Chatham, by a most audacious enterprise,
entering the very river with part of their fleet, doing us not only
disgrace, but incredible mischief in burning several of our best men-
of-war lying at anchor and moored there, and all this through our
unaccountable negligence in not setting out our fleet in due time.
This alarm caused me, fearing the enemy might venture up the
Thames even to London (which they might have done with ease,
and fired all the vessels in the river, too), to send away my best
goods, plate, etc., from my house to another place. The alarm was
so great that it put both country and city into fear, panic, and
consternation, such as I hope I shall never see more; everybody was
flying, none knew why or whither. Now, there were land forces
dispatched with the Duke of Albemarle, Lord Middleton, Prince
Rupert, and the Duke, to hinder the Dutch coming to Chatham,
fortifying Upnor Castle, and laying chains and bombs; but the
resolute enemy broke through all, and set fire on our ships, and
retreated in spite, stopping up the Thames, the rest of the fleet lying
before the mouth of it.
14th June, 1667. I went to see the work at Woolwich, a battery to
prevent them coming up to London, which Prince Rupert
commanded, and sunk some ships in the river.
17th June, 1667. This night, about two o'clock, some chips and
combustible matter prepared for some fire-ships, taking flame in
Deptford-yard, made such a blaze, and caused such an uproar in the
Tower (it being given out that the Dutch fleet was come up, and had
landed their men and fired the Tower), as had liked to have done
more mischief before people would be persuaded to the contrary
and believe the accident. Everybody went to their arms. These were
sad and troublesome times.
24th June, 1667. The Dutch fleet still continuing to stop up the
river, so as nothing could stir out or come in, I was before the
Council, and commanded by his Majesty to go with some others and
search about the environs of the city, now exceedingly distressed for
want of fuel, whether there could be any peat, or turf, found fit for
use. The next day, I went and discovered enough, and made my
report that there might be found a great deal; but nothing further
was done in it.
28th June, 1667. I went to Chatham, and thence
CHATHAM
to view not only what mischief the Dutch had
done; but how triumphantly their whole fleet lay within the very
mouth of the Thames, all from the North Foreland, Margate, even to
the buoy of the Nore—a dreadful spectacle as ever Englishmen saw,
and a dishonor never to be wiped off! Those who advised his
Majesty to prepare no fleet this spring deserved—I know what—
but11—
Here in the river off Chatham, just before the town, lay the
carcase of the "London" (now the third time burnt), the "Royal Oak,"
the "James," etc., yet smoking; and now, when the mischief was
done, we were making trifling forts on the brink of the river. Here
were yet forces, both of horse and foot, with General Middleton
continually expecting the motions of the enemy's fleet. I had much
discourse with him, who was an experienced commander, I told him
I wondered the King did not fortify Sheerness12 and the Ferry; both
abandoned.
2d July, 1667. Called upon my Lord Arlington, as from his Majesty,
about the new fuel. The occasion why I was mentioned, was from
what I said in my Sylva three years before, about a sort of fuel for a
need, which obstructed a patent of Lord Carlingford, who had been
seeking for it himself; he was endeavoring to bring me into the
project, and proffered me a share. I met my Lord; and, on the 9th,
by an order of Council, went to my Lord Mayor, to be assisting. In
the meantime they had made an experiment of my receipt of
houllies, which I mention in my book to be made at Maestricht, with
a mixture of charcoal dust and loam, and which was tried with
success at Gresham College (then being the exchange for the
meeting of the merchants since the fire) for everybody to see. This
done, I went to the Treasury for £12,000 for the sick and wounded
yet on my hands.
Next day, we met again about the fuel at Sir J. Armourer's in the
Mews.
8th July, 1667. My Lord Brereton and others dined at my house,
where I showed them proof of my new fuel, which was very
glowing, and without smoke or ill smell.
10th July, 1667. I went to see Sir Samuel Morland's inventions
and machines, arithmetical wheels, quench-fires, and new harp.
17th July, 1667. The master of the mint and his lady, Mr.
Williamson, Sir Nicholas Armourer, Sir Edward Bowyer, Sir Anthony
Auger, and other friends dined with me.
19th July, 1667. I went to Gravesend; the Dutch fleet still at
anchor before the river, where I saw five of his Majesty's men-at-war
encounter above twenty of the Dutch, in the bottom of the Hope,
chasing them with many broadsides given and returned toward the
buoy of the Nore, where the body of their fleet lay, which lasted till
about midnight. One of their ships was fired, supposed by
themselves, she being run on ground. Having seen this bold action,
and their braving us so far up the river, I went home the next day,
not without indignation at our negligence, and the nation's reproach.
It is well known who of the Commissioners of the Treasury gave
advice that the charge of setting forth a fleet this year might be
spared, Sir W. C. (William Coventry) by name.
1st August, 1667. I received the sad news of Abraham Cowley's
death, that incomparable poet and virtuous man, my very dear
friend, and was greatly deplored.
3d August, 1667. Went to Mr. Cowley's funeral,
LONDON
whose corpse lay at Wallingford House, and was
thence conveyed to Westminster Abbey in a hearse with six horses
and all funeral decency, near a hundred coaches of noblemen and
persons of quality following; among these, all the wits of the town,
divers bishops and clergymen. He was interred next Geoffry Chaucer,
and near Spenser. A goodly monument is since erected to his
memory.
Now did his Majesty again dine in the presence, in ancient state,
with music and all the court ceremonies, which had been interrupted
since the late war.
8th August, 1667. Visited Mr. Oldenburg, a close prisoner in the
Tower, being suspected of writing intelligence. I had an order from
Lord Arlington, Secretary of State, which caused me to be admitted.
This gentleman was secretary to our Society, and I am confident will
prove an innocent person.
15th August, 1667. Finished my account, amounting to £25,000.
17th August, 1667. To the funeral of Mr. Farringdon, a relation of
my wife's.
There was now a very gallant horse to be baited to death with
dogs; but he fought them all, so as the fiercest of them could not
fasten on him, till the men run him through with their swords. This
wicked and barbarous sport deserved to have been punished in the
cruel contrivers to get money, under pretense that the horse had
killed a man, which was false. I would not be persuaded to be a
spectator.
21st August, 1667. Saw the famous Italian puppet-play, for it was
no other.
24th August, 1667. I was appointed, with the rest of my brother
commissioners, to put in execution an order of Council for freeing
the prisoners at war in my custody at Leeds Castle, and taking off
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookname.com

You might also like