Search code examples
pythontkinterpython-idle

Adding a vertical bar or other marker to tkinter Text widgets at a particular column


My intention is to add a vertical bar to IDLE to indicate preferred line length at column 80.

I have tried to find a configuration option for the Text tkinter widget that would allow this but have found nothing.

I was hoping it would be a simple configuration option so I could just add a another item the text_options dictionary within EditorWindow.py found within Python\Lib\idlelib.

I am not sure how styles/themes work but do they have the capability to change the background colour of only 1 column in a Text widget?


Solution

  • Outline of possible solution:

    • Create a 1-pixel wide Frame, with a contrasting background color, as a child of the Text.
    • Use .place() to position the Frame at an appropriate horizontal coordinate.

    Possible issues:

    • I don't see any easy way to get the coordinate for a particular column. Text.bbox("1.80") looks promising, but doesn't work unless there actually are 80 characters in the line already. You may have to insert a dummy 80-character line at first, call update_idletasks to get the Text to calculate positions, call bbox to get the coordinates, then delete the dummy text. Repeat whenever the display font is changed.
    • The line would necessarily appear on top of any text or selection region, which isn't quite the visual appearance I'd expect for a feature like this.