[Tutor] Tkinter draw line segments

Phil phillor9 at gmail.com
Wed Mar 30 02:35:00 EDT 2022


The segment table contains the start and end points of lines to be drawn 
on the canvas.

         seg = {0: (20, 20, 40, 20),
                1: (44, 24, 44, 44),
                2: (44, 48, 44, 68),
                3: (20, 72, 40, 72),
                4: (16, 48, 16, 68),
                5: (16, 24, 16, 44),
                6: (20, 46, 40, 46),
               }

Say I want to draw segments 1 and 2.

         num = {1: (seg[1], seg[2])}

The following will draw all of the segments correctly.

         for i in range(7):
             self.canvas.create_line(seg[i], width=4, fill="green")

What I haven't sorted out is how to draw segments 1 and 2 without having 
two draw statements. The following does draw the two segments but 
without the break between them.

         self.canvas.create_line(num[1], width=4, fill="green")

What I'm trying to come up with is a table entry that tells the draw 
routine how many draw statements there are, one for each segment. No 
doubt I'm making this more complicated than it needs to be.

One way around this is to draw all of the segments in two colours. In 
the above case I would draw segments 1 and 2 green and the remainder 
black. That way I'd only need one draw statement in a for loop with a 
variable fill colour. This would work perfectly on a black background 
but it seems like an amateurish solution.

-- 
Regards,
Phil



More information about the Tutor mailing list