Project Part 5 Working Procedures
1. Modules to be imported
a) tkinter (Done in part 4)
b) random, i.e. import random 2. Main program
a) Creation of main window (Done in part 4)
b) Initialization of a list variable to hold the 8 images (to replace the individual image variables used
in part 4), i.e.
image_button = [0,0,0,0,0,0,0,0]
image_button[0] = PhotoImage(file=filename of the first image)
:
image_button[7] = PhotoImage(file=filename of the eighth image)
c) After that, you will use the index numbers (0 to 7) in the list variable to refer to the 8 images, e.g. use image_button[1] to refer to the second image
d) Execute new_board() and mainloop() functons. (Done in part 4)
3. Function new_board()
a) Declare all the variables in b) below as global variables using the global statement global chosen_image, guess_image, select_image
b) Initialization of variables (i.e. giving initial values to them, e.g. -1 because values 0 to 7 may be used to represent the 8 images)
i) A list variable with 3 elements for 3 random images chosen which will be represented by the numbers 0 to 7. This list variable will be used to compare with the list variable of 3 images guess to see whether the guess is correct or not. Its content will be disclosed when the game is over by either reaching the maximum of 10 attempts or all images guessed are correct.
chosen_image = [ -1, -1, -1]
ii) A list variable with 3 elements for 3 images guess which will be represented by the numbers 0 to 7. This list variable will be used to compare with the chosen images each time the O.K. button is clicked. After each attempt of guess, all the 3 values in the list will be initialized again (e.g. -1) so that the list is ready for the next attempt of guess.
guess_image = [ -1, -1, -1]
iii) A variable for image selected from the 8 available images which will be represented by the numbers 0 to 7. After each attempt of guess, its value will be initialized again (e.g. -1) so that it is ready for the next attempt of guess.
select_image = -1
c) Choose 3 numbers (representing the images) randomly from 0 to 7, all 3 numbers cannot be the same (Using while-loop, for-loop and random.randint function for this task) and place them in the list variable chosen_image. After this operation, the list variable may become:
chosen_image = [4, 7, 0]
d) Place Label widgets and Button widgets in the window using grid layout. (Done in part 4) However, you may need to:
i) Change the individual image variable to the corresponding element of the list variable containing the image in the attribute image, i.e. image=image_button[0]
ii) Change the value of the attribute command in the 8 Button widgets under the text Images available to specific functions from image_0 to image_7, i.e. command= image_0
Button(main_window, image=image_button[0], command=image_0).grid()
iii) Change the value of the attribute command in the 3 Button widgets under the text Guess images to specific functions from guess_image_1 to guess_image_3, i.e. command= guess_image_1
Button(main_window, width=10, height=2, bg=light grey, command=get_image_1).grid()
iv) Disable the Button widget O.K. so that it is not clickable at the beginning by using the attribute state=DISABLED
Button(main_window, text=O.K., bg=light grey, font=Arial 14 bold, state=DISABLED).grid()
4. Functions image_0() to image_7()
a) Create for the command attributes of the 8 image Button widgets under the text Images available
def image_0():
: # statements inside the function
return
b) Declare global variable for the variable in 3. b) iii) using the global statement global select_image
c) Assign corresponding number (0 to 7, representing the image) to the image selected variable declared in 3. b) iii) so that it is not the initial value e.g. -1
select_image = 0 # if the first image is selected
d) Replace the Label widget under the text Selected Image to the corresponding image using a new Label widget with attribute image=image_button[?] where ? denotes 0 to 7. For example, in the function image_0(), the attribute will be image=image_button[0]
Label(main_window, relief=RAISED, image=image_button[0]).grid()
5. Functions guess_image_1() to guess_image_3()
a) Create for the command attributes of the 3 Button widgets under the text Guess images
b) If an image is selected from the Images available and the Label widget under the text Selected image shows the image (i.e. the image selected variable is not the initial value e.g. -1), perform the following tasks:
i) Assign the number (0 to 7, representing the image selected) to the corresponding element of the list variable image guess so that it is not the initial value, e.g. -1.
guess_image[0]=select_image
ii) Show the image in the corresponding button under Guess images using a new Button widget with attribute image=image_button[?] to replace the old button.
Button(main_window, image=image_button[select_image], command=guess_image_1).grid()
c) If all the three guess images are filled up (i.e. all the elements in the list are not the initial values, e.g. -1) and they are not the same (i.e. all the elements in the list are not the same number, use if with not in statement to check), make the O.K. button ready to be clicked by using a new Button widget to replace the old one. The new Button widget will have the attribute command= check_result. check_result is a new function for checking whether the guess is correct or not and its content will be set up in project part 6. Therefore, there is no response when it is clicked.
Button(main_window, text=O.K , bg=light grey, font=Arial 14 bold, command=check_result).grid()
d) As a result of c) above, you have to create a new function after the function guess_image_3() as follow:
def check_result() return
e) If you have already used the same image in 2 guess image buttons, the same image cannot be used for the last guess image button, i.e. the last guess image button does not give any response when being clicked. This will be accomplished by using an if statement at the beginning of the function to test whether the values in the variable select_image and the list variable guess_image are the same. If it is the same, then use a return statement to jump out of the function and do not execute any other statements following the if statement.
f) Opportunities should be provided to users to change their mind for the images guessed before clicking the O.K. button. To accomplish this action, the new Button widget in 5. b) ii) above should have the attribute command=current function. For example, if the current function is guess_image_1(), then the attribute will be command=guess_image_1(). That means the new Button widget will execute the function where it resides if it is clicked.
g) After all the three guess images are filled up, the list variable guess_image should be:
guess_image = [1, 3, 6]
First, click this image
Then the corresponding image shown here
If you want to change the guess image, click a new image in order to change the selected image below
Next, click here to show the selected image
Then, click here to change the image
You may choose the same image twice but all the 3 images cannot be the same. Therefore, after the same image is used twice here, the remaining button cannot be used for the same image and there will be no response if it is clicked
The O.K. button is ready to be clicked if all the 3 guess image buttons are filled up
Chapters Reference
1. Using the Random Module (7.7)
2. Input validation (8.9)
3. Command attribute (10.8)
4. Using List, Global vs Local Variable (10.10)
5. Disabled button (10.11)
Program Structure
a) Up to now, the structure of the program is:
Main body of program
b) The flow of functions is:
new_board
Import statements (2 statements)
Functions definition (13 functions)
Window definition statements (5 statements)
Variable definition statements (9 statements)
Function calling statements (2 statements)
8 functions for the Images available buttons
3 functions for the Guess buttons
check_result
Reviews
There are no reviews yet.