I have the following code where I'm trying to allow the user to open a text file and once the user has selected it, I would like the code to read it (this isn't a finished block of code, just to show what I'm after).
However, I'm having difficulties either using tkFileDialog.askopenfilename and adding 'mode='rb'' or using the code like below and using read where it produces an error.
Does anyone know how I can arrange to do this as I don't wish to have to type Tkinter.'module' for each item such as Menu and Listbox. Beginner to Tkinter and a bit confused! Thanks for the help!
import sys
from Tkinter import *
import tkFileDialog
from tkFileDialog import askopenfilename # Open dialog box
fen1 = Tk() # Create window
fen1.title("Optimisation") #
menu1 = Menu(fen1)
def open():
filename = askopenfilename(filetypes=[("Text files","*.txt")])
txt = filename.read()
print txt
filename.close()
fen1.mainloop()
Obviously the error I'm getting here is:
AttributeError: 'unicode' object has no attribute 'read'
I don't understand how to use the askopen and also be able to read the file I'm opening.
Use the askopenfilename() function to display an open file dialog that allows users to select one file. Use the askopenfilenames() function to display an open file dialog that allows users to select multiple files.
File dialogs help you open, save files or directories. This is the type of dialog you get when you click file,open. This dialog comes out of the module, there's no need to write all the code manually. Tkinter does not have a native looking file dialog, instead it has the customer tk style.
Instead of hard coding the path to a file to be used by a python program, we can allow the user to browse the os folder structure using a GUI and let the user select the file. This is achieved using the tkinter module in which we define a canvas and put a button on it to browse the files.
askopenfilename
only returns a file name, what you wanted was askopenfile
which accepts a mode
parameter and opens the file for you.
The filename
in your sample code is just that -- a string indicating the name of the file you wish to open. You need to pass that to the open()
method to return a file handle for the name. You can then read from the file handle.
Here's some quick and dirty code to run in the Python interpreter directly. (You can run this in a script, too, but I really like REPL interfaces for quickly trying things out. You may like it as well.)
$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
>>> from tkFileDialog import askopenfilename
>>> root = Tkinter.Tk() ; root.withdraw()
''
>>> filename = askopenfilename(parent=root)
>>> filename
'/tmp/null.c'
>>> f=open(filename)
>>> f.read()
'#include<stdio.h>\n\nint main()\n{\n for(;NULL;)\n printf("STACK");\n\n return 0;\n}\n\n'
>>> f.close()
>>>
Note especially that there's nothing Tkinter-specific in reading the file -- the dialog box just gives you a filename.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With