det*_*tly 11 python pygtk py2exe
我正在使用Windows XP上的一体化安装程序中的 Python 2.6和PyGTK 2.22.6 ,尝试为我的应用程序构建单文件可执行文件(通过py2exe).
我的问题是,当我将我的应用程序作为脚本运行时(即,没有内置到.exe文件中,就像一个松散的.py文件集合),它使用看起来像本机的Windows主题,但是当我运行构建的exe时,我看到默认GTK主题.
我知道可以通过将一堆文件复制到distpy2exe创建的目录中来解决这个问题,但我读过的所有内容都涉及手动复制数据,而我希望这是构建过程的自动部分.此外,关于该主题的所有内容(包括FAQ)都已过时 - PyGTK现在保留其文件C:\Python2x\Lib\site-packages\gtk-2.0\runtime\...,只是复制lib和etc目录不能解决问题.
我的问题是:
我希望能够以编程方式找到GTK运行时数据,setup.py而不是硬编码路径.我该怎么做呢?
我需要包含哪些最小资源?
更新:我可能通过反复试验几乎回答了#2.要使"wimp"(即MS Windows)主题起作用,我需要以下文件:
runtime\lib\gtk-2.0\2.10.0\engines\libwimp.dll
runtime\etc\gtk-2.0\gtkrc
runtime\share\icons\*
runtime\share\themes\MS-Windows
Run Code Online (Sandbox Code Playgroud)
...没有runtime前缀,但具有相同的目录结构,直接坐在distpy2exe生成的目录中.但是,在什么地方2.10.0来的,因为gtk.gtk_version是(2,22,0)?
在这里回答我自己的问题,但如果有人知道更好,也可以随意回答.其中一些似乎非常脆弱(例如路径中的版本号),所以如果你知道更好的方法,请评论或编辑.
首先,我使用此代码实际找到GTK运行时的根.这非常特定于您安装运行时的方式,并且可能通过对常见位置的大量检查来改进:
#gtk file inclusion
import gtk
# The runtime dir is in the same directory as the module:
GTK_RUNTIME_DIR = os.path.join(
os.path.split(os.path.dirname(gtk.__file__))[0], "runtime")
assert os.path.exists(GTK_RUNTIME_DIR), "Cannot find GTK runtime data"
Run Code Online (Sandbox Code Playgroud)
这取决于(a)关注大小的大小,以及(b)应用程序部署的上下文.我的意思是,你是将它部署到整个世界,任何人都可以拥有任意的语言环境设置,还是仅供内部企业使用,你不需要翻译的股票字符串?
如果您想要Windows主题,您需要包括:
GTK_THEME_DEFAULT = os.path.join("share", "themes", "Default")
GTK_THEME_WINDOWS = os.path.join("share", "themes", "MS-Windows")
GTK_GTKRC_DIR = os.path.join("etc", "gtk-2.0")
GTK_GTKRC = "gtkrc"
GTK_WIMP_DIR = os.path.join("lib", "gtk-2.0", "2.10.0", "engines")
GTK_WIMP_DLL = "libwimp.dll"
Run Code Online (Sandbox Code Playgroud)
如果你想要Tango图标:
GTK_ICONS = os.path.join("share", "icons")
Run Code Online (Sandbox Code Playgroud)
还有本地化数据(我省略了,但你可能不想这样做):
GTK_LOCALE_DATA = os.path.join("share", "locale")
Run Code Online (Sandbox Code Playgroud)
首先,这是一个在给定点遍历文件系统树并生成适合该data_files选项的输出的函数 .
def generate_data_files(prefix, tree, file_filter=None):
"""
Walk the filesystem starting at "prefix" + "tree", producing a list of files
suitable for the data_files option to setup(). The prefix will be omitted
from the path given to setup(). For example, if you have
C:\Python26\Lib\site-packages\gtk-2.0\runtime\etc\...
...and you want your "dist\" dir to contain "etc\..." as a subdirectory,
invoke the function as
generate_data_files(
r"C:\Python26\Lib\site-packages\gtk-2.0\runtime",
r"etc")
If, instead, you want it to contain "runtime\etc\..." use:
generate_data_files(
r"C:\Python26\Lib\site-packages\gtk-2.0",
r"runtime\etc")
Empty directories are omitted.
file_filter(root, fl) is an optional function called with a containing
directory and filename of each file. If it returns False, the file is
omitted from the results.
"""
data_files = []
for root, dirs, files in os.walk(os.path.join(prefix, tree)):
to_dir = os.path.relpath(root, prefix)
if file_filter is not None:
file_iter = (fl for fl in files if file_filter(root, fl))
else:
file_iter = files
data_files.append((to_dir, [os.path.join(root, fl) for fl in file_iter]))
non_empties = [(to, fro) for (to, fro) in data_files if fro]
return non_empties
Run Code Online (Sandbox Code Playgroud)
所以现在你可以这样打电话setup():
setup(
# Other setup args here...
data_files = (
# Use the function above...
generate_data_files(GTK_RUNTIME_DIR, GTK_THEME_DEFAULT) +
generate_data_files(GTK_RUNTIME_DIR, GTK_THEME_WINDOWS) +
generate_data_files(GTK_RUNTIME_DIR, GTK_ICONS) +
# ...or include single files manually
[
(GTK_GTKRC_DIR, [
os.path.join(GTK_RUNTIME_DIR,
GTK_GTKRC_DIR,
GTK_GTKRC)
]),
(GTK_WIMP_DIR, [
os.path.join(
GTK_RUNTIME_DIR,
GTK_WIMP_DIR,
GTK_WIMP_DLL)
])
]
)
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1616 次 |
| 最近记录: |