Page 206 - thinkpython
P. 206
184 Chapter 19. Case study: Tkinter
Figure 19.1: TurtleWorld after running the snowflake code.
self.canvas = self.ca(width=400, height=400, bg= 'white ')
self.col()
The first widget in the column is a grid Frame, which contains four buttons arranged two-
by-two:
self.gr(cols=2)
self.bu(text= 'Print canvas ', command=self.canvas.dump)
self.bu(text= 'Quit ', command=self.quit)
self.bu(text= 'Make Turtle ', command=self.make_turtle)
self.bu(text= 'Clear ', command=self.clear)
self.endgr()
gr creates the grid; the argument is the number of columns. Widgets in the grid are laid
out left-to-right, top-to-bottom.
The first button uses self.canvas.dump as a callback; the second uses self.quit . These
are bound methods, which means they are associated with a particular object. When they
are invoked, they are invoked on the object.
The next widget in the column is a row Frame that contains a Button and an Entry:
self.row([0,1], pady=30)
self.bu(text= 'Run file ', command=self.run_file)
self.en_file = self.en(text= 'snowflake.py ', width=5)
self.endrow()
The first argument to row is a list of weights that determines how extra space is allocated
between widgets. The list [0,1] means that all extra space is allocated to the second wid-
get, which is the Entry. If you run this code and resize the window, you will see that the
Entry grows and the Button doesn’t.
The option pady “pads” this row in the y direction, adding 30 pixels of space above and
below.