recitation 11 programming for engineers in python

Click here to load reader

Post on 20-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

  • Slide 1
  • Recitation 11 Programming for Engineers in Python
  • Slide 2
  • Plan GUI Swampy Text widgets PyPad 2
  • Slide 3
  • Install Swampy 3 Download Swampy: http://greenteapress.com/thinkpython/swampy/swampy- 2.0.python2.zip http://greenteapress.com/thinkpython/swampy/swampy- 2.0.python2.zip Unzip to C:\Python27\Lib\site-packages Now you should have a directory C:\Python27\Lib\site- packages\swampy-2.0 Download this file http://db.tt/gQqPvRok (it might open as a text file, right click and choose Save as) and also put it in C:\Python27\Lib\site-packages, making sure its name is swampy.pthhttp://db.tt/gQqPvRok Now you should have a file C:\Python27\Lib\site- packages\swampy.pth
  • Slide 4
  • Check Swampy was installed 4 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
  • Slide 5
  • GUI - reminder 5 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
  • Slide 6
  • History of GUI 6 http://www.youtube.com/watch?v=TZGGUrom1Mg
  • Slide 7
  • Widgets 7 In class: Button Label Canvas
  • Slide 8
  • More widgets! 8 Textual input widgets: Entry single line entry_example.py: https://gist.github.com/1523427https://gist.github.com/1523427 Text multiple lines text_example.py: https://gist.github.com/1523471https://gist.github.com/1523471 Think Python 19.5
  • Slide 9
  • More complex example 9 Change the color of a circle based on the color name given by the user circle_demo.py: thinkpython.com/code/circle_demo.pythinkpython.com/code/circle_demo.py Think Python 19.5 pg. 185
  • Slide 10
  • PyNote a Python Notepad 10
  • Slide 11
  • Packing 11 >>> g = Gui() >>> g.title('PyNote 0.1') Creates the GUI & title
  • Slide 12
  • Packing 12 >>> g.col(weights=[1,0]) Creates a column that holds widgets Weights later
  • Slide 13
  • Packing 13 >>> g.st() st: Scrollable text widget
  • Slide 14
  • Packing 14 >>> 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
  • Slide 15
  • Resizing 15 With weights=[1,0,0,0] Without specifying weights
  • Slide 16
  • Packing 16 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)
  • Slide 17
  • Check if a file exists 17 os.path.exists(filename) Returns True if file exists, False if it does not
  • Slide 18
  • Command 18 g.bu(text='Save', command=save) def save(): '''Saves the current note in a file''' if exists(filename.get()): # exists is os.path.exists if not tkMessageBox.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() tkMessageBox.showinfo('Save','File saved: +filename.get())
  • Slide 19
  • Event driven programming 19 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 >>> def freeze(): time.sleep(5) >>> g=Gui() >>> g.bu(text=Freeze!,command=freeze) >>> g.mainloop()
  • Slide 20
  • tkMessageBox 20 Creates a message box that asks the user a question or gives him information Usually with a button or two Examples: tkMessageBox.showinfo('Load','File does not exist: '+filename.get()) tkMessageBox.askyesno('Quit?','Do you want to quit?') tkMessageBox.askyesno('Save','File already exists, do you want to overwrite?') http://www.tutorialspoint.com/python/tk_messagebox.htm
  • Slide 21
  • 21 tkMessageBox.askyesno('Save','File already exists, do you want to overwrite?')
  • Slide 22
  • tkMessageBox.askyesno('Quit?','Do you want to quit?') 22
  • Slide 23
  • tkMessageBox.showinfo('Load','File does not exist: + filename.get()) 23
  • Slide 24
  • Load command 24 def load(): '''Opens a file to the text widget''' if not exists(filename.get()): tkMessageBox.showinfo('Load','File does not exist: ' +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()
  • Slide 25
  • File dialog box 25 We can get the file by writing/copy-pasting its name, but its easier with a file dialog box: t = tkFileDialog.askopenfilename()
  • Slide 26
  • File dialog box contd. 26 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 = tkFileDialog.askopenfilename() if t: filename.delete(0,END) filename.insert(0,t)
  • Slide 27
  • Exit the GUI 27 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 tkMessageBox.askyesno ('Quit?','Do you want to quit?'): g.destroy() # this actually quits the GUI
  • Slide 28
  • Reverse text 28 We create a button that will reverse all the words in the text
  • Slide 29
  • Reverse text 29 We create a button that will reverse all the words in the text
  • Slide 30
  • Reverse word 30 def reverse_word(word): return word[-1::-1].capitalize() We used capitalize so that the words will look nice: January -> Yraunaj and not yraunaJ
  • Slide 31
  • Reverse text 31 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
  • Slide 32
  • Reverse button 32 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-library-for- google-translate/)http://www.catonmat.net/blog/python-library-for- google-translate/