Python 3.x TKinter打开文件对话框是否有其他选项?

Python 3.x TKinter打开文件对话框是否有其他选项?,python-3.x,tkinter,pygame,Python 3.x,Tkinter,Pygame,我正在使用pygame制作一个小型2d游戏引擎,我想打开文件对话框。我能找到的唯一选项是tkinter,当我运行此代码时 root = Tk() *code here* filedialog.askopenfile(initialdir="/", title="Select A File", filetypes=(

我正在使用pygame制作一个小型2d游戏引擎,我想打开文件对话框。我能找到的唯一选项是tkinter,当我运行此代码时

root = Tk()
*code here*
filedialog.askopenfile(initialdir="/", title="Select A File",
                                                      filetypes=(
                                                          ("Png Files", "*.png"), ("Jpg Files", "*.jpg"))).name
*code here*
root.mainloop()
除了另一个白色GUI正在打开之外,其他一切都正常工作,因此我删除了
mainloop()
root=Tk()
行,但它给了我一个错误

错误是:

pygame 2.0.0.dev8 (SDL 2.0.12, python 3.8.5)
Hello from the pygame community. https://www.pygame.org/contribute.html
2020-11-27 22:04:14.676 Python[3271:272456] -[SDLApplication _setup:]: unrecognized selector sent to instance 0x7f9ee3438be0
2020-11-27 22:04:14.679 Python[3271:272456] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SDLApplication _setup:]: unrecognized selector sent to instance 0x7f9ee3438be0'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff204b66af __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00007fff201ee3c9 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff20538c85 -[NSObject(NSObject) __retain_OA] + 0
    3   CoreFoundation                      0x00007fff2041e06d ___forwarding___ + 1467
    4   CoreFoundation                      0x00007fff2041da28 _CF_forwarding_prep_0 + 120
    5   libtk8.6.dylib                      0x000000010adb21f2 TkpInit + 408
    6   libtk8.6.dylib                      0x000000010ad21aac Initialize + 2454
    7   _tkinter.cpython-38-darwin.so       0x000000010ab75714 Tcl_AppInit + 84
    8   _tkinter.cpython-38-darwin.so       0x000000010ab75032 _tkinter_create + 1362
    9   Python                              0x0000000109dc890f cfunction_vectorcall_FASTCALL + 175
    10  Python                              0x0000000109e5a93c call_function + 444
    11  Python                              0x0000000109e5779e _PyEval_EvalFrameDefault + 25678
    12  Python                              0x0000000109e5b784 _PyEval_EvalCodeWithName + 2804
    13  Python                              0x0000000109d8cace _PyFunction_Vectorcall + 270
    14  Python                              0x0000000109d8be37 _PyObject_FastCallDict + 247
    15  Python                              0x0000000109d8d43f _PyObject_Call_Prepend + 143
    16  Python                              0x0000000109de3631 slot_tp_init + 145
    17  Python                              0x0000000109ddea79 type_call + 297
    18  Python                              0x0000000109d8bfe5 _PyObject_MakeTpCall + 373
    19  Python                              0x0000000109e5a995 call_function + 533
    20  Python                              0x0000000109e57835 _PyEval_EvalFrameDefault + 25829
    21  Python                              0x0000000109d8c930 function_code_fastcall + 128
    22  Python                              0x0000000109e5a93c call_function + 444
    23  Python                              0x0000000109e5779e _PyEval_EvalFrameDefault + 25678
    24  Python                              0x0000000109e5b784 _PyEval_EvalCodeWithName + 2804
    25  Python                              0x0000000109d8cace _PyFunction_Vectorcall + 270
    26  Python                              0x0000000109e5a93c call_function + 444
    27  Python                              0x0000000109e5779e _PyEval_EvalFrameDefault + 25678
    28  Python                              0x0000000109e5b784 _PyEval_EvalCodeWithName + 2804
    29  Python                              0x0000000109d8cace _PyFunction_Vectorcall + 270
    30  Python                              0x0000000109d8be37 _PyObject_FastCallDict + 247
    31  Python                              0x0000000109d8d43f _PyObject_Call_Prepend + 143
    32  Python                              0x0000000109de3631 slot_tp_init + 145
    33  Python                              0x0000000109ddea79 type_call + 297
    34  Python                              0x0000000109d8bfe5 _PyObject_MakeTpCall + 373
    35  Python                              0x0000000109e5a995 call_function + 533
    36  Python                              0x0000000109e57835 _PyEval_EvalFrameDefault + 25829
    37  Python                              0x0000000109e5b784 _PyEval_EvalCodeWithName + 2804
    38  Python                              0x0000000109d8cace _PyFunction_Vectorcall + 270
    39  Python                              0x0000000109e5a93c call_function + 444
    40  Python                              0x0000000109e5777a _PyEval_EvalFrameDefault + 25642
    41  Python                              0x0000000109e5b784 _PyEval_EvalCodeWithName + 2804
    42  Python                              0x0000000109d8cace _PyFunction_Vectorcall + 270
    43  Python                              0x0000000109e5a93c call_function + 444
    44  Python                              0x0000000109e578c8 _PyEval_EvalFrameDefault + 25976
    45  Python                              0x0000000109e5b784 _PyEval_EvalCodeWithName + 2804
    46  Python                              0x0000000109e51274 PyEval_EvalCode + 100
    47  Python                              0x0000000109ea0ee0 PyRun_FileExFlags + 336
    48  Python                              0x0000000109ea05d0 PyRun_SimpleFileExFlags + 864
    49  Python                              0x0000000109ebdae3 Py_RunMain + 2179
    50  Python                              0x0000000109ebde3f pymain_main + 223
    51  Python                              0x0000000109ebe03b Py_BytesMain + 43
    52  libdyld.dylib                       0x00007fff2035f631 start + 1
    53  ???                                 0x0000000000000002 0x0 + 2
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
谢谢大家!

(如果这个问题有任何问题,强烈建议在评论中输入,这样我可以编辑这个问题,使它更方便用户使用)

Python有打开文件的能力,它实际上只是
file=open('filename.ext')
但是pygame不为您进行文件系统遍历

因此,您需要找到一个可以打印目录内容的库,并允许您从该列表中进行选择,或者进行选择。EasyGUI简化了这一过程,因为你不必寻找解决方案,只要它就在那里


在不涉及pygame的情况下运行这段代码,这样您就可以看到它的功能了。如果它给你一个对话框,并返回文件,那么你就可以开始了。只需从该点初始化pygame,然后从该点开始。

是否导入pygame?如果是这样的话,你是否在代码的其他地方使用它?是的,我只是把这部分用在了基础上,我做得很好。奇怪。。。这就好像pygame正在干扰tkinterit,因为我打开了一个pygame GUI,当我调用root=Tk()时,它正在生成另一个GUI,所以我删除了root=Tk(),现在对话框的打开不起作用了。pypi.org/project/easygui——easygui很容易理解——我确信pygame可以加载文件,否则,您无法运行带有声音或图形的游戏,但没有内置的菜单。你需要为此找到一些分类库,或者建立自己的分类库。这只是碰巧很快。在游戏开始运行之前,你可以加载它吗?这似乎是最好的方法。我想使用“文件类型”,你能告诉我怎么做吗?我不完全理解你的评论,我也选择pygame是有原因的,我不想完全重做我的游戏引擎(对不起,如果我的消息听起来像是愤怒的语气),同样的错误再次出现。。。
#! /usr/bin/env python3
import easygui as g

title = 'Choose your destiny'
filename = g.fileopenbox( title )

file = open( filename )