This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Tkinter's canvas' dashed lines have incorrect segment lengths
Type: Stage: resolved
Components: Tkinter, Windows Versions: Python 3.5, Python 2.7
process
Status: closed Resolution: third party
Dependencies: Superseder:
Assigned To: Nosy List: kms70847, paul.moore, serhiy.storchaka, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2016-05-19 15:12 by kms70847, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
output.PNG kms70847, 2016-05-19 15:12 screenshot of incorrect output
canvas_dash.tcl serhiy.storchaka, 2016-05-19 15:59
Messages (5)
msg265868 - (view) Author: Kevin (kms70847) * Date: 2016-05-19 15:12
When creating a dashed line using `Canvas.create_line`. Minimal reproducing example:

    from Tkinter import *
    root = Tk()
    canvas = Canvas(root, width=100, height=30, bg="black")
    canvas.pack()
    canvas.create_line((0,10,100,10), dash=(20,), fill="red")
    canvas.create_line((0,20,100,20), dash=(20,20), fill="green")
    root.mainloop()

Expected result: each line segment should be 20 pixels wide, separated by gaps 20 pixels wide.

Actual result: each line segment is 18 pixels wide, separated by gaps 6 pixels wide. See attached file for screenshot.

Additional information: this problem appears to only occur on Windows. The Stack Overflow Python chat room attempted to replicate this issue, starting around here: http://chat.stackoverflow.com/transcript/message/30645798#30645798. Users of Windows 7, 8, and 10 were able to replicate the incorrect segmenting behavior. Users of Linux had correct segment lengths. (some Windows & 2.7 users also noticed that their green line was 3 pixel segments with 3 pixel gaps, but this seems to be an independent bug which was already fixed somewhere between 2.7.2 and 2.7.10.)
msg265871 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-05-19 15:59
Could you please test pure Tcl/Tk example?
msg265876 - (view) Author: Kevin (kms70847) * Date: 2016-05-19 16:39
Ok, I've tried testing Serhiy's `canvas_dash.tcl` file, by running:

    import Tkinter
    root = Tkinter.Tk()
    root.tk.eval("source canvas_dash.tcl")
    root.mainloop()

(I'm not sure if this counts as "pure" Tcl/Tk. I don't have any experience in using Tcl/Tk outside of Python. If there's a way to execute `canvas_dash.tcl` straight from the command line, I'm willing to try, but after some cursory research I couldn't figure out how to do it.)

The result is the same as before: 18 pixel segments, 6 pixel gaps.
msg265877 - (view) Author: Kevin (kms70847) * Date: 2016-05-19 17:23
This problem appears to go deeper than Python. On the TK bug tracker, under the issue "-dashofset option doesnt work on ms windows build" (https://core.tcl.tk/tk/tktview?name=1055974fff), a commenter mentions:

 > On Windows, only certain dash patterns and no dash offsets are supported.

Which implies that you can't create segments and gaps of arbitrary length ratios- you can only select from a limited collection of premade patterns. This comment from tkWinDraw.c (https://github.com/tcltk/tk/blob/master/win/tkWinDraw.c#L1188) supports this:

 > Below is a simple translation of serveral dash patterns to valid  windows pen types. Far from complete, but I don't know how to do it better.

The code goes on to invoke CreatePen (https://msdn.microsoft.com/en-us/library/windows/desktop/dd183509%28v=vs.85%29.aspx), which only allows for four different variations of dashed line styles.

Conclusions:
1) It isn't Python's fault.
2) The guy who wrote the Tk code was aware of the limitation at the time, so it's arguably not a bug.

So, this issue ought to be closed; not much we can do, except perhaps petition the popular Tkinter reference websites to put up a warning label on their descriptions of the `dash` keyword argument.
msg265880 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-05-19 18:00
"Not our fault. Try reporting this to the Tcl team" ;-)

In any case thank you for your report Kevin.
History
Date User Action Args
2022-04-11 14:58:31adminsetgithub: 71245
2016-05-19 18:00:48serhiy.storchakasetresolution: not a bug -> third party
messages: + msg265880
stage: resolved
2016-05-19 17:23:02kms70847setstatus: open -> closed
resolution: not a bug
messages: + msg265877
2016-05-19 16:39:54kms70847setmessages: + msg265876
2016-05-19 15:59:15serhiy.storchakasetfiles: + canvas_dash.tcl
nosy: + serhiy.storchaka
messages: + msg265871

2016-05-19 15:12:58kms70847create