1 programming for engineers in python autumn 2011-12 lecture 11: gui

41
1 Programming for Engineers in Python Autumn 2011- 12 Lecture 11: GUI

Upload: martin-barrie-miles

Post on 05-Jan-2016

234 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

1

Programming for Engineers in

Python

Autumn 2011-12

Lecture 11: GUI

Page 2: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

2

Lecture 10: Highlights• What is an image?

• Representation, resolution, quantization, gray-level images• http://www.youtube.com/watch?v=bgtHKR0aQpQ&feature=related

• http://en.wikipedia.org/wiki/Lenna

• Image processing using PIL• Read, write, manipulate pixels

• Edges detection

• Denoising

• Image segmentation: thresholding

• Real world computer vision application: face detection

• HW – the whole loop + GUI (today!)

Page 3: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

3

Plan

• GUI (Based on slides from the course Software1, CS, TAU)

• GUI in Python (Based on Chapter 19 from the book “Think Python”)

• Sorting – completions (merge sort, bucket sort)

• Off topic: incremental development (?)

Page 4: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

4

Page 5: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

5

גראפי ממשק פיתוח שלבי

מהנדס ממשקי אנוש

מפתח ממשק כך שיהיה •מובן, יעיל, נעים

מודדים את איכות •הממשק על קב'

משתמשים ומתקנים בהתאם

מחליט איך הממשק •יתנהג

מעצב גראפי

מחליט על סוגי •האלמנטים שיופיעו על

המסך, ועל סידורם

מחליט איך הממשק •ייראה/יישמע מהנדס תכנה

למתכנתים אין תפקיד כ"כ גדול מימושבתהליך

Page 6: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

6

אנוש ממשקי הנדסתשל; אינטואיטיביות• המוקדמות לציפיות בהתאם להתנהג צריך הממשק

,) , - ( ; פריטים/ של המראה למשל הדבק גזור אוטומטיות פעולות ת המשתמש , ,) , של) התוכנית של הכללית וההתנהגות המראה למשל צלמיות

הפלטפורמה

• , המחשב/ לא בשליטה ת , המשתמש מה; ידיעה באשף אחורה חזרהכרגע עושה היא ומה התוכנית של הנוכחי המצב

• , המחשב של לא המשתמש של , יעילות הן; משכורות זולה היא חומרהיקרות, יותר עוד הן ואכזבות יקרות

התוכנה • וללימוד השימוש לתכיפות באופן; התאמה בה משתמשים האם ;) ( ) יומיומי ) משתמש גם דואל יומיומי או צוואות לכתיבת אשף פעמי חד

ניסיון חסר מתחיל פעם היה בתוכנה

Page 7: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

7

מ שקיםמעיצוב

קונסיסטנטיות•; קונטרסט• ויזואלי עומס הדגשה דרוש שבאמת מה להדגשת

הקונטרסט את מפחית(ארגון• בסריג ) שימוש תוך כלל בדרך המסך של ברורברורים • וסדר משמאל ) כיוון למטה מלמעלה המידע לסריקת

) לשמאל, ימין או לימיןמ • של הגרפי ; מהעיצוב מוחלט אינו כלל בדרך תוכנית של שק

/ בחירת על להשפיע עשויים הפלטפורמה או ו המשתמש ;) , תפריטים ) כפתורים גראפיים פריטים של הסגנון ועל גופנים

לסביבה עצמו את להתאים צריך העיצוב

Page 8: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

8

למימוש ועכשיו

Page 9: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

9

גרפית תוכנה של הצירים שלושת

המסך אלמנטים• על שונים מסוגיםהאלמנטים הארגון• של הדו־מימדישל ההתנהגות• לפעולות בתגובה האלמנטים של הדינמית

) , , :" גרירה/ )" הקלקה הקלדה ארועים ת המשתמש

Page 10: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

10

כמיכלים חלונות

הוא • דבר (widgetכל , , , משטח ) תפריט כפתור חלון•) לרבים ) אחד הכלה יחס מקיימים החלונות

משטח עבודה)אב עליון(

שורת משימות

אפליקציה)צייר(

Page 11: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

11

) המשך ) החלונות מודל

Page 12: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

12

Swampy and Tkinter• We will use Swampy (http://greenteapress.com/thinkpython/swampy/)

• Download )http://greenteapress.com/thinkpython/swampy/install.html(

• Add Swampy to search path

• Instructions on how to modify Python’s search path http://docs.python.org/install/index.html#inst-search-path

• The convention is to download packages to …/site-packages/

• Create path configuration file (.pth) in …/site-packages/

• It is easy to create it on your own (if not automatically created)

Page 13: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

13

Path Configuration File

Page 14: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

14

