0

This is the code I am working on. In the section where I create the sumbit function, I try to use the .get() function however it does not work. This code is for my assessment I am currently doing and it is due in a month. I have been watching tkinter videos on youtube to try and get an idea of what I am doing but after rewatching the video I can not find any differences between my code and his. The link to the video is https://www.youtube.com/watch?v=YR3h2CY21-U&list=PLCC34OHNcOtoC6GglhF3ncJ5rLwQrLGnV&index=19. I am up to 12 minutes and 50 seconds.

from tkinter import *
import sqlite3

root = Tk()
# root.geometry("800x500")


# Databases


# Create a database or connect to one
conn = sqlite3.connect('incidents.db')

# create a cursor
c = conn.cursor()

# Create table

'''c.execute("""CREATE TABLE incidents (
        first_name text,
        last_name text,
        address text,
        city text,
        state text,
        zipcode integer
        )""")'''


# Create Submit Function For database
def submit():
    # Create a database or connect to one
    conn = sqlite3.connect('incidents.db')
    # Create cursor
    c = conn.cursor()

    # Insert Into Table
    c.execute("INSERT INTO incidents VALUES (:name, :date, :address, :city, :state, :zipcode)",
              {
                  'name': Name.get(),
                  'date': date.get(),
                  'address': address.get(),
                  'city': city.get(),
                  'state': state.get(),
                  'zipcode': zipcode.get()
              })

    # Commit Changes
    conn.commit()

    # Close Connection
    conn.close()

    # Clear The Text Boxes
    Name.delete(0, END)
    date.delete(0, END)
    address.delete(0, END)
    city.delete(0, END)
    state.delete(0, END)
    zipcode.delete(0, END)


# Create Text Boxes
Name = Entry(root, width=30).grid(row=0, column=1, padx=20, pady=(10, 0))
date = Entry(root, width=30).grid(row=0, column=3)
address = Entry(root, width=30).grid(row=0, column=5)
city = Entry(root, width=30).grid(row=0, column=7)
state = Entry(root, width=30).grid(row=1, column=1)
zipcode = Entry(root, width=30).grid(row=1, column=3)
delete_box = Entry(root, width=30).grid(row=1, column=5, pady=5)

# Create Text Box Labels
name_label = Label(root, text="Name").grid(row=0, column=0, pady=(10, 0))
date_label = Label(root, text="Last Name").grid(row=0, column=2)
address_label = Label(root, text="Address").grid(row=0, column=4)
city_label = Label(root, text="City").grid(row=0, column=6)
state_label = Label(root, text="State").grid(row=1, column=0)
zipcode_label = Label(root, text="Zipcode").grid(row=1, column=2)
delete_box_label = Label(root, text="Select ID").grid(row=1, column=4, pady=5)

# Create Submit Button
submit_btn = Button(root, text="Add Record To Database", command=submit)
submit_btn.grid(row=6, column=0, columnspan=2, pady=10, padx=10, ipadx=100)

# Commit Changes
conn.commit()

# Close Connection
conn.close()


root.mainloop()

When I run the code I get this error message:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1699, in __call__
    return self.func(*args)
  File "/Users/School/PycharmProjects/HelloWorld/Databases.py", line 39, in submit
    'name': Name.get(),
AttributeError: 'NoneType' object has no attribute 'get'

If someone can help me it would be greatly appreciated.

  • 1
    Does this answer your question? [Tkinter: AttributeError: NoneType object has no attribute ](https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name) – Saad Jun 27 '20 at 08:22

2 Answers2

0

There is no problem in your connectivity execution. The error you are getting is because of the way you packed your widgets on the screen.

If you pack an entry box in the same line in which you defined it, it returns None and None is a special data type of python which obviously do not have any attribute get

Instead of

Name = Entry(root, width=30).grid(row=0, column=1, padx=20, pady=(10, 0))
date = Entry(root, width=30).grid(row=0, column=3)
address = Entry(root, width=30).grid(row=0, column=5)
city = Entry(root, width=30).grid(row=0, column=7)

Try this

Name = Entry(root, width=30)
Name.grid(row=0, column=1, padx=20, pady=(10, 0))
date = Entry(root, width=30)
date.grid(row=0, column=3)
address = Entry(root, width=30)
address.grid(row=0, column=5)
city = Entry(root, width=30)
city.grid(row=0, column=7)

Hope it helps

Salik Malik
  • 207
  • 1
  • 2
  • 15
0

What I think you should try is : Name=tk.StringVar(root) tk.Entry(root,textvariable=Name).grid(row=0,column=1) then use Name.get() One more thing instead of import * use import tkinter as tk (as convention). For your case you can directly write StingVar() and Entry without tk. prefix. Hope this will work

  • I forgot that StringVar also requires a master. I have edited the answer and it will definitely work. Here is a sample: from tkinter import * def submit(): print(name.get()) root=Tk() name=StringVar(root) Entry(root,textvariable=name).grid(row=0,column=1) Button(root,text="Submit",command=submit).grid(row=1,column=2) root.mainloop() –  Jun 27 '20 at 12:34