Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8065599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:43:12+00:00 2026-06-05T11:43:12+00:00

I have the following code where I’m trying to allow the user to open

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:43:13+00:00Added an answer on June 5, 2026 at 11:43 am

    askopenfilename only returns a file name, what you wanted was askopenfile which accepts a mode parameter and opens the file for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code and it returns false for @B LIKE @A comparison in
I have following code if (rs != null) { out.println(result set has got something);
I have following code in the html file: <div class=navbar navbar-fixed-top> <div class=navbar-inner> <div
I have following code for opening all files: int ret= open(zFile, flags, mode); posix_fadvise
I have following code in my template: <div class=account> {% if request.user.is_authenticated %} <a
I have following code to serialize my data into a file: out = new
I have following code that determines the user's language. if (locale.equalsIgnoreCase(eng)) { .... }
I have following code to find if row is selected, which is selected and
I have following code, after five tries (wrong words like dfasfasfb) the application stops.
I have following code in VS2008 If Linq.Enumerable.Contains(Of String)(selectedUsersRoles, RoleCheckBox.Text) Then RoleCheckBox.Checked = True

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.