2017-10-05 114 views
0

我的代碼的目的是創建一個具有4個按鈕的GUI。其中2個是打開「瀏覽」窗口,允許用戶從目錄中選擇一個文件。第三個按鈕是允許用戶選擇要輸出到最終文檔的目錄。第四個按鈕將我的Python代碼應用於這兩個文件,創建輸出的文檔。askopenfilename沒有定義? - 在tkinter中製作瀏覽按鈕

在嘗試創建「瀏覽」按鈕時,通過stackoverflow和Internet上的許多帖子,大多數解決方案都包含通常從tkFileDialog導入的「askopenfilename」模塊。然而,不管我怎麼說,或者我導入的任何tkinter模塊的變體,我總是會收到「no module name tkfileDialog」或「askopenfilename is not defined」的錯誤消息。

我的代碼有問題嗎?這是在python 3.6的tkinter中發現的常見錯誤嗎?如何創建一個瀏覽按鈕來查找文件並添加路徑?

請讓我知道!

謝謝。

下面是我的代碼:

import os 
#from tkFileDialog import * 
from tkinter import filedialog 
from Tkinter import * 
from tkfileDialog import askopenfilename 
content = 'apple' 
file_path = 'squarebot' 


#FUNCTIONS 
def browsefunc(): #browse button to search for files 
    filename = askopenfilename() 
    infile = open(filename, 'r') 
    content = infile.read() 
    pathadd = os.path.dirname(filename)+filename 

    pathlabel.delete(0, END) 
    pathlabel.insert(0, pathadd) 

    return content 
def open_file(): #also browse button to search for files - im trying various things to get this to work! 
    global content 
    global file_path 

    #filename = filedialog.askopenfilename(filetypes = (typeName {.txt},)) 
    filename = askopenfilename()          
    infile = open(filename, 'r') 
    content = infile.read() 
    file_path = os.path.dirname(filename) 
    entry.delete(0, END) 
    entry.insert(0, file_path) 
    return content 

def process_file(content): #process conversion code 
    print(content) 

def directoryname(): 
    directoryname = filedialog.askdirectory() # pick a folder 


#GUI 

root = Tk() 
root.title('DCLF Converter') 
root.geometry("598x600") 

mf = Frame(root) 
mf.pack() 


f1 = Frame(mf, width=600, height=250) #DC file 
f1.pack(fill=X) 

f2 = Frame(mf, width=600, height=250) #LF file 
f2.pack(fill=X) 

f3 = Frame(mf, width=600, height=250) #destination folder 
f3.pack(fill=X) 

f4 = Frame(mf, width=600, height=250) #convert button 
f4.pack() 

file_path = StringVar 


Label(f1,text="Select Your DC File (Only txt files)").grid(row=0, column=0, sticky='e') #DC button 
entry = Entry(f1, width=50, textvariable=file_path) 
entry.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25) 

Label(f2,text="Select Your LF File (Only csv files)").grid(row=0, column=0, sticky='e') #LF button 
entry = Entry(f2, width=50, textvariable=file_path) 
entry.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25) 

Label(f3,text="Select Your Destination Folder").grid(row=0, column=0, sticky='e') #destination folder button 
entry = Entry(f3, width=50, textvariable=directoryname) 
entry.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25) 

Button(f1, text="Browse", command=browsefunc).grid(row=0, column=27, sticky='ew', padx=8, pady=4)#DC button 
Button(f2, text="Browse", command=browsefunc).grid(row=0, column=27, sticky='ew', padx=8, pady=4)#LF button 
Button(f3, text="Browse", command=browsefunc).grid(row=0, column=27, sticky='ew', padx=8, pady=4)#destination folder button 
Button(f4, text="RECONCILE NOW", width=32, command=lambda: process_file(content)).grid(sticky='ew', padx=10, pady=10)#convert button 


root.mainloop() 

P.S如果你發現任何其他錯誤在我的代碼,請讓我知道。我剛剛開始使用tkinter,因此這可能歸因於完全無關的事情!

備受讚賞

+0

如果將'tkfileDialog'改爲'tkFileDialog',會發生什麼? – Kevin

+0

對於Python 3.x,使用'filedialog.askopenfilename()'在現有代碼中調用該函數,或者添加'from tkinter.filedialog import askopenfilename'並使用'askopenfilename()'來調用它。 – martineau

回答

3

這是我在我的代碼使用,因此它會在這兩個Python 2和3與Tkinter的模塊工作:

try: 
    import Tkinter as tk 
    import ttk 
    from tkFileDialog import askopenfilename 
    import tkMessageBox 
    import tkSimpleDialog 
    from tkSimpleDialog import Dialog 
except ModuleNotFoundError: # Python 3 
    import tkinter as tk 
    from tkinter import ttk 
    from tkinter.filedialog import askopenfilename 
    import tkinter.messagebox as tkMessageBox 
    import tkinter.simpledialog as tkSimpleDialog 
    from tkinter.simpledialog import Dialog 
1

模塊名稱被錯誤命名。

由於python版本是3.6,你需要使用filedialog庫。在包括應該是這個樣子:

import os 
from tkinter import * 
import tkinter.filedialog 

import os 
from tkinter import * 
from tkinter import filedialog 
+0

我仍然收到錯誤「ModuleNotFoundError:No module named'tkFileDialog'」 –

+0

我可以知道您正在使用的python版本。它適用於python 2.7 –

+0

如果模塊仍然沒有找到,那麼你需要使用'pip install tkFileDialog'來安裝模塊。 –

0

的問題是實際上我需要追加askopenfilename()來FileDialog的如河東獅在現在刪除評論中提及了(它看起來這樣的 - > filedialog.askopenfilename()

+0

其他人已經給出了準確的答案。別人提供答案時,不要回答自己的問題。 v.coder和martineau都是正確的。您應該注意,提供python版本會立即爲您提供準確的答案。因爲2.X和3.X不同地導入filedialog。 –