As usual, create a directory to hold today's activities:
$ cd ~/cs170/labs $ mkdir lab1 $ cd lab1
Or, you can do it the slightly more efficient way:
$ mkdir ~/cs170/labs/lab1 $ cd ~/cs170/labs/lab1
The Tk toolkit is an open source mechanism for creating graphical windows. As a matter of fact, it was used extensively for creating windows in the Linux environment, although most variants have moved away from this setup. The nice thing about Tk is that it can be used to create "professional" looking windows, complete with file menus, buttons, etc.
Today, you are going to get an overview on how you can use Tk to create drawings, similar to your original introduction to Turtle.
Yes, I know you've drawn fish before. However, it is different not only in every language, but also in every graphical toolkit. So, you're doing it again!
In a file called gone_fishing.py, create a function
draw_fish(canvas, x, y)
, which draws a fish on the
canvas, with the center of the fish placed on the specified
coordinates. Your fish should look like the one featured below:
Use this function to draw at least one fish on the canvas
You will have to create a canvas, and pack that into your root window. All of the drawing methods belong to the canvas object, so you need a reference to that canvas in order to draw.
You only need to draw two arcs to complete a fish: one for the tail, and another for the head. You will need to use some math to figure out what coordinates to specify for the bounding rectangle.
This is probably a little boring looking. Spruce it up a bit. You should be able to figure out how to change the background color of the canvas (maybe to blue, for water?). Add some details to the fish, such as eyes or gills. Get creative!
Tkinter allows us to add more than one thing to our graphical interfaces. In the past, we focused mostly on drawing, so Turtle and the Graphics modules were sufficient for that. However, real applications have buttons, and menus associated with them. Today, you will get practice with how buttons work in Tkinter
In your gone_fishing.py file, add two buttons to the bottom of the canvas. One button should allow the user to move the drawing of the fish to the left, while the other button should allow the user to move the drawing of the fish to the right.
You will have to create a canvas, and pack that into your root window. All of the drawing methods belong to the canvas object, so you need a reference to that canvas in order to draw.
One nuance of the "callback" function is that you can only really access elements from the global scope. Which poses a problem because we cannot modify the values of variables in global scope...or can we?
If we leverage the capabilities of mutability we
can technically modify variables in the global scope.
Create a global variable called fish_location
,
which is a list of 2 values: a x-coordinate and a
y-coordinate. You will use this list to determine where to
draw the fish.
You will use the Button function from the Tk module to create a button to be placed on the tk Window. The important thing to note is that you need to set a command for the button to execute when clicked.
Write two additional functions: move_left()
and move_right()
. Note that neither of these
functions take parameters. These functions should modify
variables which specify where the fish is being drawn, then
redraw the fish.
Every time a button is pressed, you need to completely clear
the canvas. You can use the
command canvas.delete(tk.ALL)
in order to
delete everything from the canvas.
Add more buttons to modify the fish. Maybe you want to add a button that changes the color of the fish. Maybe you want to add buttons that changes the size of the fish. The only requirement for this challenge portion is that you add at least 3 additional buttons that modify the fish in some way, other than position.
When you have finished, create a tar file of your lab1
directory. To create a tar file, execute the following commands:
cd ~/cs170/labs tar czvf lab1.tgz lab1/
To submit your activity, go to inquire.roanoke.edu. You should
see an available assignment called Lab Assignment 1
.
Make sure you include a header listing the authors of the file.