Python文件对话框问题

时间:2011-08-01 21:24:03

标签: python dialog text-files

我正在尝试使用Tkinter选择一个文件,然后将该文件名导入参数以传入函数。选择文件后,程序将停止。我包含了一个print语句,只是为了查看它是否返回路径,它确实如此,我不知道为什么它在函数中不起作用。

#Main

from Tkinter import *
import tkFileDialog


fileOpen = Tk()
fileOpen.withdraw() #hiding tkinter window

file_path = tkFileDialog.askopenfilename(title="Open file", filetypes=[("txt file",".txt"),("All files",".*")])

if file_path != "":
   print "you chose file with path:", file_path

else:
   print "you didn't open anything!"

master.quit()

print file_path   


spaceParser (file_path,'r','/Users/Desktop/TygerTygerParsed.txt','w')

1 个答案:

答案 0 :(得分:3)

这个(缩短版)工作得很好:

from Tkinter import *
import tkFileDialog

fileOpen = Tk()
fileOpen.withdraw() #hiding tkinter window

file_path = tkFileDialog.askopenfilename(
    title="Open file", filetypes=[("txt file",".txt"),("All files",".*")])

if file_path != "":
   print "you chose file with path:", file_path

else:
   print "you didn't open anything!"

print file_path   

所以我猜你的程序正在停止

master.quit()