简体   繁体   English

Python:Tkinter / ttk主题消息框

[英]Python: Tkinter/ttk themed Message Box

I started making a GUI with Tkinter and I added the module tkMessageBox as well. 我开始使用Tkinter制作GUI,我也添加了模块tkMessageBox 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. 但最近我发现导入模块ttk会提供更多“最新”结果:按钮和文本框会显示当前操作系统的实际样式。 This is: Windows 10 buttons are plain and blue-lighted, and not those shaded gray blocky buttons from previous versions. 这是: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 ). 但不幸的是,我找不到在常用的Dialog Boxes(我从tkMessageBox导入的那些 )上使用这个ttk主题小部件的方法。 So OK/Cancel dialogs (for instance) still appear with a theme that doesn't belongs to Windows 10. 因此,确定/取消对话框(例如)仍然显示不属于Windows 10的主题。

All the documentation I check brings me to Tkinter . 我检查的所有文档都将我带到了Tkinter

The Tk messagebox on Windows is the system provided messagebox. Windows上的Tk消息框系统提供的消息框。 Tk does not create a messagebox using Tk buttons and frames but shows the stock dialog provided by the Windows API. Tk不使用Tk按钮和框架创建消息框,但显示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. 为了检查你的报告,我运行了Tcl / Tk 8.6,Python 3.4和Powershell,并让每个人都显示了一个消息框。 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看起来正如我们所期望的那样,但Python和Powershell都默认为旧式的非主题外观和感觉。

Windows 10上的Tk消息框

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. Tcl / Tk wish可执行文件包含一个清单资源,表明应用程序支持主题公共控件库。 The python.exe and pythonw.exe files have no such resources. python.exe和pythonw.exe文件没有这样的资源。 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. 必要的清单嵌入在tkinter使用的Tk86.dll文件中,但我认为这需要与可执行文件相关联。 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. 我通过使用Visual Studio打开pythonw.exe文件并将现有的RT_MANIFEST资源替换为从Tk86.dll中提取的资源来测试此假设。 With a suitable manifest in place the Python tkinter messagebox comes up properly themed: 使用合适的清单,Python tkinter消息框正确地为主题: Python 3.4使用嵌入式清单

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. 据推测,这个清单可以作为XML文件提供在与可执行文件相同的文件夹中,但在测试时似乎不起作用。 I had to actually embed the manifest in the executable resources. 我必须将清单实际嵌入可执行资源中。

See "Enabling Visual Styles" for more information on manifest resources. 有关清单资源的更多信息,请参阅“启用视觉样式”

An easier way to get properly themed dialog boxes is by compiling your python scripts into .exe files using pyinstaller . 获取正确主题对话框的更简单方法是使用pyinstaller将python脚本编译为.exe文件。 ( 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...) py2exe也可以工作,但我有一段时间没有使用该模块,如果我不需要,我不打算回去...)

Error message when running as a python script: 作为python脚本运行时出现错误消息:

错误消息 - 脚本

Error message when running as an .exe: 作为.exe运行时出现错误消息:

错误消息 -  .exe

I tried forcing a different theme with ttk.Style().theme_use('clam') , however the dialog boxes don't seem to conform to those themes. 我尝试使用ttk.Style().theme_use('clam')强制使用不同的主题,但是对话框似乎不符合这些主题。 It must default to the user's system's style. 它必须默认为用户的系统样式。 Still better than the windows 98 feel! 仍然比Windows 98感觉更好! for the record, I tested on a computer running windows 7 with Python 2.7. 为了记录,我在使用Python 2.7运行Windows 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. 看来你可以在pyinstaller中使用--manifest选项添加一个清单文件,但是看起来这不太适用,因为看起来pyinstaller会为你做这pyinstaller

If you need themed dialog boxes that prompt the user for input, consider using the ttk themed version of tkSimpleDialog called ttkSimpleDialog ( link ). 如果您需要提示用户输入的主题对话框,请考虑使用名为ttkSimpleDialoglink )的tkk主题版本的tkSimpleDialog Full Disclosure: I made the modifications to the original tkSimpleDialog module. 完全披露:我对原始的tkSimpleDialog模块进行了修改。

From the docs : 来自文档

Starting with Tk 8.5, the ttk module became available. 从Tk 8.5开始,ttk模块可用。 This module replaces much (but not all) of the original Tkinter machinery. 该模块取代了许多(但不是全部)原始Tkinter机器。 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. 在Tk 8.5之前的版本中,关于Tk应用程序的最常见的抱怨之一是它们不符合各种平台的风格。

  • 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. ttk模块允许您以通用方式编写应用程序,但您的应用程序可能看起来像Windows下的Windows应用程序,如MacOS下的MacOS应用程序,依此类推,无需对程序进行任何更改。

  • Each possible different appearance is represented by a named ttk theme. 每个可能的不同外观由命名的ttk主题表示。 For example, the classic theme gives you the appearance of the original Tkinter widgets described in the previous sections. 例如,经典主题为您提供了前面部分中描述的原始Tkinter小部件的外观。

ttk uses the system styles to represent something. ttk使用系统样式来表示某些东西。 Tkinter does not. Tkinter没有。 It is its own style. 它自己的风格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM