Programming for Engineers in Python Recitation 11 Plan

  • Slides: 32
Download presentation
Programming for Engineers in Python Recitation 11

Programming for Engineers in Python Recitation 11

Plan �GUI �Swampy �Text widgets �Py. Pad 2

Plan �GUI �Swampy �Text widgets �Py. Pad 2

Install Swampy � Download Swampy: � http: //greenteapress. com/thinkpython/swampy/swamp y-2. 0. python 2. zip

Install Swampy � Download Swampy: � http: //greenteapress. com/thinkpython/swampy/swamp y-2. 0. python 2. zip � Unzip to C: Python 27Libsite-packages � Now you should have a directory C: Python 27Libsitepackagesswampy-2. 0 � Download this file http: //db. tt/g. Qq. Pv. Rok (it might open as a text file, right click and choose ‘Save as…’) and also put it in C: Python 27Libsite-packages, making sure its name is swampy. pth � Now you should have a file C: Python 27Libsitepackagesswampy. pth 3

Check Swampy was installed �Open the shell (IDLE) �Run the following commands to check

Check Swampy was installed �Open the shell (IDLE) �Run the following commands to check that Swampy is working: >>> import Tkinter >>> from Gui import * >>> g=Gui() >>> g. mainloop() �If you see a window like this -> then the installation worked 4

GUI - reminder �Graphical User Interface �The part of the program that communicates with

GUI - reminder �Graphical User Interface �The part of the program that communicates with the human user �Computer → Human: graphics (sometimes audio & even vibrations) �Human → Computer: keyboard & mouse (sometimes even touchscreen & microphone) �In many applications this is the most important part of the program 5

History of GUI 6 http: //www. youtube. com/watch? v=TZGGUrom 1 Mg

History of GUI 6 http: //www. youtube. com/watch? v=TZGGUrom 1 Mg

Widgets �In class: �Button �Label �Canvas 7

Widgets �In class: �Button �Label �Canvas 7

More widgets! �Textual input widgets: �Entry – single line entry_example. py: https: //gist. github.

More widgets! �Textual input widgets: �Entry – single line entry_example. py: https: //gist. github. com/1523427 �Text – multiple lines text_example. py: https: //gist. github. com/1523471 8 Think Python 19. 5

More complex example �Change the color of a circle based on the color name

More complex example �Change the color of a circle based on the color name given by the user circle_demo. py: thinkpython. com/code/circle_demo. py 9 Think Python 19. 5 pg. 185

Py. Note – a Python Notepad 10

Py. Note – a Python Notepad 10

Packing >>> g = Gui() >>> g. title('Py. Note 0. 1') Creates the GUI

Packing >>> g = Gui() >>> g. title('Py. Note 0. 1') Creates the GUI & title 11

Packing >>> g. col(weights=[1, 0] ) Creates a column that holds widgets Weights –

Packing >>> g. col(weights=[1, 0] ) Creates a column that holds widgets Weights – later… 12

Packing >>> g. st(…) st: Scrollable text widget 13

Packing >>> g. st(…) st: Scrollable text widget 13

Packing >>> g. row(weights=[1, 0, 0, 0]) Creates a row that holds widgets �The

Packing >>> g. row(weights=[1, 0, 0, 0]) Creates a row that holds widgets �The weights option determines which widget is resized and by how much when the window is resized 14

Resizing �With weights=[1, 0, 0, 0] �Without specifying weights 15

Resizing �With weights=[1, 0, 0, 0] �Without specifying weights 15

Packing filename = g. en(text='filename. txt', width=16) g. bu(text='. . . ', command=browse) g.

Packing filename = g. en(text='filename. txt', width=16) g. bu(text='. . . ', command=browse) g. bu(text='Save', command=save) g. bu(text='Load', command=load) g. bu(text='New', command=new) g. bu(text='Quit', command=quit_) g. bu: button widget g. en: entry widget (one line of text) 16

Check if a file exists �os. path. exists(filename) �Returns True if file exists, False

Check if a file exists �os. path. exists(filename) �Returns True if file exists, False if it does not 17

Command g. bu(text='Save', command=save) 18 def save(): '''Saves the current note in a file'''

Command g. bu(text='Save', command=save) 18 def save(): '''Saves the current note in a file''' if exists(filename. get()): # exists is os. path. exists if not tk. Message. Box. askyesno ('Save', 'File already exists, do you want to overwrite? '): return f = open(filename. get(), 'w') f. write(textbox. text. get(0. 0, END)) f. close() tk. Message. Box. showinfo('Save', 'File saved: ‘ +filename. get())

