32
Graphical User Interface Programming (Part 1) Copyright © 2021 by Robert M. Dondero, Ph.D. Princeton University 1

Programming (Part 1) Graphical User Interface

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Programming (Part 1) Graphical User Interface

Graphical User Interface Programming (Part 1)

Copyright © 2021 byRobert M. Dondero, Ph.D.

Princeton University

1

Page 2: Programming (Part 1) Graphical User Interface

Objectives

• We will cover:– Graphical user interface (GUI) programming

2

Page 3: Programming (Part 1) Graphical User Interface

Objectives

More specifically…

We will cover:“High-level” GUI programming

Programming with buttons, menus, text fields, …

We will not cover:“Low-level” GUI programming

Drawing lines, circles, rectangles, etc.See COS 126

Objectives

• More specifically…• We will cover:

– “High-level” GUI programming• We will not cover:

– “Low-level” GUI programming

3

Page 4: Programming (Part 1) Graphical User Interface

Motivation

Question: Why study GUI programming?

Answer 1: Mainstream “advanced programming”This course is about “advanced programming techniquesGUI programming clearly falls under that heading

GUI programming is used to create desktop/laptop appsGranted, most COS 333 projects involve creating web apps, not desktop appsNevertheless, there is more to COS 333 than the projectsThere is more to advanced programming than composing web apps

Answer 2: Desktop apps are importantDesktop apps are not going awayDesktop editors, ftp clients, terminal apps -- not to mention browsers -- are here to staySomebody must compose them!

Answer 3: Illustrates event-driven programmingGood lead into programming browsers

Answer 4: Illustrates concurrent programmingAs we’ll see later in the course, GUI programming illustrates multi-threaded

Motivation

• Question: Why study GUI programming?• Answers:

– Mainstream “advanced programming”– Important– Illustrates event-driven programming– Illustrates concurrent programming

4

Page 5: Programming (Part 1) Graphical User Interface

programming very well

Page 6: Programming (Part 1) Graphical User Interface

Agenda

• Introduction to PyQt5• GUI programming in COS 333• PyQt5 widgets• PyQt5 layout managers

5

Page 7: Programming (Part 1) Graphical User Interface

Intro to PyQt5

• Qt– Haavard Nord and Eirik Chambe-Eng– Trollteck, Nokia, The Qt Company

6

Introduction to PyQt5

Qt“Cute”Haavard Nord and Eirik Chambe-EngTrollteck, Nokia, The Qt CompanyVery successful cross-platform GUI libraryWritten in C++440 classes; > 6000 functions/methods

Page 8: Programming (Part 1) Graphical User Interface

Intro to PyQt5

• PyQt5– Phil Thompson– Riverbank Computing– A “Python binding” of Qt

7

Page 9: Programming (Part 1) Graphical User Interface

Intro to PyQt5

• Question: Why study PyQt5?– Instead of some other GUI library?

• Answers:– Well designed, stable, robust– Written in Python

8

Page 10: Programming (Part 1) Graphical User Interface

Intro to PyQt5

• PyQt5 ≈ PySide2 ≈ Qt for Python• In COS 333:

– Write PyQt5 code– Read PyQt5, PySide2, or Qt for Python

documentation

9

Introduction to PyQt5

There are three popular Python bindings of Qt:PyQt5, PySide2, and Qt for Python

They’re very similarIn COS 333 you should write PyQt5 codeBut to learn more, it would be OK to read the documentation for any of the 3 productsA slide near the end of the GUI Pgmming (Part 2) lecture provides references to the documentation

Page 11: Programming (Part 1) Graphical User Interface

Intro to PyQt5

• See hellopyqt.py– The job:

• Display “Hello, world” in a window

10

Page 12: Programming (Part 1) Graphical User Interface

Intro to PyQt5

11

QM

ainW

indo

w

QFr

ame

(cen

tralW

idge

t)

QLabel

See hellopyqt.py (cont.)

Introduction to PyQt5

[see slide]

Code notes:Instantiate QApplication object

Must do so before instantiating any widgetsCould pass command-line arguments; beyond our scope

Instantiate QLabel objectInstantiate QGridLayout object

Add QLabel object to itMore on layout managers soon

