Reputation: 19
As a part of an inventory system I am making, I want Tkinter to collect the values of the data I wish to insert into a database through the user typing in entry boxes and clicking an 'Add Stock' button.
My problem is, that, well I'm not entirely sure how to combine sqlite3 with Tkinter code.
Below is my Tkinter code for the 'Add Stock' window
def addStock():
root = Tk()
root.title('Add Stock')
label1 = Label(root, text='Gender')
label2 = Label(root, text='Price')
label3 = Label(root, text='Enter Product Name')
label4 = Label(root, text='Colour')
label5 = Label(root, text='Size')
label6 = Label(root, text='Enter Amount')
label7 = Label(root, text='Source')
label8 = Label(root, text='Enter ProductID')
entry1 = Entry(root)
entry2 = Entry(root)
entry3 = Entry(root)
entry4 = Entry(root)
entry5 = Entry(root)
entry6 = Entry(root)
entry7 = Entry(root)
entry8 = Entry(root)
label1.grid(row=0, sticky=E)
label2.grid(row=1, sticky=E)
label3.grid(row=2, sticky=E)
label4.grid(row=3, sticky=E)
label5.grid(row=4, sticky=E)
label6.grid(row=5, sticky=E)
label7.grid(row=6, sticky=E)
label8.grid(row=7, sticky=E)
entry1.grid(row=0, column=1)
entry2.grid(row=1, column=1)
entry3.grid(row=2, column=1)
entry4.grid(row=3, column=1)
entry5.grid(row=4, column=1)
entry6.grid(row=5, column=1)
entry7.grid(row=6, column=1)
entry8.grid(row=7, column=1)
frame3 = Frame(root)
frame3.grid(columnspan = 2)
button1 = Button(frame3, padx=10, pady=10, bd=2, text="Add Stock", command=insert_data)
button1.grid()
Below is my insert
statements for sqlite3 that I wish to link with the Tkinter code.
def insert_data(values):
with sqlite3.connect("jam_stock.db") as db:
cursor = db.cursor()
sql = "insert or ignore into Product (Name, ProductID) values (?,?)"
query(sql,values)
db.commit()
def insert_product_type_data(records): #normalised
sql = "insert into ProductType(AmountInStock, Size, Colour, Source) values (?,?,?,?)"
for record in records:
cursor.execute(sql,record)
def insert_product_gender_data(records): #normalised
sql = "insert into ProductGender(Gender, Price) values (?,?)"
for record in records:
cursor.execute(sql, records)
I have already defined the tables referenced in the subroutines, but I'm struggling to find a way to insert data into those tables through Tkinter.
I am running Python 3.4 for those wondering. Help would be highly appreciated.
Upvotes: 1
Views: 7637
Reputation: 21
One trick is using 'command' in your button. As listed in the above comment try using the method:
def _add_to_db: get_data = entry.get()#This should be the text from Entry insert = *some code for inserting data to database plus get_data in \n order to be present in your database*
btn = Button(root, text="Place in DB", command=_add_to_db).pack()
Upvotes: 0
Reputation: 385830
The normal way to approach this is to have your button call a function specially for that button. The responsibilities of that function are to gather the data from the GUI, call some function that uses the data, and then post any results back to the GUI.
For example:
...
button1 = Button(... text="Add Stock", command=_on_add_stock)
...
def _on_add_stock():
gender = entry1.get()
price = entry2.get()
...
insert_data(gender, price, ...)
The above won't work with the code you posted, but it gives the general idea. One reason it won't work with the code you've posted is because entry1
, etc are all local variables. It would behoove you to use a more object-oriented approach, so those variables are all attributes of a class. You can see the answers to Best way to structure a tkinter application for examples.
Upvotes: 1