1

I'm having a bit of an issue returning values that are entered into the entry points that I've created and I'm not sure why.

class DbGui:
    def __init__(self, master):
        self.master = master
        self.label = ttk.Label(master, text="Tkinter DB File-r")
        self.label.grid(row=0, column=0, columnspan=2)
        ttk.Button(master, text="Connect", command=self.connect).grid(row=1, column=0)
        ttk.Button(master, text="Disconnect", command=self.disconnect).grid(row=1, column=1)
        ttk.Button(master, text="Submit", command=self.tk_submit).grid(row=1, column=2)
        ttk.Button(master, text="Exit", command=self.tk_exit).grid(row=6, column=3)

    def show_inputs(self):
        # Name
        ttk.Label(self.master, text="Name").grid(row=2, column=0)
        name = ttk.Entry(self.master).grid(row=2, column=1)

        # Date
        ttk.Label(self.master, text="Date").grid(row=3, column=0)
        date = ttk.Entry(self.master).grid(row=3, column=1)
        # Value
        ttk.Label(self.master, text="Value").grid(row=4, column=0)
        value = ttk.Entry(self.master).grid(row=4, column=1)
        return name, date, value

    def tk_submit(self):
        print(name.get()) # This is where I think the problem is.

def main():
    root = Tk()
    app = DbGui(root)
    app.show_inputs()
    root.mainloop()


if __name__ == '__main__':
    main()

How can I get these values, once entered by the user to populate variables or a list or anything really. I'm VERY new to tkinter so any advice or help is appreciated.

mnickey
  • 727
  • 1
  • 6
  • 15
  • 2
    Possible duplicate of [Why is None returned instead of tkinter.Entry object](https://stackoverflow.com/q/6933572/953482). Short answer: create your widget and call `grid` on it on two separate lines. – Kevin Oct 03 '17 at 18:40

1 Answers1

3

You must initialize your variable name as an attribute of the class (prefixed with self.), and call grid on self.name on a separate line (grid does return None - thanks @MikeSMT) :

self.name = ttk.Entry(self.master)
self.name.grid(row=2, column=1)

and call it as such:

def tk_submit(self):
    print(self.name.get())
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80