14

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
Xesa
  • 153
  • 1
  • 1
  • 5

3 Answers3

11

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
patthoyts
  • 32,320
  • 3
  • 62
  • 93
  • Thanks for you answer, this really explains it all. I'll research a bit more from that starting point, but I'm afraid that tweaking those files from Visual Studio could end up with uncompatibilities with Unix systems. – Xesa Nov 19 '15 at 15:04
  • Python exe's and manifest files have nothing to do with unix. This is just a note on the program that lets windows know it can enable the themed visual styles API for the common controls library. – patthoyts Nov 19 '15 at 16:24
  • 1
    A reader of your answer opened an issue on the CPython tracker `https://bugs.python.org/issue27309`. I am not the main tkinter maintainer, but I would like to see it upgraded to make themed versions of the builtin dialog boxes available. If you wish to add more information there, or sign the contributor agreement and contribute a patch at least for Windows, please feel free. – Terry Jan Reedy Jun 17 '16 at 23:28
4

An easier way to get properly themed dialog boxes is by compiling your python scripts into .exe files using pyinstaller. (py2exe might also work, but I haven't used that module in a while and don't plan on going back if I don't have to...)

Error messages when running as a python script and .exe are the same:

error message - script

I tried forcing a different theme with ttk.Style().theme_use('clam'), however the dialog boxes don't seem to conform to those themes. It must default to the user's system's style. Still better than the windows 98 feel! for the record, I tested on a computer running windows 7 with Python 2.7.

EDIT

It appears you can add a manifest file using the --manifest option in pyinstaller, however it appears that this isn't very applicable anyways since it seems pyinstaller does that for you.

If you need themed dialog boxes that prompt the user for input, consider using the ttk themed version of tkSimpleDialog called ttkSimpleDialog (link). Full Disclosure: I made the modifications to the original tkSimpleDialog module.

Julia Meshcheryakova
  • 3,162
  • 3
  • 22
  • 42
TylerH
  • 132
  • 7
2

From the docs:

Starting with Tk 8.5, the ttk module became available. This module replaces much (but not all) of the original Tkinter machinery. Use this module to gain these advantages:
  • Platform-specific appearance. In releases before Tk 8.5, one of the commonest complaints about Tk applications was that they did not conform to the style of the various platforms.

  • The ttk module allows you to write your application in a generic way, yet your application can look like a Windows application under Windows, like a MacOS app under MacOS, and so on, without any change to your program.

  • Each possible different appearance is represented by a named ttk theme. For example, the classic theme gives you the appearance of the original Tkinter widgets described in the previous sections.

ttk uses the system styles to represent something. Tkinter does not. It is its own style.

R4PH43L
  • 2,122
  • 3
  • 18
  • 30
  • 1
    This affects Tk widgets like buttons and edit fields but the messagebox and file open, file save dialogs have always been provided by the system implementations on Windows. On Unix Tk has to create it's own version. On MacOSX Tk calls up system standard dialogs where possible. – patthoyts Nov 19 '15 at 12:03