0

I know that this question has been answered before but that does not solve my problem.

This is the faulty code :

def insert_row(*args):
        for i in args:
            name1 = i[0]
            phone1 = i[1]
            c.execute('''INSERT INTO users(name,phone)VALUES(?,?)''', (name1, phone1))
            print('Record inserted')

And this is the traceback :

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\ak00479324\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1550, in __call__
    return self.func(*args)
  File "D:/ak00479324/TECHM/Python POC\db_demo.py", line 13, in insert_row
    name1 = i[0]
TypeError: 'Event' object does not support indexing

Edit : Following is the tkinter code :

from tkinter import *
from tkinter import ttk
import db_demo_2

root = Tk()
root.title("Form")

frame = ttk.Frame(root, padding="20 20 50 50")
frame.grid(column=0, row=0, sticky=(N, W, E, S))

name = StringVar()
phone = StringVar()

name_entry = ttk.Entry(frame, width=20, textvariable=name)
name_entry.grid(column=3, row=1, sticky=(W, E))

phone_entry = ttk.Entry(frame, width=20, textvariable=phone)
phone_entry.grid(column=3, row=2, sticky=(W, E))

ttk.Button(frame, text="Enter", command=db_demo_2.insert_row).grid(column=3, row=3, sticky=W)

ttk.Label(frame, text="Name").grid(column=1, row=1, sticky=W)
ttk.Label(frame, text="Phone").grid(column=1, row=2, sticky=W)

for child in frame.winfo_children(): child.grid_configure(padx=10, pady=10)

name_entry.focus()
root.bind('<Return>', db_demo_2.insert_row)

root.mainloop()

And the db_demo_2 file which contains the insert_row method :

import sqlite3

sqlite_file = 'db_1.sqlite'

conn = sqlite3.connect(sqlite_file)
c = conn.cursor()

c.execute('''CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, name TEXT, phone INTEGER)''')

def insert_row(*args):
    print(*args)
    name1 = args[0]
    phone1 = args[1]
    for i in args:
        c.execute('''INSERT INTO users(name,phone) VALUES(?,?)''',(name1,phone1))
        print('Record inserted')

conn.commit()
3
  • I think the error message explains what's going on. you are trying to access an Event object like it's a List. it won't work. put a print(args) as the first statement of the function to see what your insert_row function is actually receiving. Commented Jul 14, 2017 at 9:31
  • @Himal This is what I get when I print the list (<tkinter.Event object at 0x044AFE90>,) Commented Jul 14, 2017 at 12:06
  • As you can see it's a tkinter.Event object. you are not passing the name/phone. can you update your question to include the relevant tkinter code ? include the tkinter tag as well. Commented Jul 14, 2017 at 14:19

1 Answer 1

1

You can't get the name / phone out of the "args". You have to use the StringVar objects you created. Also note that we generally use event=None instead of *args for a function that you use in bind.

def insert_row(event=None):
    name1 = name.get() # get the value from the "name" StringVar
    phone1 = phone.get()
    c.execute('''INSERT INTO users(name,phone)VALUES(?,?)''', (name1, phone1))
    print('Record inserted')

This code needs to be in the same file as the tkinter code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.