2011-08-01 20 views
0

我正在嘗試使用Tkinter來選擇一個文件,然後將該文件名導入一個參數以傳遞函數。該程序在文件被選擇後停止。我包含一個打印語句只是爲了查看它是否返回路徑,它確實如此,我不確定它爲什麼不能在函數中使用。Python文件對話框問題

#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') 
+0

'master'的是不是在你的腳本中定義,所以當它到達'master.quit()'它會引發:'NameError:name'master'is not defined'。 – TorelTwiddler

回答

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() 
+0

它工作正常,但它不會將文件名傳遞到函數 –

+0

您能向我們展示定義'spaceParser'的代碼嗎?如果程序沒有在'master.quit'中停止,那麼問題可能在'spaceParser'中。您向我們展示的所有其他代碼都應該正常工作。 – unutbu