12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

184 Chapter 19. Casestudy: Tkinter19.4 CoordinatesequencesTherectanglemethodtakesasequenceofcoordinatesthatspecifyoppositecornersoftherectangle.Thisexampledrawsagreenrectanglewiththelowerleftcornerattheoriginandtheupperrightcorner at (200,100):canvas.rectangle([[0, 0], [200, 100]],fill='blue', outline='orange', width=10)Thiswayofspecifyingcornersiscalledaboundingboxbecausethetwopointsboundtherectangle.ovaltakes a bounding box and draws an oval withinthespecified rectangle:canvas.oval([[0, 0], [200, 100]], outline='orange', width=10)linetakesasequenceofcoordinatesanddrawsalinethatconnectsthepoints. Thisexampledrawstwolegs ofatriangle:canvas.line([[0, 100], [100, 200], [200, 100]], width=10)polygontakesthesamearguments,butitdrawsthelastlegofthepolygon(ifnecessary)andfillsitin:canvas.polygon([[0, 100], [100, 200], [200, 100]],fill='red', outline='orange', width=10)19.5 MorewidgetsTkinter provides two widgets that let users type text: an Entry, which is a single line, and a Textwidget, which has multiplelines.encreates a new Entry:entry = g.en(text='Default text.')Thetextoptionallowsyoutoputtextintotheentrywhenitiscreated. Thegetmethodreturnsthecontents of theEntry (which mayhave been changed by theuser):>>> entry.get()'Default text.'tecreates a Text widget:text = g.te(width=100, height=5)widthandheightarethedimensions of the widget incharacters and lines.insertputs text intotheText widget:text.insert(END, 'A line of text.')ENDisaspecial index that indicates the lastcharacter inthe Text widget.You can also specify a character using a dotted index, like 1.1, which has the line number beforethe dot and the column number after. The following example adds the letters 'nother' after thefirstcharacter of thefirstline.

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

Saved successfully!

Ooh no, something went wrong!