python代码使用tkinter库和canvas画布实现井字棋九宫格

import tkinter as tk


def draw_grid(canvas, width, height, line_color='black'):
    # 计算每个格子的宽度高度
    cell_width = width / 3
    cell_height = height / 3

    # 绘制垂直线
    for i in range(3):
        x = i * cell_width
        # 计算当前循环中垂直线的x坐标。
        # 由于九宫格是3x3的,所以每条垂直线的x坐标是格子宽度的整数倍,
        # 通过将循环变量 i 乘以 cell_width 来计算得到。
        canvas.create_line(x, 0, x, height, fill=line_color)    # 绘制一条从(0, x)到(height, x)的垂直线, 线条颜色:黑色(默认)
        canvas.create_line(x + cell_width, 0, x + cell_width, height, fill=line_color)  # 在画布上绘制九宫格。与上一条线的间隔为一个格子的宽度

    # 绘制水平线
    for i in range(3):
        y = i * cell_height
        canvas.create_line(y, 0, y, width, fill=line_color)
        canvas.create_line(0, y + cell_height, width, y + cell_height, fill=line_color)


def draw_numbers(canvas, width, height):
    # 计算每个格子的宽度高度
    cell_width = width / 3
    cell_height = height / 3

    # 在每个格子中心绘制数字
    for i in range(3):
        for j in range(3):
            x = i * cell_width + cell_width / 2     # 计算当前格子中心的x坐标。这是通过将列索引i乘以格子高度,然后加上格子高度的一半来得到
            y = j * cell_height + cell_height / 2   # 计算当前格子中心的y坐标。这是通过将列索引j乘以格子高度,然后加上格子高度的一半来得到
            number = i * 3 + j + 1
            # 计算当前格子应该绘制的数字。
            # 由于九宫格是从1开始编号的,所以通过行索引i乘以3(因为每行有3个格子),
            # 然后加上列索引j,最后加上1来得到正确的数字。
            canvas.create_text(y, x, text=str(number), font=("Helvetica", 20))


root = tk.Tk()
root.title('九宫格')
root.geometry("600x500")

canvas = tk.Canvas(root, width=300, height=300, bg="white")
canvas.pack()
draw_grid(canvas, 300, 300) # 在画布上绘制九宫格的网格线
draw_numbers(canvas, 300, 300)
# 调用了定义的draw_numbers函数,将画布、宽度(300像素)和高度(300像素)作为参数传递,
# 在画布上绘制数字1到9,每个数字位于九宫格的一个格子中心。

label2 = tk.Label(text="请输入要落下棋子的位置(1-9):", width=30, height=2)
label2.pack()
label2.place(x=190, y=310)

entry2 = tk.Entry(width=20)
entry2.pack()
entry2.place(x=220, y=350)

button11 = tk.Button(text="落下棋子", width=10, height=2)
button11.pack()
button11.place(x=250, y=380)

button22 = tk.Button(text="重新开始", width=10, height=2)
button22.pack()
button22.place(x=250, y=430)

root.mainloop()

分段解析

1.导入Tkinter库,并重命名为tk:

import tkinter as tk

2.定义draw_grid函数,用于绘制九宫格的网格线:

def draw_grid(canvas, width, height, line_color='black'):
    # 计算每个格子的宽度高度
    cell_width = width / 3
    cell_height = height / 3

    # 绘制垂直线
    for i in range(3):
        x = i * cell_width
        canvas.create_line(x, 0, x, height, fill=line_color)
        canvas.create_line(x + cell_width, 0, x + cell_width, height, fill=line_color)

    # 绘制水平线
    for i in range(3):
        y = i * cell_height
        canvas.create_line(0, y, width, y, fill=line_color)
        canvas.create_line(0, y + cell_height, width, y + cell_height, fill=line_color)

 3.定义draw_numbers函数,用于在九宫格的每个格子中心绘制数字1到9:

def draw_numbers(canvas, width, height):
    # 计算每个格子的宽度高度
    cell_width = width / 3
    cell_height = height / 3

    # 在每个格子中心绘制数字
    for i in range(3):
        for j in range(3):
            x = i * cell_width + cell_width / 2
            y = j * cell_height + cell_height / 2
            number = i * 3 + j + 1
            canvas.create_text(x, y, text=str(number), font=("Helvetica", 20))

4. 创建主窗口,设置标题和大小:

root = tk.Tk()
root.title('九宫格')
root.geometry("600x500")

 5.创建一个画布,并在画布上绘制九宫格的网格线和数字:

canvas = tk.Canvas(root, width=300, height=300, bg="white")
canvas.pack()
draw_grid(canvas, 300, 300)
draw_numbers(canvas, 300, 300)

 6.创建标签、输入框和按钮,并设置它们在主窗口中的位置:

label2 = tk.Label(text="请输入要落下棋子的位置(1-9):", width=30, height=2)
label2.pack()
label2.place(x=190, y=310)

entry2 = tk.Entry(width=20)
entry2.pack()
entry2.place(x=220, y=350)

button11 = tk.Button(text="落下棋子", width=10, height=2)
button11.pack()
button11.place(x=250, y=380)

button22 = tk.Button(text="重新开始", width=10, height=2)
button22.pack()
button22.place(x=250, y=430)

 7.启动Tkinter事件循环,显示主窗口和所有组件:

root.mainloop()


 

  • 15
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值