Python: Tkinter/ttk themed Message Box

后端 未结 3 2025
情歌与酒
情歌与酒 2021-01-02 07:22

I started making a GUI with Tkinter and I added the module tkMessageBox as well. But recently I discovered that importing the module

3条回答
  •  囚心锁ツ
    2021-01-02 07:32

    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.

    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:

    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.

提交回复
热议问题