我的代碼的目的是創建一個具有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,因此這可能歸因於完全無關的事情!
備受讚賞
如果將'tkfileDialog'改爲'tkFileDialog',會發生什麼? – Kevin
對於Python 3.x,使用'filedialog.askopenfilename()'在現有代碼中調用該函數,或者添加'from tkinter.filedialog import askopenfilename'並使用'askopenfilename()'來調用它。 – martineau