讲解:Tic-Tac-Toe、C++,Java、Python、Game BoardProcessing|R

Project Part 4Chapters 10 & 1110.2 What is Tkinter10.4 Creating a Window for Tic-Tac-Toe10.5 Creating the Game Board10.7 Using Label Widget10.8 Using Button Widget10.9 Creating Other Labels11.8 Using ImagesButtonsButtonsNested for-loop1. The part highlighted below in the image can be set up by using a nested for-loop. The outer forloopworks 10 times vertically and the inner for-loop works 3 times horizontally.2. One of the statements in the outer for-loop include a Label widget to show the numbers from 1 to10 which should come from the counter variable of the outer for-loop. As the content from thecounter variable is an integer, you have to change it to a string (using the str() function) beforeusing it in the attribute “text=string” in the Label widget.3. For the inner for-loop, it should show two Label widgets each time it is executed. One is for thesmall rectangle to be used for the peg and the other is an empty label which will be used to showimage later.4. To position the Label widgets correctly using the grid layout, the row and column index numberscan be integers, variables or integers plus variables. Assuming that the value of the variable used inthe examples below will start its value from zero and will increase its value one by one (i.e. theincrement is 1). The variable may come from the counter variable of the for-loop.a. For example, to show the numbers from 1 to 10 in the second column, you may use an integeras the column index, i.e. “column=1” and a variable as the row index, i.e. “row=variable”.b. If you want to start the first label in the second row, the second label in the third row and soon, you may use an integer plus a variable for the row index, i.e. “row=1+variable”. The integer1 indicates which row to start, i.e. the second row.c. If you want to start the first label in the second row, the second label in the fourth row and soon, then you have to multiply the variable with 2, i.e. “row=1+variable*2”. The integer 2indicates how many rows to jump or skip.5. In the Label widget, you may use the attribute “relief=RAISED” inside the parentheses to give anoutline border to the label. You may use other relief as shown in page 152.6. The width and height of the Label w代做Tic-Tac-Toe、代写C++,Java编程设计、Pidget is determined by the attributes “width=x” and “height=x”where x is an integer. You have to set the width and height of the labels to be similar size of theimages that you are going to replace them later in the game.7. Lastly, there are only 5 statements in the nested for-loop just mentioned above to generate thehighlighted area as shown in the below image.The five statements of the nested for-loop will be:for … Label(…).grid(…) for … Label(…).grid(…) Label(…).grid(…)Try to use a nestedfor-loop to set up thispart.Examples of using for-loop to generate Label widgets1. Five Label widgets with border in a vertical position in the first column for x in range(5): Label(main_window, text=, width=10, height=2, relief=RAISED, bg=light grey).grid(row=x, column=0)2. Five Label widgets with text in a vertical position in the first column for x in range(5): Label(main_window, text=GEB204, width=10, height=2, relief=RAISED, bg=light grey).grid(row=x, column=0)3. Five Label widgets with numbers (starting from 1) in a vertical position in the first column for x in range(5): Label(main_window, text=str(x+1), width=10, height=2, relief=RAISED, bg=light grey).grid(row=x, column=0)4. Five Label widgets with border in a vertical position in the second column(Assuming a red Labelwidget is placed in the first row) for x in range(5): Label(main_window, text=, width=10, height=2, relief=RAISED, bg=light grey).grid(row=x+1, column=1)5. Five Label widgets with border in a horizontal position in the second row for x in range(5): Label(main_window, text=, width=10, height=2, relief=RAISED, bg=light grey).grid(row=1, column=x+1)6. Ten Label widgets (half with border) in a horizontal position in the second row for x in range(5): Label(main_window, text=, width=10, height=2, relief=RAISED, bg=light grey).grid(row=1, column=x*2+1) Label(main_window, text=, width=1, bg=light grey).grid(row=1, column=x*2+2)7. Fifteen Label widgets with border in both vertical and horizontal position (3 rows x 5 columns),from the second row for x in range(3): for y in range(5): Label(main_window, text=, width=10, height=2, relief=RAISED, bg=light grey).grid(row=1+x, column=1+y)转自:http://www.daixie0.com/contents/13/4391.html

你可能感兴趣的:(讲解:Tic-Tac-Toe、C++,Java、Python、Game BoardProcessing|R)