1

Question about Tkinter :

I want to create a browse along with a text display which will display the file that I pick from the browse button. Following is my code :

Edit 1.

button_opt = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5}
    Tkinter.Button(self, text='Browse and open filename - then manually upload it', command=self.askopenfilename).pack(**button_opt)

    self.file_opt = options = {}        
    options['defaultextension'] = '.txt'
    options['filetypes'] = [('all files', '.*'), ('text files', '.txt')]
        options['initialdir'] = 'C:\\'
    options['initialfile'] = 'myfile.txt'
        options['parent'] = root
        options['title'] = 'Browse'

    self.dir_opt = options = {}
    options['initialdir'] = 'C:\\'
    options['mustexist'] = False
    options['parent'] = root
    options['title'] = 'Browse'

    image = Image.open("/home/kuber/Downloads/godata/images/header.png")
    photo = ImageTk.PhotoImage(image)
    label = Label(image=photo)
    label.image = photo # keep a reference!
    label.place(width=768, height=576)
    label.pack(side = TOP)

    self.centerWindow()
    self.master.columnconfigure(10, weight=1)
    #Tkinter.Button(self, text='upload file', command=self.Fname).pack(**button_opt)    
    self.file_name = Text(self, width=39, height=1, wrap=WORD)

def Fname(self):
    self.file_name = Text(self, width=39, height=1, wrap=WORD)
        self.file_name.grid(row=1, column=1, columnspan=4, sticky=W)

def askopenfilename(self):

   # get filename
     filename = tkFileDialog.askopenfilename(**self.file_opt)

   # open file on your own
     if filename:
     with open(self.filename, 'r') as inp_file:
        print "1"
        self.file_name.delete(0.0, END)
        self.file_name.insert(0.0, inp_file.read())
            #return open(filename, 'r')

As I press the button for Browse and open file. I expect to go from askopenfilename function to the text widget. But I get the error :

AttributeError: TkFileDialogExample instance has no attribute 'filename'

Also when I include self.file_name.grid(row=1, column=1, columnspan=4, sticky=W) outside Fname, Tkinter hangs.

1

1 Answer 1

1

When you see a python error like `X instance has no attribute 'filename', it means exactly what it says. The root cause is usually one of two things:

  • X is the object you intended to use, and it indeed does not have that attributev(maybe you misspelled it, o forgot to define it), or
  • You told the program to use X, but X is not what you think it s (ie: you think it refers to an object of a particular type but it's a string or number or some other class.

So, ask yourself, "why doesn't TkFileDialogExample have this attribute? Did you define it to have that attribute? Where and when? Did you misspell it? Or, is your code supposed to get the attribute from some other object?

In other words, your code is using self.filename: is self what you think it is?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.