Here is an implementation that allows you to set a hexagonal cell of the desired color and also allow you to create custom border colors.
This is all done manually, it just took 1 hour using your link site. You may need to adapt it to your needs, but it seems to work.
from tkinter import * class HexaCanvas(Canvas): """ A canvas that provides a create-hexagone method """ def __init__(self, master, *args, **kwargs): Canvas.__init__(self, master, *args, **kwargs) self.hexaSize = 20 def setHexaSize(self, number): self.hexaSize = number def create_hexagone(self, x, y, color = "black", fill="blue", color1=None, color2=None, color3=None, color4=None, color5=None, color6=None): """ Compute coordinates of 6 points relative to a center position. Point are numbered following this schema : Points in euclidiean grid: 6 5 1 . 4 2 3 Each color is applied to the side that link the vertex with same number to its following. Ex : color 1 is applied on side (vertex1, vertex2) Take care that tkinter ordinate axes is inverted to the standard euclidian ones. Point on the screen will be horizontally mirrored. Displayed points: 3 color3/ \color2 4 2 color4| |color1 5 1 color6\ /color6 6 """ size = self.hexaSize Δx = (size**2 - (size/2)**2)**0.5 point1 = (x+Δx, y+size/2) point2 = (x+Δx, y-size/2) point3 = (x , y-size ) point4 = (x-Δx, y-size/2) point5 = (x-Δx, y+size/2) point6 = (x , y+size )
I tried to comment my code correctly. If something seems unclear to you, please feel free to ask for an explanation.
Good luck Arthur Weiss.
NB: the script runs on python 3. The picture is a bit jagged. Improving your Tk canvas can add an anti-alias, as suggested at https://mail.python.org/pipermail/tkinter-discuss/2009-April/001904.html
source share