Getting values from object oriented tkinter

I’m creating object oriented tkinter application, but I can’t find way to get values from entry.

class App(tk.Frame):
    from verification import Verification
    validation = Verification()

    def __init__(self, parent, *args, **kwargs):

        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        self.email = tk.Entry(self).grid(column=1, row=4)
        self.email_lb = tk.Label(self, text='Email: ', font='Helvetica 11').grid(column=0, row=4)

        self.submit = tk.Button(self, text="Check", width=30, command = self.check_validity).grid(column=1, row=11)

    def check_validity(self, *args):
        email = self.email.get()
        print(email)

But when running this code I get this error

Traceback (most recent call last):
  File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.8_3.8.2032.0_x64__qbz5n2kfra8p0libtkinter__init__.py", line 1895, in __call__
    return self.func(*args)
  File "C:UserskrystPycharmProjectscovidFormapp.py", line 164, in check_validity
    email = self.email.get()
AttributeError: 'NoneType' object has no attribute 'get'

Does anyone know how to solve this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Ok this is a very common mistake. When you create your tk.Entry you do this:

self.email = tk.Entry(self).grid(column=1, row=4)

That creates the entry then immediately calls its .pack method. It stores whatever .pack returns (always None) to self.email. What you want to do is this:

self.email = tk.Entry(self)
self.email.grid(column=1, row=4)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x