Hi,

On Thu, 16 Jan 2020 13:14:08 +0000
Vasilis Vlachoudis <vasilis.vlachou...@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

Reply via email to