Event driven programming �Nothing happens until the user initiates an event �When the user

Event driven programming �Nothing happens until the user initiates an event �When the user clicks the button the command is called �Now the GUI is “frozen” until the command returns �When it returns, the GUI is released 19 >>> def freeze(): time. sleep(5) >>> g=Gui() >>> g. bu(text=‘Freeze!’, command=freeze) >>> g. mainloop()

http: //www. tutorialspoint. com/python/tk_messagebox. htm tk. Message. Box �Creates a message box that asks

http: //www. tutorialspoint. com/python/tk_messagebox. htm tk. Message. Box �Creates a message box that asks the user a question or gives him information �Usually with a button or two �Examples: tk. Message. Box. showinfo('Load', 'File does not exist: '+filename. get()) tk. Message. Box. askyesno('Save', 'File already exists, do you want to overwrite? ') 20 tk. Message. Box. askyesno('Quit? ', 'Do you want to quit? ')

tk. Message. Box. askyesno('Save', 'File already exists, do you want to overwrite? ') 21

tk. Message. Box. askyesno('Save', 'File already exists, do you want to overwrite? ') 21

tk. Message. Box. askyesno('Quit? ', 'Do you want to quit? ') 22

tk. Message. Box. askyesno('Quit? ', 'Do you want to quit? ') 22

tk. Message. Box. showinfo('Load', 'File does not exist: ‘ + filename. get()) 23

tk. Message. Box. showinfo('Load', 'File does not exist: ‘ + filename. get()) 23

Load command def load(): '''Opens a file to the text widget''' if not exists(filename.

Load command def load(): '''Opens a file to the text widget''' if not exists(filename. get()): tk. Message. Box. showinfo('Load', 'File does not exist: ' 24 +filename. get()) return new() # delete previous note from text widget f = open(filename. get()) t = f. read() textbox. text. insert(0. 0, t) f. close()

File dialog box �We can get the file by writing/copy-pasting its name, but it’s

File dialog box �We can get the file by writing/copy-pasting its name, but it’s easier with a file dialog box: t = tk. File. Dialog. askopenfilename() 25

File dialog box – contd. �We use the dialog box in the browse function

File dialog box – contd. �We use the dialog box in the browse function which is called by the ‘…’ button def browse(): '''opens a file dialog and sets its result to the filename entry''' t = tk. File. Dialog. askopenfilename() if t: filename. delete(0, END) filename. insert(0, t) 26

Exit the GUI �User: �By clicking the windows X icon �By pressing Ctrl-C in

Exit the GUI �User: �By clicking the windows X icon �By pressing Ctrl-C in the shell �Program: �By calling g. destroy() �Example – quit_() function (‘_’ is used in the name because quit is a python word): def quit_(): '''asks the user if he wants to quit''' if tk. Message. Box. askyesno ('Quit? ', 'Do you want to quit? '): g. destroy() # this actually quits the GUI 27

Reverse text �We create a button that will reverse all the words in the

Reverse text �We create a button that will reverse all the words in the text 28

Reverse text �We create a button that will reverse all the words in the

Reverse text �We create a button that will reverse all the words in the text 29

Reverse word def reverse_word(word): return word[-1: : -1]. capitalize() �We used capitalize so that

Reverse word def reverse_word(word): return word[-1: : -1]. capitalize() �We used capitalize so that the words will look nice: January -> Yraunaj and not yrauna. J 30

Reverse text def reverse(): orig = textbox. text. get(0. 0, END) new() # delete

Reverse text def reverse(): orig = textbox. text. get(0. 0, END) new() # delete the current text for line in orig. split('n'): for word in line. split(' '): textbox. text. insert(END, reverse_word(word)) textbox. text. insert(END, ' ') # ‘ ‘ is a space textbox. text. insert(END, 'n') # newline 31

Reverse button g. bu(text='Reverse', command=reverse) �Add functionality, then add a button �We can now

Reverse button g. bu(text='Reverse', command=reverse) �Add functionality, then add a button �We can now add other functionalities (if time permits): �Count words �Count characters (frequency counter) �Count lines �Decipher (HW 5) �Markov analysis (HW 4) �Translate (http: //www. catonmat. net/blog/python 32 library-for-google-translate/)