1

I know and have seen examples of highlighting just a word (such as "print"), but I was wondering if I was able to do something like:

[B]Bold Text[\B]

I would like for it to start making the text bold when it locates:

 [B] 

and stop making the text bold when it finds:

 [\B]

I know this is how it works on most forum sites, but I didn't know if this would work in Python. I remember seeing the expressions "wordstart" and "wordend" on the EffBot page for the Text widget, I thought this may be the way to do it, but I don't really know how to apply this to my code.

This will probably get marked as a copy/duplicate, but I haven't found anything answering how to highlight with something like:

[B]

EffBot page: http://effbot.org/tkinterbook/text.htm

Post Scriptum: I would also be able to highlight from the marker to the end the next space with something like:

www. or http://
PotatoBeenCrafted
  • 302
  • 1
  • 6
  • 14
  • Do you want the `[b]` to remain in the text, or do you want it to be removed when you add the highlighting? Also, are you aware that the text widget has the ability to find regions of text based on a regular expression? – Bryan Oakley Mar 13 '16 at 13:05
  • @BryanOakley I would like the `[B]` and `[\B]` to be removed from the text widget but not from the text file. No, no I wasn't. I haven't done too much messing around with it as I just use it as a means to store and write text. I'm developing quite a big program at the moment, and I've just gotten round to configuring the basic text set up I wrote in the first place. – PotatoBeenCrafted Mar 13 '16 at 14:34
  • You would change the font to a bold font for the text you want in bold. Do a search for tkFont to find the fonts available to Tkinter. –  Mar 13 '16 at 17:42
  • @CurlyJoe I know how to do that, but I'm more of making a program that allows the user to change the formatting of the text. I would like for when a button it clicked, it takes the selected text, say: "Bold Text", and it adds `[B]` at the start of the selected text and `[\B]` at the end of the selected text. So at the end, it looks like `[B]Bold Text[\B]`. After that, the program automatically checks to see if there is anything like `[B]` or `[\B]` in the text area and hides it, making the text between it bold. – PotatoBeenCrafted Mar 14 '16 at 03:13

1 Answers1

2

I haven't got the button in here, but here's how to find the codes, turn the text to bold, and hide the codes:

#!/usr/bin/env python3
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
root = Tk()
tx = Text(root, background='white', font='Courier 12')
tx.grid(column=0, row=0, sticky=(N, W, E, S))
ybar = ttk.Scrollbar(root, orient=VERTICAL, command=tx.yview)
ybar.grid(column=1, row=0, sticky=(N, W, E, S))
xbar = ttk.Scrollbar(root, orient=HORIZONTAL, command=tx.xview)
xbar.grid(column=0, row=1, columnspan=2, sticky=(N, W, E, S))
tx["yscrollcommand"] = ybar.set
tx["xscrollcommand"] = xbar.set
tx.tag_config('hide', elide=1)
tx.tag_config('bold', font='Courier 12 bold')
lastplace=tx.index('1.0')
def boldit():
    global tx, lastplace
    nextplace = tx.search('[B]', lastplace, 'end')
    if nextplace:
        boldon = nextplace + ' +3c'
        tx.tag_add('hide', nextplace, boldon)
        boldoff = tx.search('[/B]', boldon, 'end')
        if boldoff:
            tx.tag_add('bold', boldon, boldoff) 
            codoff = boldoff + ' +4c'
            tx.tag_add('hide', boldoff, codoff)
        lastplace = codoff
        boldit()
    else:
        return
tx.insert('1.0', """When, in the course of [B]human events,[/B] it becomes [B]necessary[/B] for one people to [B]dissolve[/B] the political bands ...""")
boldit()        
root.mainloop()

You might be able to do the same thing with a "while" loop and eschew global variables, but this is easy and it worked for me.

McClamrock
  • 857
  • 1
  • 8
  • 12
  • Thank you very much! This (for the most part) was exactly what I was looking for. I'll get right on adding this into my previous code. There are a few things I need to add, though, for instance: the ability to write in different codes and have them hide and make the text bold and have buttons you can press to add the codes in (I have the buttons set up already). – PotatoBeenCrafted Mar 14 '16 at 17:53
  • You're welcome! You might consider checking my answer off as "accepted" if you see fit. As for the buttons, if there will be ones to hide or show the codes, all they should need to do is toggle the 'hide' tag setting between `elide=0` and `elide=1`. :o) – McClamrock Mar 14 '16 at 21:08
  • With some careful editing and a little manipulation, I've managed to add this into my code for every type of formatting that the Tkinter Text widget allows, apart from fonts and colours, which I'm having a little trouble with. I've accepted this as an answer as it with some editing can be added into someone's code and work just fine. – PotatoBeenCrafted May 19 '16 at 04:52