Python:Tkinter/ttk主题消息框 [英] Python: Tkinter/ttk themed Message Box

查看:263
本文介绍了Python:Tkinter/ttk主题消息框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用 Tkinter 制作GUI,并且还添加了模块 tkMessageBox .但是最近我发现导入模块 ttk 会得到更多的最新"结果:按钮和文本框与当前操作系统的实际样式一起出现.这是:Windows 10按钮是纯蓝色的,而不是以前版本中那些阴影的灰色块状按钮.

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.

但是不幸的是,我找不到在通用对话框(我从 tkMessageBox 导入的对话框)中使用此 ttk 主题小部件的方法.因此,确定/取消"对话框(例如)仍会以不属于Windows 10的主题显示.

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.

我检查的所有文档都将我带到 Tkinter .

All the documentation I check brings me to Tkinter.

推荐答案

Windows 上的Tk消息框是系统提供的消息框. Tk不会使用Tk按钮和框架创建消息框,而是显示Windows API提供的股票对话框.

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.

要在此处查看您的报告,我运行了Tcl/Tk 8.6,Python 3.4和Powershell,并分别显示了一个消息框.如您所见,Tcl/Tk看起来像我们期望的那样,但是Python和Powershell都默认使用旧风格的非主题外观.

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.

最可能的原因与启动此文件的可执行文件中的嵌入式清单有关. Tcl/Tk wish可执行文件包含一个清单资源,该清单资源表明应用程序支持主题公用控件库. python.exe和pythonw.exe文件没有此类资源.必要的清单嵌入在tkinter使用的Tk86.dll文件中,但是我认为这需要与可执行文件相关联.我通过使用Visual Studio打开pythonw.exe文件并将现有的RT_MANIFEST资源替换为从Tk86.dll中提取的资源来测试了这一假设.使用适当的清单来正确设置主题的Python tkinter消息框:

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:

假定此清单可以作为XML文件提供在与可执行文件相同的文件夹中,但在测试中似乎无效.我实际上必须将清单嵌入可执行文件资源中.

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.

这篇关于Python:Tkinter/ttk主题消息框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