讲解:program、Java,c/c++、PythonWeb|R

Project Part 6 Working Procedures1. Module to be importedFor the message box to work, another module has to be imported into the program. It is the functionmessagebox which comes from the tkinter module. The import statement should be at the beginningof the program together with other import statements.from tkinter import messagebox2. Function new_board()Initialize a new variable to record the number of attempts by the player. Its value will be increasedby one each time the “O.K.” is clicked. This variable has to be declared as global variable using theglobal statement because it will be used in other functions, e.g. the function check_result().global chosen_image, guess_image, select_image, attemptattempt = 03. Function check_result()a) This function was created from project part 5. When the “O.K.” button is clicked, this functionwill be carried out.b) The first statement in this function is the global statement to declare that three variables areglobal variables. One is the variable for number of attempts, the second one is the variable to holdthe number of images selected and the last one is the list variable for the 3 guess images.global attempt, select_image, guess_imagec) The content of the variable for number of attempts will be increased by one to indicate theupdated number of attempts.attempt +=1d) The variable to hold the number of images selected will be initialized again (e.g. -1) so that it isready for the next attempt of guess.select_image = -1e) Move the three images under the text “Guess” to the corresponding row of attempt. This is doneby 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 berepresented by the numbers 0 to 7. You should make use of this list variable to represent theimages to be shown in the Label widgets. It is indicated as image_button[y] in the statementbelow. It is because we have used variables to represent images as image_button[y] where yis the image numbers from 0 to 7. You may refer to those Button widgets in the functionnew_board. ii) The number y will come from one of the content of the list variable guess_image and it willbe represent by guess_image[0] for the content of the first element. Then, you can use theassignment statement y = guess_image[0] to get the first number from the list variableguess_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 thatthe 3 Label widgets with images could be positioned correctly on the game board. The ? inthe row attribute is the starting row to show the first set of guess images. If you start to showthe first set of the 3 guess images in row 4, then ? will be 3 because index number startsfrom 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 reliefattribute 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 shoulduse 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 replacethe old one.iii) The three Budget widgets under the text “Guess” will be reset to without any image. Youshould use 3 new Buttprogram代写、Java,c/c++编程语言调试、Pyton 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 beexecuted. 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 thesame time, disabled all the Button widgets. It is written as a separate new function because it willbe 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 attributestate=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 Buttonwidgets to replace the old ones.5. Othersa) 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 sameimage for the third button. This is also an error that all the 3 guess images cannot be thesame.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.When the 3 images arefilled up under the text“Guess”, the “O.K.”button can be clicked. After the “O.K.” button isclicked, the “O.K.” buttonbecomes disabled. The 3buttons under the text“Guess” and the labelunder “Selected image”are reset without images.After the “O.K.” buttonis clicked, the 3 imagesfrom the “Guess” roware copied to the row ofattempt. After 10 attempts, therow of “Result” showsthe 3 chosen images.The 8 images buttonsunder “Imagesavailable” becomedisabled.After 10 attempts, the 3buttons under “Guess”become disabled.Program Structurea) Up to now, the structure of the program is:b) The flow of functions is:Import statements(3 statements)Functions definition(14 functions)Main body of programFunction calling statements(2 statements)Variable definition statements(9 statements)Window definition statements(5 statements)new_board8 functions for the“Images available”buttonscheck_resultshow_chosen_image3 functions for the“Guess” buttons转自:http://www.daixie0.com/contents/13/4392.html

你可能感兴趣的:(讲解:program、Java,c/c++、PythonWeb|R)