Week 11: Exercises

1. Bidirectional Temperature

Modify the Fahrenheit-to-Celsius program that we saw in the lecture so that it can convert in both directions. As one possible user interface, you could have two Entry boxes, one for Fahrenheit and one for Celsius. When the user presses the Convert button, if either box is empty, it will be filled in by converting the value from the other box. As another possible user interface, you could let the user choose the direction of the conversion via a checkbutton or group of radio buttons.

2. Calculator

Write a Tkinter program that implements a simple calculator on integers. The program should let the user enter two numbers, choose an operation (+, -, *, or /), then press a button 'Compute' to see the result. The / operator should implement integer division. The program might look something like this:

3. Colors

Write a Tkinter program that lets the user enter the red, green, and blue components of a color, each of which should be an integer in the range from 0 to 255. When the user presses a button labelled Update, the lower part of the window should fill with the specified color. The program might look like this:

Hint: Use a canvas to display the color. You can use the configure() method to set its 'background' option to the color the user has specified.

4. Random Lines

Write a Tkinter program that draws 1000 random lines of length 300 pixels, each with a random color. All line angles should be equally likely.

5. Czech Flag

Write a Tkinter program that draws a Czech flag:

Do not use any external image files – instead, the program should draw the flag itself using filled rectangles and/or polygons.

6. Drawing Lines

Write a Tkinter program that lets the user draw lines. Initially the program should display a blank canvas. If the user clicks and drags the mouse, the program should display a line from the point where the user clicked to the current mouse position. When the user releases the mouse, the line should remain on the canvas. Each line should have a random color.