Instantiate QFrame objectAdd QGridLayout object to it

Instantiate QMainWindow objectAdd QFrame object to itSet its initial size

Make the QMainWindow object visibleapp.exec_() method call starts event handling loop

Whatever value it returns should be the exit status of the programSuggestion: accept as a pattern

Page 13: Programming (Part 1) Graphical User Interface

Agenda

• Introduction to PyQt5• GUI programming in COS 333• PyQt5 widgets• PyQt5 layout managers

12

Page 14: Programming (Part 1) Graphical User Interface

GUI Pgmming in COS 333Option 1: Run on your computer

Local Computer

App

13

GUI Programming in COS 333

Option 1: Run on your computer

[see slide]

Must install Python 3.8Must install PyQt5See A COS 333 Programming Environment documentCaveat:

Assignments must work on courselab too

Page 15: Programming (Part 1) Graphical User Interface

GUI Pgmming in COS 333Option 2: Run on courselab

Local Computer

X WindowServer

courselab03/04

App(User 1)

X Windows

14

GUI Programming in COS 333

Option 2: Run on courselab

[see slide]

Must install X Window System Server on local computerSee instructions in the A COS 333 Programming Environment document from 1st lecture

Your application, while running on courselab, displays its window(s) on your local computer

Page 16: Programming (Part 1) Graphical User Interface

Agenda

• Introduction to PyQt5• GUI programming in COS 333• PyQt5 widgets• PyQt5 layout managers

15

Page 17: Programming (Part 1) Graphical User Interface

PyQt5 Widgets

• See widgettest.py– The job:

• Display the most fundamental widgets

16

Page 18: Programming (Part 1) Graphical User Interface

PyQt5 Widgets

18

QPushButton QLabel

QLineEdit QTextEdit

QSlider QCheckBox

QRadioButton(3 of them) QListWidget

See widgettest.py (cont.)

PyQt5 Widgets

[see slide]

4 rows, 2 columns, each cell contains a widget

QPushButtonUser communicates to pgm that it should perform some action

QLabelPgm communicates some text to user

QLineEditPgm communicates one line of text to userUser can edit the text, and thereby communicate one line of text to pgm

QTextEditPgm communicates multiple lines of text to userUser can edit the text, and thereby communicate multiple lines of text to the pgmText is wrapped as window size changesWindow size too small vertically => QTextEdit displays vertical scroll bar

QSliderUser communicates a number to the pgm

QCheckBoxUser communicates a Boolean value to pgm

QRadioButton (set of 3)

Page 19: Programming (Part 1) Graphical User Interface

User communicates a 1 out of 3 choice to pgmQListWidget

Pgm communicates a list of items to userUser can select items, and communicate selection to pgmWindow too small vertically => vertical scroll barWindow too small horizontally => horizontal scroll bar

Page 20: Programming (Part 1) Graphical User Interface

PyQt5 Widgets

QMenuBarQMenuSee widgettest.py (cont.)

18

PyQt5 Widgets

[see slide]

Code notes:PyQt5 supports menusPattern:

Ask the window for a menuBarTo the menuBar, add MenusTo each Menu, add Menus or actions

You should try running the program!

Page 21: Programming (Part 1) Graphical User Interface

PyQt5 WidgetsConceptual Widget Relevant PyQt5 ClassesContainer QMainWindow

QFrame

Button QPushButton

Label QLabel

Text field QLineEdit

Text area QTextEdit

Scrolling text area QTextEdit

Slider QSlider

Check box QCheckBox

Radio button QRadioButton

19

PyQt5 Widgets

Any GUI library implements “conceptual widgets” via specific classes

In PyQt5…

[see slide]

Page 22: Programming (Part 1) Graphical User Interface

PyQt5 WidgetsConceptual Widget Relevant PyQt ClassesList box QListWidget

Scrolling list box QListWidget

Menu QMenuBarQMenu

20

PyQt5 Widgets

Any GUI library implements “conceptual widgets” via specific classes

In PyQt5…

[see slide]

Page 23: Programming (Part 1) Graphical User Interface

QObject & QPaintDeviceQWidget

QMainWindowQAbstractButton

