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.

186 Chapter 19. Casestudy: TkinterThissectionpresentsthecodethatcreatesthisGUI,brokenintoaseriesofsteps. Youcandownloadthecomplete example fromthinkpython.com/code/SimpleTurtleWorld.py.At the top level, this GUI contains two widgets—a Canvas and a Frame—arranged in a row. So thefirststepistocreate the row.class SimpleTurtleWorld(TurtleWorld):"""This class is identical to TurtleWorld, but the code thatlays out the GUI is simplified for explanatory purposes."""def setup(self):self.row()...setup is the function that creates and arranges the widgets. Arranging widgets in a GUI is calledpacking.row creates a row Frame and makes it the “current Frame.” Until this Frame is closed or anotherFrame iscreated, all subsequent widgets are packed inarow.Here isthecode that creates the Canvas and thecolumn Frame that hold theother widgets:self.canvas = self.ca(width=400, height=400, bg='white')self.col()The firstwidget inthecolumn isagridFrame, which contains four buttons arranged two-by-two:self.gr(cols=2)self.bu(text='Print canvas', command=self.canvas.dump)

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

Saved successfully!

Ooh no, something went wrong!