Python Formula Average Miles Per Gallon

76 # Create an instance of the MyGUI class.

12-25 Window displayed by Program 12-1 3

12-25 Window displayed by Program 12-1 3

r? Option 1 f* Option 2 P Option 3 OK I Quit r? Option 1 f* Option 2 P Option 3 OK I Quit

Checkpoint

12.15 You want the user to be able to select only one item from a group of items. Which type of component would you use for the items, radio buttons or check boxes?

12.16 You want the user to be able to select any number of items from a group of items. Which type of component would you use for the items, radio buttons or check boxes?

12.17 How can you use an IntVar object to determine which Radiobutton has been selected in a group of Radiobuttons?

12.18 How can you use an IntVar object to determine whether a Checkbutton has been selected?

Review Questions

Multiple Choice

1. The_„___ is the part of a computer with which the user interacts.

a. centra] processing unit b. user interface c. control system cl interactivity system

2. Before GUIs became popular, the_ interface was the most commonly used.

a. command line b. remote terminal c. sensory d. event-driven

3. A_is a small window that displays information and allows the user to perform actions.

a. menu b. confirmation window c. startup screen d. dialog box

4. These types of programs that are event driven.

a. command line b. text-based c. GUI

d. procedural

5. An item that appears in a program's graphical user interface is known as a a. gadget b. widget c. tool d. iconified object

6. You can use this module in Python to create GUI programs.

b. PythonGui c. Tkinter d. Tgui

7. This widget is an area that displays one line of text.

a. Label b. Entry c. TextLine d. Canvas

8. This widget is an area in which the user may type a single line of input from the keyboard.

a. Label b. Entry c. TextLine d. Input

9. This widget is a container that can hold other widgets.

a. Grouper b. Composer c. Fence d. Frame

10. This method arranges a widget in its proper position, and it makes the widget visible when the main window is displayed.

a. pack b. arrange c. position d. show

11. A(n)_ is a function or method that is called when a specific event occurs.

a. callback function b. auto function c. startup function d. exception

12. The showinfo function is in this module.

a, Tkinter b, Tklnfo c, sys d, tkMessageBox

13. You can call this method to close a GUI program.

a. The root widget's close method b. Any widget's quit method c. The sys.shutdown function d. The Tk. shutdown method

14. You call this method to retrieve data from an Entry widget.

15. An object of this type can be associated with a Label widget, and any data stored in che object will be displayed in the Label, a. StringVar b. LabelVar c. LabelValue d. DisplayVar

16. If there are a group of these in a container, only one of them can be selected at any given time.

a. Checkbutton b. Radiobutton c. Mutualbutton d. Button

True or False

1. The Python language has built-in keywords for creating GUI programs.

2. Every widget has a quit method that can be called to close the program.

3. The data that you retrieve from an Entry widget is always of the int data type.

4. A mutually exclusive relationship is automatically created among all Raciiobutton widgets in the same container.

5. A mutually exclusive relationship is automatically created among all Checkbutton widgets in the same container.

Short Answer

1. When a program runs in a text-based environment^ such as a command line interface, what determines the order in which things happen?

2. What does a widget's pack method do?

3. What does the Tkinter module's mainloop function do?

4. If you create two widgets and call their pack methods with no arguments, how will the widgets be arranged inside their parent widget?

5. How do you specify that a widget should be positioned as far left as possible inside its parent widget?

6. How do you retrieve data from an Entry widget?

7. How can you use a StringVar object to update the contents ot a Label widget?

8. How can you use an IntVar object to determine which Radiobutton has been selected in a group of Radiobuttons?

9. How can you use an IntVar object to determine whether a Checkbutton has been selecred?

Algorithm Workbench

L Write a statement that creates a Label widget. Irs parent should be self .main_ window and its text shoud be ' Programming is fun!'

2. Assume self, labell and self. Iabel2 reference two Label widgets. Write code that packs the two widgets so they are positioned as far left as possible inside their parent widget.

3. Write a statement that creates a Frame widget. Its parent should be self .main_ window.

4. Write a statement that displays an info dialog box with the title "Program Paused" and the message "Click OK when you are ready to continue."

5. Write a statement that creates a Button widget. Its parent should be self .button_ frame, its text should be 'Calculated and its callback function should be the self-calculate method.

6. Write a statement that creates a Button widget that closes the program when it is clicked. Its parent should be self. buttonf rame, its text should be 'Quit'.

7. Assume the variable data^entry references an Entry widget. Write a statement that retrieves the data from the widget, converts it to an int, and assigns it to a variable named var.

Programming Exercises

1. Name and Address

Write a GUI program that that displays your name and address when a button is clicked. The program's window should appear as the sketch on the left side of Figure 12-26 when it runs. When the user clicks the Show Info button, the program should display your name and address, as shown in the sketch on the right of the figure.

Figure 12-26 Name and address program

■Steven Marcus 214 Pail-j Drive VJaynesville, mW

Show Info

Quit

2. Latin Translator

Look at the following list of Latin words and their meanings.

Latin

English

sinister

left

dexter

right

medium

center

Write a GUI program thar translates rhe Latin words to English. The window should have three buttons, one for each Latin word. When the user clicks a button, the program displays the English translation in a label.

3. Miles-per Gallon Calculator

Write a GUI program that calculates a car's gas mileage. The program s window should have Entry widgets that let the user enter rhe number of gallons of gas the car holds, and the number of miles it can be driven on a full tank. When a Calculate MPG button is clicked, the program should display the number of miles that the car may be driven per gallon of gas. Use the following formula to calculate miles-per-gallon:

gallons

4. Celsius to Fahrenheit

Write a GUI program that converts Celsius temperatures to Fahrenheit temperatures. The user should be able to enter a Celsius temperature, click a button, and then see the equivalent Fahrenheit temperature. Use the following formula to make the conversion:

+2 0

Post a comment