Below adds the entered values from the user, adds them to the selected tables and then adds them to the listbox. However, it becomes complicated for the user to identify the values added, as at the moment they're just appearing as 1, Male, Name1, Blue, S, 5, 12.50, Internal
.
How would I use tkinter to identify the values added to the listbox. Ideally, it would be nice to have the description directly above the inserted value. Help is greatly appreciated!
class AddStock(MainApp):
def __init__(self, master):
frame=Frame(master, width=80, height=50)
frame.grid()
self.text = Label(frame, text="Format of data below is ProductID/Gender/Colour/Size/Amount/Source.")
self.text.grid(row=11, rowspan=1, sticky='w')
self.content=Listbox(master, width=100)
self.content.grid(row=12)
#open database
self.conn = sqlite3.connect('jamstock')
c = self.conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS Product
(productid INTEGER primary key NOT NULL,
name TEXT )''')
c.execute('''CREATE TABLE IF NOT EXISTS ProductType
(productid INTEGER,
gender TEXT,
colour TEXT,
size TEXT,
amountinstock INTEGER,
source TEXT,
primary key(productid, colour, size, gender)
foreign key(gender) REFERENCES ProductGender(gender)
foreign key(productid) REFERENCES Product(productid)) ''')
c.execute('''CREATE TABLE IF NOT EXISTS ProductGender
(gender TEXT primary key,
price FLOAT)''')
self.conn.commit()
#read people
product = c.execute("SELECT * FROM Product NATURAL JOIN ProductType NATURAL JOIN ProductGender")
self.conn.commit()
#add to list
for amount in product:
self.content.insert(END, amount)
c.close()
I have deleted the entry fields for the sake of importance so the item
variable does have a purpose! Here is where tkinter retrieves the values and inserts.
def add_stock(self):
item1 = self.productidField.get() #variable 'item' used to define values entered into fields
item2 = self.genderField.get()
item3 = self.nameField.get()
item4 = self.colourField.get()
item5 = self.sizeField.get()
item6 = self.amountinstockField.get()
item7 = self.priceField.get()
item8 = self.sourceField.get()
self.productidField.delete(0, END)
self.genderField.delete(0, END)
self.nameField.delete(0, END)
self.colourField.delete(0, END)
self.sizeField.delete(0, END)
self.amountinstockField.delete(0, END)
self.priceField.delete(0, END)
self.sourceField.delete(0, END)
#add to database
c = self.conn.cursor()
try:
c.execute("INSERT INTO Product VALUES (?, ?)", (item1, item3))
except:
showerror('Error', 'Invalid character used for field. Please insert correct values.')
try:
c.execute("INSERT OR IGNORE INTO ProductType VALUES (?, ?, ?, ?, ?, ?)", (item1, item2, item4, item5, item6, item8))
except:
showerror('Error', 'Invalid character used for field. Please insert correct values.')
try:
c.execute("INSERT OR IGNORE INTO ProductGender VALUES (?, ?)", (item2, item7))
except:
showerror('Error', 'Invalid character used for field. Please insert correct values.')
self.conn.commit()
c.close()
#add to list
self.content.insert(END, (item1, item2, item3, item4, item5, item6, item7, item8))
Aucun commentaire:
Enregistrer un commentaire