Write a program that creates a GUI window with following objects and their functionalities:
Layout and the content of the GUI window after 5 clicks on the left button is shown in the diagram below. see image.
Implement function delay(d), which returns after a time delay specified by inter d.
Implement function is_button(click, rect), which returns True if point click is within rectangle rect and returns False otherwise.
Initially put text "0" into the left button, and text Quit into the right button. If the click was within left button:
Slide 3, lecture 8.5, explains how to use object Text to display a text on a button. Use method setText(), slide 4, to change the value of the counter on the left button.
The algorithm for the main function looks like this:
Create a window (win = GraphWin(…))
Draw left button and text on the left button
Draw right button and text on the right button
while (True):
click = win.getMouse()
if the right button clicked, break to terminate program
elif the left button clicked
change color of the left button
delay(1000)
return the button to original color
increment counter on the left button
close the window
Write a program that uses a graphical user interface (GUI) to calculate body mass index (BMI). The GUI window should have two objects for entering the height in inches and weight in pounds, a text object for displaying BMI and two buttons:
Ignore invalid inputs.
Assignment 6, question 2, describes how to calculate the BMI.
The layout of the GUI window: see image.
I suggest that you start with program that you developed in Question 1 and gradually modify it to implement this program. In the first step just insert additional objects, then add operations triggered by a click on the left button and right button.
Slide 7, lecture 8.5, illustrates the use of an entry object.
In this question, you will implement a program that keeps track of a game, described in Assignment 7, Question 5, but this time with graphical user interface. Please see Assignment 7, question 5 for the description of how to score the game.
Write a program that uses a graphical user interface (GUI) to keep track of the score in the game and displays a player who wins the game.
The GUI is supposed to contain the following objects:
The buttons have to change color when clicked to mimic button click.
Layout of the GUI window: see image.
I suggest the following approach
Write a program that creates a window with a circle that bounces from the window walls. The program terminates when the user hits "q" key.
The layout of the graphics window: see image.
Use method move(dx, dy) to move a circle from the current position dx in the x direction and dy in the y direction. Initially set dx = 1 and dy = 1. If a circle hits a vertical wall than change the horizontal direction (dx = - dx). If the circle hits a horizontal wall, then change the vertical direction (dy = - dy).
The circle will hit a wall if the center of the circle is within radius distance of the wall. Use method getCenter() to obtain a point which is the center of the circle. The following code will bounce the circle from a vertical wall:
# r is a radius, "width" is the width of the window
c = circ.getCenter()
if ((c.getX() - r) <= 0) or ((c.getX() + r) >= width):
dx = - dx
Note that you will have to slow down the movement of the circle; otherwise, the circle would move too fast. The algorithm looks like this:
create window
draw a circle
dx = 1
dy = 1
while True
move circle by dx and dy
delay(50)
change x direction if circle hits a vertical wall
change y direction if circle hits a horizontal wall
break if the user terminates the program
close the window
Terminate the program by entering "q" on the keyboard. The following code breaks the event loop to quit the program on key q:
key = win.checkKey()
if key == "q": # loop exit
break
Write a program that creates a circle at a random point in a window. If the user clicks within the circle, it will disappear and the program draws a new circle at a random point.
If the user does not click on the circle within a specified time interval:
Sample outputs: see image.
You will need function randrange(m, n) from the random library to generate a random coordinates of a circle within the windows. If r is the radius, width and height the dimensions of the window, than generate a random center point of the circle with statements:
from random import randrange
center = Point(randrange(r, width -r), randrange(r, height - r))
The algorithm:
create window
while True:
create a circle at a random point within the window
delay
# experiment with various delay intervals
if there is a mouse click:
break if the mouse click is not in the circle
break if there is no mouse click
display label for game over
wait for mouse click
close the window