[Tutor] Tkinter draw line segments
Dennis Lee Bieber
wlfraed at ix.netcom.com
Wed Mar 30 11:45:17 EDT 2022
On Wed, 30 Mar 2022 17:35:00 +1100, Phil <phillor9 at gmail.com> declaimed the
following:
>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")
for s in seg:
self.canvas.create_line(s, ...)
No magic numbers (7), no need to create an index (though you've made it
a dictionary where the KEY is nothing more than what would be a list index
if you drop the x: notation and change { } to [ ].
>
>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")
Based upon Tkinter terminology, you are drawing TWO DISCONNECTED LINES
-- not "segments" (segments are the result of adjacent pairs of coordinates
provided in a single call). {Apparently Tkinter will unpack a tuple if
provided -- the documentation just uses a sequence of coordinate pairs, as
all options are keyword type} A line with two segments would look like
...create_line(20, 20, 40, 20, 44, 24, ...)
This will draw a segment from (20, 20) to (40, 20) and then continue on to
draw to (44, 24).
>
>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.
No dictionary to be seen <G>
lines = [ (20, 20, 40, 20),
(44, 24, 44, 44, 68, 72), #toss in a 2-segment line
(44, 48, 44, 68),
(20, 72, 40, 72),
(16, 48, 16, 68),
(16, 24, 16, 44),
(20, 46, 40, 46) ]
>Say I want to draw segments 1 and 2.
>
desired_lines = (1, 2)
for ln in desired_lines:
self.canvas.create_line(lines[ln], width=4, fill="green")
The lines don't have to be consecutive in "lines" (and "desired_lines
can be tuple, list, or other iterator object)
desired_lines = [1, 3, 4, 6]
for ln in desired_lines:
self.canvas.create_line(lines[ln], width=4, fill="green")
Or even in list order
desired_lines = (4, 0, 2, 6)
for ln in desired_lines:
self.canvas.create_line(lines[ln], width=4, fill="green")
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/
More information about the Tutor
mailing list