0
我嘗試在使用此代碼的終端中打開pdf文件並打印文件名。Tkinter filedialog.askopenfilename中的例外
from Tkinter import *
# Hold onto a global reference for the root window
root = None
filename = ''
def openFile():
global filename
root.filename = root.filedialog.askopenfilename(filetypes = (("PDF File" , "*.pdf"),("All Files","*.*")))
print root.filename
def main():
global root
root = Tk() # Create the root (base) window where all widgets go
openButton = Button(root, text="Genarate",command=openFile)
openButton.pack()
root.mainloop() # Start the event loop
main()
但是,代碼是不正確的工作。當我按Genarate按鈕給這個錯誤。
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1540, in __call__
return self.func(*args)
File "1gui.py", line 12, in openFile
root.filename = root.filedialog.askopenfilename(filetypes = (("PDF File" , "*.pdf"),("All Files","*.*")))
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1902, in __getattr__
return getattr(self.tk, attr)
AttributeError: filedialog
我的代碼有什麼問題?