This is a static archive of the Python wiki, which was retired in February 2026 due to lack of usage and the resources necessary to serve it — predominately to bots, crawlers, and LLM companies.
Pages are preserved as they were at the time of archival. For current information, please visit python.org.
If a change to this archive is absolutely needed, requests can be made via the infrastructure@python.org mailing list.

Widgets in a layout

This page contains the source code shown in the Creating widgets and a layout video on YouTube.

This shows basic widget creation, though most programs will tend to create and instantiate subclasses of QWidget or other widget classes for their main user interfaces.

The code below uses version 4 of PyQt. For version 5, see for instance the PyQt Widgets example on GitHub.

   1 # [Create a window]
   2 
   3 import sys
   4 from PyQt4.QtGui import *
   5 
   6 app = QApplication(sys.argv) #ignore()
   7 window = QWidget()
   8 window.setWindowTitle("Hello World")
   9 window.show()
  10 
  11 # [Add widgets to the widget]
  12 
  13 # Create some widgets (these won't appear immediately):
  14 nameLabel = QLabel("Name:")
  15 nameEdit = QLineEdit()
  16 addressLabel = QLabel("Address:")
  17 addressEdit = QTextEdit()
  18 
  19 # Put the widgets in a layout (now they start to appear):
  20 layout = QGridLayout(window)
  21 layout.addWidget(nameLabel, 0, 0)
  22 layout.addWidget(nameEdit, 0, 1)
  23 layout.addWidget(addressLabel, 1, 0)
  24 layout.addWidget(addressEdit, 1, 1)
  25 layout.setRowStretch(2, 1)
  26 
  27 # [Resizing the window]
  28 
  29 # Let's resize the window:
  30 window.resize(480, 160)
  31 
  32 # The widgets are managed by the layout...
  33 window.resize(320, 180)
  34 
  35 # [Run the application]
  36 
  37 # Start the event loop...
  38 sys.exit(app.exec_())

2026-02-14 16:12