Create a GUI

• mainloop runs the event loop, which waits for the user to do something and responses accordingly

• It is an infinite loop – runs until the user closes the window or causes the program to quite

• This Gui doesn’t do much because is does not have any widgets

Page 15: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

15

Widgets• Widgets are elements that make up a GUI, they include:

• Button: A widget, containing text or an image, that performs an action when pressed

• Canvas: A region that can display lines, rectangles, circles and other shapes

• Entry: A region where users can type text

• Scrollbar: A widget that controls the visible part of another widget

• Frame: A container, often invisible, that contains other widgets

• The empty gray square you see when creating a Gui is a Frame

• When creating new widgets, they are added to this Frame

Page 16: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

• bu – a method that returns a Button object

• bu takes up to 32 parameters that control the appearance and function of the button (e.g., text)

• When you add a widget to the Frame, it gets “shrink-wrapped;” • Add a label widget

• By default, Tkinter stacks the widgets top-to-bottom and centers them. We’ll see how to override that behavior soon 16

Add Button and Label

Page 17: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

• Pressing the button…we haven’t told it what to do!

• command – controls the behavior of a button

• The value of command is a function that gets executed when the button is pressed

• Example:

17

Callbacks

Global variable

Page 18: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

18

Callbacks

Page 19: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

• Callback - a function object, after we call bu to create the button, the flow of execution “calls back” when the user presses the button.

• This kind of flow is characteristic of event-driven programming. User actions, like button presses and key strokes, are called events. In event-driven programming, the flow of execution is determined by user actions rather than by the programmer.

• A major challenge of event-driven programming is to construct a set of widgets and callbacks that work correctly for any sequence of user actions.

19

Event-Driven Programming

Page 20: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

20

Canvas Widgets

Page 21: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

21

Examplebutton + canvas

• Create a Canvas and a Button

• When the use presses the button a circle is drawn on the canvas

Page 22: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

22

Examplebutton + canvas

Page 23: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

23

Coordinates Sequences

Page 24: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

24

More Widgets

In the tirgul )19.5 in the book(

Page 25: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

25

Packing Widgets• Until now we have been stacking widgets in a single column

• Most GUIs’ layouts are more complicated

• Example: Turtle World (19.6 in the book) (http://www.greenteapress.com/thinkpython/code/SimpleTurtleWorld.py ):

Page 26: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

26

Another Example

• PyNote – a Python Notepad, in the tirgul

Page 27: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

27

Display an Image in a GUI

Page 28: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

28

Display an Image in a GUI

Page 29: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

29

Code – Bubble Sort

n iterations

i iterations

constant

(n-1 + n-2 + n-3 + …. + 1 * )const ~ ½ * n2

Page 30: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

30

7 2 8 5 4

2 7 8 5 4

2 7 8 5 4

2 7 5 8 4

2 7 5 4 8

2 7 5 4 8

2 5 7 4 8

2 5 4 7 8

2 7 5 4 8

2 5 4 7 8

2 4 5 7 8

2 5 4 7 8

2 4 5 7 8

2 4 5 7 8

(done)

Bubble Sort Example

Page 31: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

We showed that it is possible to sort in O(n2)

Can we do it faster?

31

Yes!

Page 32: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

32

Comparing Bubble Sort with sorted

Page 33: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

The Idea Behind Merge Sort

33

• Sorting a short array is much faster than a long array

• Two sorted arrays can be merged to a combined sorted array quite fast (O(n))

Page 34: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

Merging Two Sorted Arrays

34

1 2 5 7 9 3 4 6 8 10

p q

u

1

u

p p p p q q q q

u u u u u u u u

2 3 4 5 6 7 8 9 10

p

Page 35: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

Merge Sort Algorithm

35

• If the array is of size < 2 it is already sorted

• Partition the array to two halves

• Sort recursively each half

• Merge the sorted sub arrays to a single sorted array

Page 36: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

Merge Sort (Partial) Code

36

Page 37: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

37

Merge Sort Example

Page 38: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

Merge Sorttime complexity analysis

38

• If the array is of size < 2 it is already sorted

• Partition the array to two halves

• Sort recursively each half

• Merge the sorted sub arrays to a single sorted array

n + 2 * (n/2) + 22 * n/22 + 23 * n/23 + … + 2log(n) * n/2log(n)=

n + n + … + n = (n+1) * log(n)

log)n( +1

Page 39: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

Graphical Comparison

39

Page 40: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

40

Bucket SortO(n)אלגוריתם בזמן לינארי : •

אבל... מוגבל למספרים שלמים, חסומים •בטווח.

Page 41: 1 Programming for Engineers in Python Autumn 2011-12 Lecture 11: GUI

41

Bucket Sort