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

© 2026 一点知道
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com