联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2019-12-01 09:18

Project Part 4

Chapters 10 & 11

10.2 What is Tkinter

10.4 Creating a Window for Tic-Tac-Toe

10.5 Creating the Game Board

10.7 Using Label Widget

10.8 Using Button Widget

10.9 Creating Other Labels

11.8 Using Images

Buttons

Buttons

Nested for-loop

1. The part highlighted below in the image can be set up by using a nested for-loop. The outer forloop

works 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 to

10 which should come from the counter variable of the outer for-loop. As the content from the

counter variable is an integer, you have to change it to a string (using the str() function) before

using 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 the

small rectangle to be used for the peg and the other is an empty label which will be used to show

image later.

4. To position the Label widgets correctly using the grid layout, the row and column index numbers

can be integers, variables or integers plus variables. Assuming that the value of the variable used in

the examples below will start its value from zero and will increase its value one by one (i.e. the

increment 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 integer

as 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 so

on, you may use an integer plus a variable for the row index, i.e. “row=1+variable”. The integer

1 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 so

on, then you have to multiply the variable with 2, i.e. “row=1+variable*2”. The integer 2

indicates how many rows to jump or skip.

5. In the Label widget, you may use the attribute “relief=RAISED” inside the parentheses to give an

outline border to the label. You may use other relief as shown in page 152.

6. The width and height of the Label widget 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 the

images 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 the

highlighted 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 nested

for-loop to set up this

part.

Examples of using for-loop to generate Label widgets

1. 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 Label

widget 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)


版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp