Python: Tkinter/ttk themed Message Box

Xesa picture Xesa · Nov 18, 2015 · Viewed 7.8k times · Source

I started making a GUI with Tkinter and I added the module tkMessageBox as well. But recently I discovered that importing the module ttk gives more 'up-to-date' results: buttons and texts boxes appear with the actual style of the current OS. This is: Windows 10 buttons are plain and blue-lighted, and not those shaded gray blocky buttons from previous versions.

But unfortunately, I can't find a way to use this ttk themed widgets on the common Dialog Boxes (the ones which I imported from tkMessageBox). So OK/Cancel dialogs (for instance) still appear with a theme that doesn't belongs to Windows 10.

All the documentation I check brings me to Tkinter.

Answer

patthoyts picture patthoyts · Nov 19, 2015

The Tk messagebox on Windows is the system provided messagebox. Tk does not create a messagebox using Tk buttons and frames but shows the stock dialog provided by the Windows API.

To check your report here I ran up Tcl/Tk 8.6, Python 3.4 and Powershell and got each to show a messagebox. As you can see Tcl/Tk comes up looking as we would expect but both Python and Powershell are defaulting to the old style non-themed look and feel.

Tk messageboxes on Windows 10

The reason for this is most likely to do with embedded manifests in the executable that launches this. The Tcl/Tk wish executable contains a manifest resource that states the application supports the themed common controls library. The python.exe and pythonw.exe files have no such resources. The necessary manifest is embedded in the Tk86.dll file that is used by tkinter but I think this needs to be associated with the executable. I tested this hypothesis by opening the pythonw.exe file with Visual Studio and replacing the existing RT_MANIFEST resource with the one extracted from the Tk86.dll. With a suitable manifest in place the Python tkinter messagebox comes up properly themed: Python 3.4 using embedded manifest

Supposedly this manifest can be provided as an XML file in the same folder as the executable but in testing this that did not seem to work. I had to actually embed the manifest in the executable resources.

See "Enabling Visual Styles" for more information on manifest resources.