在打开文件的cxFreeze编译的python可执行文件中打开自定义文件类型

问题描述 投票:2回答:1

我正在使用cx_Freeze在python中编译Rubiks Cube模拟器;它使用tkinter。

我希望用户能够将您在中心看到的2d表示的布局保存到.cube文件中,并能够从程序本身打开以前的.cube文件。

但是,我还希望用户能够从资源管理器中打开.cube文件,并让程序启动显示用户打开的.cube文件的内容。

做过一些研究后,我想我需要访问“运行时环境”或其他东西 - 但我绝对不知道。

python windows tkinter cx-freeze rubiks-cube
1个回答
1
投票

UPDATE

我用argparse模块解决了这个问题。基于以下事实:每次explorer打开一个文件时,它都会使用文件目录的参数调用应用程序,我所要做的就是添加一个可选参数来捕获这些数据。

import argparse
parser=argparse.ArgumentParser()
parser.add_argument("cubefile",nargs="?",default=False)
#'nargs="?"' makes the argument optional
#-meaning an error will not be thrown if no file is parsed on execution
args=parser.parse_args()
if args.universefile != False:
    init_defaultcube = cubetools.getCubeFromCubeFileDir(args.universefile)
    #cubetools is my class and getCubeFromCubeFileDir just interprets the text in the file

但是,因为这个参数改变了exe的工作目录,并且我的GUI图像引用是相对的,所以我不得不重置当前目录 os.chdir(os.path.dirname(os.path.abspath(sys.executable))) 我现在正在修改初始化时的注册表,以设置.cube文件的默认应用程序和图标。

© www.soinside.com 2019 - 2024. All rights reserved.