tkinter tag_config does not work
<p>I am building a notepad like application in tkinter-python. There is an option to change the font of the text writen in the text field of the application.</p>

<p>I have created a Font Chooser popup screen to be called from main window on clicking 'font' menu, which basically creates a FontChooser class object and passes to the main window, which sets the font in man window. </p>

<p>A sample of the code where font is getting set in main window is,</p>

<pre><code>root = Tix.Tk(className="Notepad")
notepad = ScrolledText(root, width=100, height=100)

def open_font():
    font = MyFont.askChooseFont(root)
    notepad.tag_add("bt", "sel.first", "sel.last")
    notepad.tag_config("bt", font=font.getFontTuple())
</code></pre>

<p>Now when I first run the application and select a portion of text and change the font, it works correctly. But after that, whatever portion of text I am selecting and changing the font, it is ignoring the selection and applying the font on the whole text. Can anyone let me know what is the problem here? </p>

<p>IDLE uses tag_config to syntax color python code and it works on all Python versions and major OSes for the last 15 years.</p>

<p>To have some idea of why it seems to fail for you, you need to find an <a href="https://stackoverflow.com/help/mcve">MCVE</a> that fails.  Start without tix and scrollbars.  (Tix is deprecated in 3.6 and bugs are not being fixed.) Also notice that your code uses the same tag for each selection, so that when you change the configuration, it applies to all previous selections.</p>

<p>Here is simplified code that works as intended and expected.</p>

<pre><code>import tkinter as tk
import time
root = tk.Tk()
text = tk.Text(root)
text.pack()
text.insert('1.0', "line 1\nline 2\nline 3\n")
text.tag_add('bg', '1.0', '1.4')
text.tag_config('bg', background='red')
root.update()
time.sleep(1)
text.tag_add('bg', '2.0', '2.4')
text.tag_config('bg', background='blue')
root.update()
</code></pre>

<p>You could try modifying it step by step until it either reproduces your problem or does what you want.</p>

<p>EDIT with example modification: use 'sel.first' and 'sel.last' instead of hard-coded indexes.</p>

<pre><code>import tkinter as tk
import time
root = tk.Tk()
text = tk.Text(root)
text.pack()
text.insert('1.0', "line 1\nline 2\nline 3\n")
root.update()  # make text visible for selection
input('select some text')
text.tag_add('bg', 'sel.first', 'sel.last')
text.tag_config('bg', background='red')
root.update()  # make change visible
input('select some text')
text.tag_add('bg', 'sel.first', 'sel.last')
text.tag_config('bg', background='blue')
root.update()  # make 2nd change visible
input('look at result')
</code></pre>

<p>Run in console.  Move tk window so console and GUI are both visible. Make selection as prompted.  Click on console* and hit return to allow input statement to return.  Repeat.  The result for me is that both selections, but not everything, turns blue.  I suggest changing font instead of bg color for the next experiment.</p>

<ul>
<li>On Windows, the selection highlighting in the tk windows disappears when one clicks on the console because Windows only allows visible selection in one window at a time.  However, the select markers are still present in the text widget so that tag_add still works.</li>
</ul>

python tkinter