0

I've implemented a Tkinter Notebook with multiple tabs, each of which contains the context of some text file. If the user changes the text, I would like to change the appearance of the tab so the user can see which tabs contain unsaved changes. For instance, in Nodepad++, there is a diskette icon changing its color.

Is there a way of changing the appearance of the tabs in the Notebook? I know there is a possibility to define a style at the creation time of the tab. But the examples I found use styles depending on pre-defined states of the tabs like "disabled", "pressed", "active", etc. I tried user-defined state, e.g. "text_inside_tab_changed", but I get the error _tkinter.TclError: Invalid state name.

Here is a minimal example:

# Import the required libraries
from tkinter import *
from tkinter import ttk

text = None
nb = None

def on_key(event):
    # text["state"]="tab_content_changed" <-- does not work
    # nb.tab(text, image=PhotoImage("floppy-disk.png")) <-- my png image does not appear
    # how can I change the appearance of the tab here?
    print(nb.tab(text))
    pass

# Create an instance of tkinter frame
win = Tk()

# Set the size of the tkinter window
win.geometry("700x350")

# Create a Notebook widget
nb = ttk.Notebook(win)

# Add a text frame
text = Text(nb, width=400, height=180)
text.bind_all('<Key>', on_key)


nb.add(text, text='Text File', compound='left')


nb.pack(fill=BOTH, padx=5, pady=5)


win.mainloop()

Sorry, I have no idea how to add/change images in the tab.

apio
  • 154
  • 11
  • 1
    Can you please show a minimal working example of what you tried? – TheLizzard Jul 15 '21 at 17:52
  • A minimal example would be too long because of the text file handling, etc. My reference is [this example](https://stackoverflow.com/questions/39458337/is-there-a-way-to-add-close-buttons-to-tabs-in-tkinter-ttk-notebook) by Bryan Oakley. Basically, I would like to change the appearance of any of the selected tabs by a user action like typing a text into a text frame being a child of a tab. – apio Jul 15 '21 at 18:01
  • 1
    @apio: I don't see why the file handling would make the example too long. All you need is a button that simulates that the text changed. Click it to toggle the value from changed to unchanged. – Bryan Oakley Jul 15 '21 at 18:04
  • 1
    To make a minimal example you can just put a label or a text widget inside the notebook without adding the saving/opening/... A minimal example is an example where if you remove any part of it, the problem will disappear. – TheLizzard Jul 15 '21 at 18:04

0 Answers0