python - Can't get value of Entry Widget in Tkinter -
python - Can't get value of Entry Widget in Tkinter -
i'm wanting retrieve value (a filename) of entry widget in tkinter gui , utilize in file_open function filename, read it, , homecoming contents text widget.
but i'm getting attribute error saying application object has no attribute f3_entry (despite me creating entry widget assigned f3_entry). doing wrong here?
from tkinter import * 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 @ {} {}".format(event.widget, event.x, event.y)) r in range(6): self.master.rowconfigure(r, weight=1) 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 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()
you created local variable f3_entry
in createwidgets()
method, not attribute. want add together self.
reference in front end of that:
self.f3_entry = entry(frame3) self.f3_entry.pack(fill=both)
that create attribute on application
instance , can referenced in file_open()
method.
note need phone call .pack()
method separately; method returns none
, need store entry()
object in attribute first before calling it.
the same applies f3_text
widget; need treat attribute both in createwidgets()
, in file_open()
:
# in createwidgets self.f3_text = text(frame3) self.f3_text.pack(fill=both) # ... # in file_open self.f3_text.insert(f)
python user-interface tkinter
Comments
Post a Comment