用canvas画直线和棋盘(python)

 时间:2026-02-15 00:20:04

1、先建立一个300*190的画板(画布、canvas),然后画对角线:

from tkinter import *

master = Tk()

canvas_width = 300

canvas_height = 190

w = Canvas(master, width=300,height=190)

w.pack()

w.create_line(0,0,300,190)

mainloop()

用canvas画直线和棋盘(python)

2、把对角线变粗,线色改为绿色:

w.create_line(0,0,300,190,

              fill='green',

              width=2,)

用canvas画直线和棋盘(python)

3、画一条贯穿画布的绿色横线:

w.create_line(0,30,300,30,

              fill='green',

              width=2,)

用canvas画直线和棋盘(python)

4、再画一条纵贯画布的红色竖线:

w.create_line(100,0,100,190,

              fill='red',

              width=2,)

用canvas画直线和棋盘(python)

5、用for语句批量画横线和竖线,就是棋盘:

for i in range(10):

    w.create_line(0,int(190/10*i),300,int(190/10*i),

                  fill='green',

                  width=2,)

    w.create_line(int(300/10*i),0,int(300/10*i),190,

                  fill='red',

                  width=2,)

用canvas画直线和棋盘(python)

6、棋盘的网格线密集化。

用canvas画直线和棋盘(python)

7、用两个for循环,把横线和竖线分开来画。

用canvas画直线和棋盘(python)

用canvas画直线和棋盘(python)

  • 安装webpy框架提示No module named 'utils'
  • WPS演示文稿如何插入不等号?
  • Word如何拆分表格
  • 怎么给声音去噪?
  • Word文档如何插入封面
  • 热门搜索
    庐山旅游 沈阳旅游 香格里拉旅游 上海旅游集散中心 四川旅游学院 春节旅游 长治旅游 井冈山旅游攻略 南京旅游 湖南旅游