Lab: Widgets#

In lesson 10 you learned how to take input from the user. That’s something that every program does one way or another. This lab will help you practice getting and using input.

Don’t forget to run the import cell.

import ipywidgets

Part 1: Using Widgets#

In the next cell there are a collection of widgets that you will use to answer the questions in Part 1.

name_widget = ipywidgets.Text(
year_widget = ipywidgets.Dropdown(
    options=['Freshman', 'Sophomore'],
color_widget = ipywidgets.ColorPicker(
display(name_widget, year_widget, color_widget)

1. Greeting#

Use the value of name_widget to print a greeting like “Hello Mike!”

2. Are You a Sophomore?#

Write a statement that prints True if “Sophomore” is selected and False otherwise.

3. Mind the Default#

Write a statement that prints True if the value in name_widget is the empty string and False otherwise. Hint: The empty string is ""

4. Pick a Color#

Use Python code in the next cell to set the value of color_widget to "yellow".

Part 2: Create Your Own Widget Program#

In this part you’ll create and use widgets based on the examples in the lecture notes.

1. Turtle Controls#

Create three controls for the Turtle:

  1. An integer widget for the length of a side.

  2. An integer widget to control the width of the line.

  3. A color picker for the line color.

You can use any integer widgets you like. Create and display the widgets in the next cell:

2. Draw a Triangle#

Use the Turtle to draw a triangle in the next cell. The triangle should get it’s side length, line thickness and color from the widgets in the previous question.