Hi all, is it possible to draw a horizontal line in the Text() widget, something like the <hr> in html, extending all over the widget. Vasilis
Hi, On Thu, 16 Jan 2020 13:14:08 +0000 Vasilis Vlachoudis <Vasilis.Vlachoudis@cern.ch> wrote:
Hi all,
is it possible to draw a horizontal line in the Text() widget, something like the <hr> in html, extending all over the widget.
I have done this before with an embedded Frame widget, however changes to the Text widget size have to be taken into account and the Frame widgets need to be resized accordingly, which makes things a bit complicated. Maybe there is a simpler way to do that, but I don't know any. I'll post the important parts of the code I used below. I hope this helps :) Michael ################################################################ def send_hor_rule(self, *args): xoff = int(self.text['highlightthickness']) + \ int(self.text['bd']) + int(self.text['padx']) + 15 # 15 px. extra padding w = self.text.winfo_width() - 2 * xoff f = tkinter.Frame(self.text, height=4, highlightthickness=0, width=w, relief='ridge', bd=2, bg='gray60') i = self.text.index('insert') self.text.window_create('insert', window=f, align='center') self.text.horRules.append((i, f)) def _configureHorRules(self, *event): # callback for Configure events if self.text.horRules: # _getMaxLineSize() is slow, but calling it on every Configure # event seems to be the only way to make this work reliably maxlinesize = self._getMaxLineSize() w = self.text.winfo_width() xoff = int(self.text['highlightthickness']) + \ int(self.text['bd']) + int(self.text['padx']) w_vis = w - 2 * xoff m = max((w_vis, maxlinesize)) for (i, w) in self.text.horRules: w.configure(width=m) return def _getMaxLineSize(self, *event): # calculate the max. required size of a line to use as size # for the horizontal rules numlines = self.text.count('1.0', 'end', 'lines') or 0 xpixmax = 0 if numlines: numlines = numlines[0] horRuleLines = [ int(i.split('.')[0]) for (i, w) in self.text.horRules] for i in range(1, numlines + 1): if i in horRuleLines: # if a line with a horRule is the biggest, the hor. rule # might get bigger with each reconfiguring, so we must # not take these lines into account continue xpix = self.text.count( '%d.0' % i, '%d.0 lineend' % i, 'xpixels') if xpix and xpix[0] > xpixmax: xpixmax = xpix[0] return xpixmax ############################################################### .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. The sooner our happiness together begins, the longer it will last. -- Miramanee, "The Paradise Syndrome", stardate 4842.6
Thanks Michael, I will give it a try Vasilis ________________________________________ From: Tkinter-discuss [tkinter-discuss-bounces+vasilis.vlachoudis=cern.ch@python.org] on behalf of Michael Lange [klappnase@web.de] Sent: Thursday, January 16, 2020 17:56 To: tkinter-discuss@python.org Subject: Re: [Tkinter-discuss] horizontal line in Text Hi, On Thu, 16 Jan 2020 13:14:08 +0000 Vasilis Vlachoudis <Vasilis.Vlachoudis@cern.ch> wrote:
Hi all,
is it possible to draw a horizontal line in the Text() widget, something like the <hr> in html, extending all over the widget.
I have done this before with an embedded Frame widget, however changes to the Text widget size have to be taken into account and the Frame widgets need to be resized accordingly, which makes things a bit complicated. Maybe there is a simpler way to do that, but I don't know any. I'll post the important parts of the code I used below. I hope this helps :) Michael ################################################################ def send_hor_rule(self, *args): xoff = int(self.text['highlightthickness']) + \ int(self.text['bd']) + int(self.text['padx']) + 15 # 15 px. extra padding w = self.text.winfo_width() - 2 * xoff f = tkinter.Frame(self.text, height=4, highlightthickness=0, width=w, relief='ridge', bd=2, bg='gray60') i = self.text.index('insert') self.text.window_create('insert', window=f, align='center') self.text.horRules.append((i, f)) def _configureHorRules(self, *event): # callback for Configure events if self.text.horRules: # _getMaxLineSize() is slow, but calling it on every Configure # event seems to be the only way to make this work reliably maxlinesize = self._getMaxLineSize() w = self.text.winfo_width() xoff = int(self.text['highlightthickness']) + \ int(self.text['bd']) + int(self.text['padx']) w_vis = w - 2 * xoff m = max((w_vis, maxlinesize)) for (i, w) in self.text.horRules: w.configure(width=m) return def _getMaxLineSize(self, *event): # calculate the max. required size of a line to use as size # for the horizontal rules numlines = self.text.count('1.0', 'end', 'lines') or 0 xpixmax = 0 if numlines: numlines = numlines[0] horRuleLines = [ int(i.split('.')[0]) for (i, w) in self.text.horRules] for i in range(1, numlines + 1): if i in horRuleLines: # if a line with a horRule is the biggest, the hor. rule # might get bigger with each reconfiguring, so we must # not take these lines into account continue xpix = self.text.count( '%d.0' % i, '%d.0 lineend' % i, 'xpixels') if xpix and xpix[0] > xpixmax: xpixmax = xpix[0] return xpixmax ############################################################### .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. The sooner our happiness together begins, the longer it will last. -- Miramanee, "The Paradise Syndrome", stardate 4842.6 _______________________________________________ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss
You can try using bgstipple attribute of a tag (https://www.tcl.tk/man/tcl8.6/TkCmd/text.htm#M45): text.tag_configure(tag_name, bgstipple="@path/to/a/bitmap/containing/a/line.xbm") but unfortunately it doesn't work on macOS (https://wiki.tcl-lang.org/page/Tk+differences+on+Mac+OS+X) Best regards, Aivar On 16.01.2020 15:14, Vasilis Vlachoudis wrote:
Hi all,
is it possible to draw a horizontal line in the Text() widget, something like the <hr> in html, extending all over the widget.
Vasilis
_______________________________________________ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss
On 16/01/2020 13:14, Vasilis Vlachoudis wrote:
Hi all,
is it possible to draw a horizontal line in the Text() widget, something like the <hr> in html, extending all over the widget.
Would a line of spaces with the underline turned on in the font work? If you use a monospace font the length should be fairly predictable. Just a thought. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos
Hi, On Fri, 24 Jan 2020 00:18:20 +0000 Alan Gauld via Tkinter-discuss <tkinter-discuss@python.org> wrote:
On 16/01/2020 13:14, Vasilis Vlachoudis wrote:
Hi all,
is it possible to draw a horizontal line in the Text() widget, something like the <hr> in html, extending all over the widget.
Would a line of spaces with the underline turned on in the font work? If you use a monospace font the length should be fairly predictable.
Just a thought.
I think the problem with all text based solutions is that these "lines" will not resize along with the text widget and worse, might line-break when the widget size is being reduced. Regards Michael .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. The people of Gideon have always believed that life is sacred. That the love of life is the greatest gift ... We are incapable of destroying or interfering with the creation of that which we love so deeply -- life in every form from fetus to developed being. -- Hodin of Gideon, "The Mark of Gideon", stardate 5423.4
On 24/01/2020 09:15, Michael Lange wrote:
is it possible to draw a horizontal line in the Text() widget, something like the <hr> in html, extending all over the widget.
Would a line of spaces with the underline turned on in the font work? If you use a monospace font the length should be fairly predictable.
Just a thought.
I think the problem with all text based solutions is that these "lines" will not resize along with the text widget and worse, might line-break when the widget size is being reduced.
Yeah, after I posted I had a bit of a think and tried a few things out. I came to the conclusion you really need a widget class that would handle all those kinds of things. The humble <hr> tag has quite a lot of functionality when you start digging deeper. (size by absolute or percent, styles, thickness, etc) But if its just a divider in a text field, underline of overstrike might work as a casual fix. You could handle resize events if needed, but not much more. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos
participants (4)
-
Aivar Annamaa -
Alan Gauld -
Michael Lange -
Vasilis Vlachoudis