Patrick Beeson
Patrick Beeson

Reputation: 1695

Can't get value of Entry Widget in Tkinter

I'm wanting to retrieve the value (a filename) of my Entry widget in a Tkinter GUI and use it in my file_open function to get a filename, read it, and return the contents to my Text widget.

But I'm getting an attribute error saying Application object has no attribute f3_entry (despite me creating an entry widget assigned to f3_entry). What am I doing wrong here?

from tkinter import *
from tkinter.filedialog import LoadFileDialog, SaveFileDialog, Directory


class Application(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.grid()
        self.master.title("Grid layout")
        self.createWidgets()


    def createWidgets(self):
        def handler(event):
            print("Frame {} clicked at {} {}".format(event.widget, event.x, event.y))

        for r in range(6):
            self.master.rowconfigure(r, weight=1)  
        for c in range(6):
            self.master.columnconfigure(c, weight=1)

        Frame1 = Frame(self.master, bg="red", name='frame 1')
        Frame1.grid(row=0, column=0, rowspan=3, columnspan=3, sticky=W+E+N+S)
        Frame1.bind("<Button-1>", handler)

        Frame2 = Frame(self.master, bg="green", name='frame 2')
        Frame2.grid(row=3, column=0, rowspan=3, columnspan=3, sticky=W+E+N+S)
        Frame2.bind("<Button-1>", handler)

        Frame3 = Frame(self.master, bg="blue", name='frame 3')
        Frame3.grid(row=0, column=3, rowspan=6, columnspan=4, sticky=W+E+N+S)

        f3_entry = Entry(Frame3).pack(fill=BOTH)
        f3_text = Text(Frame3).pack(fill=BOTH)

        r_button = Button(self.master, text="Red").grid(row=6,column=1,sticky=E+W)
        b_button = Button(self.master, text="Blue").grid(row=6,column=2,sticky=E+W)
        g_button = Button(self.master, text="Green").grid(row=6,column=3,sticky=E+W)
        bk_button = Button(self.master, text="Black").grid(row=6,column=4,sticky=E+W)
        o_button = Button(self.master, text="Open", command=self.file_open).grid(row=6,column=5,sticky=E+W) 

    def file_open(self):
        d = LoadFileDialog(self)
        fname = self.f3_entry.get()
        if fname is None:
            print("No file exists...")
        else:
            f = open(fname, 'r').read()
            f3_text.insert(f)
            f.close()

root = Tk()
app = Application(master=root)
app.mainloop()

Upvotes: 0

Views: 1207

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124988

You only created a local variable f3_entry in your createWidgets() method, not an attribute. You want to add a self. reference in front of that:

self.f3_entry = Entry(Frame3)
self.f3_entry.pack(fill=BOTH)

That would actually create an attribute on your Application instance and can then be referenced in the file_open() method.

Note that you need to call the .pack() method separately; the method returns None, so you need to store the Entry() object in an attribute first before calling it.

The same applies to your f3_text widget; you need to treat that as an attribute both in createWidgets() and in file_open():

# in createWidgets
self.f3_text = Text(Frame3)
self.f3_text.pack(fill=BOTH)

# ...

# in file_open
self.f3_text.insert(f)

Upvotes: 1

Related Questions