首页 > > 详细

program留学生辅导、Java,c/c++编程语言调试、Python讲解辅导Web开发|辅导R语言编程

Project Part 6 Working Procedures
1. Module to be imported
For the message box to work, another module has to be imported into the program. It is the function
messagebox which comes from the tkinter module. The import statement should be at the beginning
of the program together with other import statements.
from tkinter import messagebox
2. Function new_board()
Initialize a new variable to record the number of attempts by the player. Its value will be increased
by one each time the “O.K.” is clicked. This variable has to be declared as global variable using the
global statement because it will be used in other functions, e.g. the function check_result().
global chosen_image, guess_image, select_image, attempt
attempt = 0
3. Function check_result()
a) This function was created from project part 5. When the “O.K.” button is clicked, this function
will be carried out.
b) The first statement in this function is the global statement to declare that three variables are
global variables. One is the variable for number of attempts, the second one is the variable to hold
the number of images selected and the last one is the list variable for the 3 guess images.
global attempt, select_image, guess_image
c) The content of the variable for number of attempts will be increased by one to indicate the
updated number of attempts.
attempt +=1
d) The variable to hold the number of images selected will be initialized again (e.g. -1) so that it is
ready for the next attempt of guess.
select_image = -1
e) Move the three images under the text “Guess” to the corresponding row of attempt. This is done
by replacing the 3 blank Label widgets with 3 Label widgets with images.
i) In project part 5, you have a list variable “guess_image” for 3 images guess which will be
represented by the numbers 0 to 7. You should make use of this list variable to represent the
images to be shown in the Label widgets. It is indicated as image_button[y] in the statement
below. It is because we have used variables to represent images as image_button[y] where y
is the image numbers from 0 to 7. You may refer to those Button widgets in the function
new_board.
ii) The number y will come from one of the content of the list variable guess_image and it will
be represent by guess_image[0] for the content of the first element. Then, you can use the
assignment statement y = guess_image[0] to get the first number from the list variable
guess_image and use it as the index number in the image_button.
iii) The variable for number of attempts will be used as the row index in the grid layout so that
the 3 Label widgets with images could be positioned correctly on the game board. The ? in
the row attribute is the starting row to show the first set of guess images. If you start to show
the first set of the 3 guess images in row 4, then ? will be 3 because index number starts
from 0. The # in the column attribute is column to show the first guess image.
iv) A for-loop is used to place the 3 Label widgets with images on the game board. The relief
attribute creates a button effect for the image labels.
for x in range(3):
y = guess_image[x]
Label(main_window, relief=RAISED, image= image_button[y]).grid(row=attempt+?,
column=x+#)
f) After the three images are moved to the row of attempt, you have to reset the following widgets.
i) The Label widget under “Selected image” will be reset to without any image. You should
use a new Label widget to replace the old one.
ii) The “O.K.” Button widget will be disabled. You should use a new Button widget to replace
the old one.
iii) The three Budget widgets under the text “Guess” will be reset to without any image. You
should use 3 new Button widgets to replace the old ones.
g) The list variable” guess_image” to hold the three guess images have to be initialized again (e.g. -
1) so that it is ready for the next attempt of guess.
guess_image=[-1, -1, -1]
h) If the number of attempts reached 10 times, a new function show_chosen_image() will be
executed. At the same time, a message window will pop up to congratulate the player.
if attempt == 10:
show_chosen_image()
messagebox.showinfo('Mastermind', 'Game over!! You have reached the maximum attempt of 10 times!')
4. Function show_chosen_image()
a) This new function will show the 3 chosen images at the top under the text “Result” and at the
same time, disabled all the Button widgets. It is written as a separate new function because it will
be re-use again if the player makes a correct guess of the 3 images, i.e. the player wins the game.
b) There are 3 jobs for this function. For-loop may be used to accomplish the task.
i) Use 3 Label widgets with images to show the 3 chosen images at the top under the text
“Result”. You should use 3 new Label widgets with images to replace the old ones.
ii) Disabled the 8 Button widgets under the text “Images available” using the attribute
state=DISABLED. You should use 8 new Button widgets to replace the old ones.
iii) Disabled the 3 Button widgets under the text “Guess”. You should use 3 new Button
widgets to replace the old ones.
5. Others
a) By making use of the message box, you may display error message in two situations:
i) When the “Image selected” Label widget is empty (i.e. without image), the buttons under
“Guess images” are clicked. This is an error which can be shown by:
messagebox.showerror('Error', 'No image selected!')
ii) When two buttons under “Guess images” carry the same image, you try to choose the same
image for the third button. This is also an error that all the 3 guess images cannot be the
same.
messagebox.showwarning('Warning', 'Triplicate image!')

b) These two statements should be placed in the functions guess_image_1() to guess_image_3()
using if…elif…else statements as:
def guess_image_1():
if condition1:
messagebox.showerror('Error', 'No color selected!')
elif condition2:
messagebox.showwarning('Warning', 'Triplicate color!')
else:
statement to assign the content from the variable select_image to the list variable guess_image
statement to show the Button widget with image
if condition3:
statements to activate the O.K. button
return
c) In the codes above, the text in bold are to be replaced by appropriate expressions or statements.

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!