user2018473
user2018473

Reputation: 245

Tkinter text widget setting tabs

I have to set the tab size for my text widget to 4 characters. When I do textwidget.config(tabs = ("4c","8c")) I don't get the required results. It tabs by a lot which in now ways is 4 characters. Am I doing something wrong while setting the tabs property? Also, when I'm displaying the line and column number I do that by getting the index of the insert however here a tab character is marked as one character and not four which I would want it to be ideally. Is there a better way of getting the column number without having to deal with this problem of tabs?

Upvotes: 4

Views: 2845

Answers (1)

patthoyts
patthoyts

Reputation: 33223

The documentation for the Tk text widget -tabs option mentions that a distance of 2c will be 2 centimeters. The documentation doesn't seem to say but this uses the Tk_GetPixels function to convert your option values into distances and here it states the following types:

<none> : The number specifies a distance in pixels.
c : The number specifies a distance in centimeters on the screen.
i : The number specifies a distance in inches on the screen.
m : The number specifies a distance in millimeters on the screen.
p : The number specifies a distance in printer's points (1/72 inch) on the screen.

If you want to use a distance in characters then you should use font measure using the font defined for your text widget and a suitable character like m or n given that for proportional fonts characters have different widths. An example of this is given in the Tk text widget -tabs documentation.

Upvotes: 6

Related Questions