QCheckBoxQPushButtonQRadioButton

QAbstractSliderQSlider

QFrameQAbstractScrollArea

QTextEditQAbstractItemView

QListViewQListWidget

QLabelQLineEditQMenuQMenuBar

PyQt5 Widgets

21

Some PyQt5 widget classes…

And many many more

PyQt5 Widgets

PyQt5 provides many widget classes

Here are some of themIndentation indicates inheritance

[see slide]

Page 24: Programming (Part 1) Graphical User Interface

PyQt5 Layout Managers

Q: How to arrange components/widgets within their containers?

Q: How to specify the resizing behavior of components/widgets?

A: Layout managers

Agenda

• Introduction to PyQt5• GUI programming in COS 333• PyQt5 widgets• PyQt5 layout managers

22

Page 25: Programming (Part 1) Graphical User Interface

PyQt5 Layout Managers

• See layoutvbox.py– The job:

• Display three QLabel objects arranged vertically

23

PyQt5 Layout Managers

[see slide]

Code notes:Setting the background color of a widgetQVBoxLayout classaddWidget() method

Page 26: Programming (Part 1) Graphical User Interface

PyQt5 Layout Managers

• See layouthbox.py– The job:

• Display three QLabel objects arranged horizontally

24

PyQt5 Layout Managers

[see slide]

Code notes:QHBoxLayout class

Page 27: Programming (Part 1) Graphical User Interface

PyQt5 Layout Managers

• See layoutgrid.py– The job:

• Display six QLabel objects arranged in a 2x3 grid

25

PyQt5 Layout Managers

[see slide]

Code notes:QGridLayout classaddWidget(row,col) methodsetSpacing(i) method

Page 28: Programming (Part 1) Graphical User Interface

PyQt5 Layout Managers

• See layoutgridstretch.py– The job:

26

PyQt5 Layout Managers

[see slide]

The job (cont.):Five QLabel objects: north, south, west, east, centerNorth and south: expand/contact horizontallyWest and east: expand/contract verticallyCenter: expands/contracts horizontally and vertically

Code notes:Can specify row/column spans

addWidget(row,col,rowSpan,colSpan) methodCan specify row/column stretches

setRowStretch(row,stretch) methodsetColumnStretch(col,stretch) method

Common layout

Page 29: Programming (Part 1) Graphical User Interface

layout.setRowStretch(0, 1)layout.setRowStretch(1, 1)layout.setRowStretch(2, 1)

PyQt5 Layout Managers

27

All 3 rows expand/contract up & down at the same rate

layout.setRowStretch(0, 1)layout.setRowStretch(1, 2)layout.setRowStretch(2, 1)

Row 1 expands/contracts up & down at twice the rate of rows 0 and 2

layout.setRowStretch(0, 0)layout.setRowStretch(1, 1)layout.setRowStretch(2, 0)

Rows 0 and 2 don’t expand/contract up & down; row 1 does

See layoutgridstretch.py (cont.)

Page 30: Programming (Part 1) Graphical User Interface

layout.setColStretch(0, 1)layout.setColStretch(1, 1)layout.setColStretch(2, 1)

PyQt5 Layout Managers

28

All 3 cols expand/contract side-to-side at the same rate

layout.setColStretch(0, 1)layout.setColStretch(1, 2)layout.setColStretch(2, 1)

Col 1 expands/contracts side-to-side at twice the rate of cols 0 and 2

layout.setColStretch(0, 0)layout.setColStretch(1, 1)layout.setColStretch(2, 0)

Cols 0 and 2 don’t expand side-to-side; col 1 does

See layoutgridstretch.py (cont.)

Page 31: Programming (Part 1) Graphical User Interface

QObject & QLayoutItemQLayout

QBoxLayoutQFormLayoutQGridLayoutQHBoxLayoutQStackedLayoutQVBoxLayout

PyQt5 Layout Managers

29

PyQt5 Layout Managers

There are several additional layout managers

[see slide]

Indentation indicates inheritance

Page 32: Programming (Part 1) Graphical User Interface

Summary

• We have covered:– Introduction to PyQt5– GUI programming in COS 333– PyQt5 widgets– PyQt5 layout managers

30