打开文本文件并将所选文件的内容保存为Tkinter中的变量

时间:2016-01-05 18:49:51

标签: python file python-3.x tkinter

我想使用下拉菜单打开并阅读文本文件

subMenu.add_command(label = "Open file", command = OpenFile)

然后我想将此文件中的文本保存到变量中。我不了解askopenfilename的东西,只是想知道用户如何选择他们的文件以及如何将该文本放入变量中。

window.fileName = filedialog.askopenfilename(filetypes = (("Text File", "*.txt"))
PlainText=open(fileName, 'r')
Entered_Text = PlainText.read()

我收到了这个错误,

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
    return self.func(*args)
  File "C:\Users\matty\Desktop\Task3Python .py", line 63, in OpenFile
    window.fileName = filedialog.askopenfilename(filetypes = (("Text File", "*.txt")))
  File "C:\Python34\lib\tkinter\filedialog.py", line 375, in askopenfilename
    return Open(**options).show()
  File "C:\Python34\lib\tkinter\commondialog.py", line 48, in show
    s = w.tk.call(self.command, *w._options(self.options))
_tkinter.TclError: bad file type "*.txt", should be "typeName {extension ?extensions ...?} ?{macType ?macTypes ...?}?"

0 个答案:

没有答案