12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

192 Chapter 19. Casestudy: Tkinteroption: A value that controls the appearance orfunction ofawidget.keyword argument: An argument that indicates theparameter name aspart ofthe function call.callback: A function associated withawidget that iscalled when theuser performs anaction.bound method: A method associated withaparticular instance.event-driven programming: Astyleofprogramminginwhichtheflowofexecutionisdeterminedby user actions.event: A user action, likeamouse click orkey press,that causes aGUI torespond.event loop: Aninfinite loop that waits foruser actions and responds.item: A graphical element on aCanvas widget.bounding box: Arectanglethatenclosesasetofitems,usuallyspecifiedbytwoopposingcorners.pack: To arrange and displaythe elements ofaGUI.geometry manager: A system for packing widgets.binding: An association between a widget, an event, and an event handler. The event handler iscalled when theevent occurs inthe widget.19.11 ExercisesExercise 19.4 For this exercise, you willwritean image viewer. Here isasimpleexample:g = Gui()canvas = g.ca(width=300)photo = PhotoImage(file='danger.gif')canvas.image([0,0], image=photo)g.mainloop()PhotoImage reads a file and returns a PhotoImage object that Tkinter can display. Canvas.imageputs the image on the canvas, centered on the given coordinates. You can also put images on labels,buttons, and some other widgets:g.la(image=photo)g.bu(image=photo)PhotoImage can only handle a few image formats, like GIF and PPM, but we can use the <strong>Python</strong>Imaging Library (PIL)toread other files.The name of the PIL module is Image, but Tkinter defines an object with the same name. To avoidtheconflict, you can useimport...aslikethis:import Image as PILimport ImageTkThe first line imports Image and gives it the local name PIL. The second line imports ImageTk,which can translateaPILimage intoaTkinter PhotoImage. Here’san example:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!