Search code examples
pythontkintertk-toolkitttk

Can a ttk style option be deleted?


How can I delete a ttk style? I am unittesting code which includes ttk styles and each needs to be deleted in the test fixture's tear down method otherwise the style setting made in test 1 affects the results of test 2. In the following simplified example I set a style for column 1.

import tkinter.ttk as ttk

style = ttk.Style()
style.configure('1.TEntry', **{'foreground': 'maroon'})
style.configure('1.TEntry', **{'foreground': ''})

As far as I can see from Shipman and the ttk man page the only possibility is clearing the option value ('maroon' -> '') shown in the final line of the above code.

Is it possible to delete the option 'foreground' from '1.TEntry'? Better, is it possible to completely remove '1.TEntry'?


Solution

  • This was never implemented. In Tcl/Tk this kind of test can be created using slave interpreters and the entire test environment discarded. In Python I think the nearest equivalent is to create a thread, load Tk in that thread and destroy it following the test. That should isolate the various Tk instances. In ttk once we create a layout or style option there is no method in Tcl to remove the items from the table holding them. See the Tcl ttk man page for the final word but you can see no ttk::style layout delete or equivalent command.