This is really helpful, thank you.
The code I posted was one iteration of the 500 I've tried!
I think I have the sql code correct now but my problem is that
Thank you once again!
EDIT:
Done it! Thank you.
I'm sure my code it structured incorrectly but so far it's doing what I wanted. The database stuff is in the method
The code I posted was one iteration of the 500 I've tried!
I think I have the sql code correct now but my problem is that
entry_value
is created using a for
loop so the tuple is only ever one value long. I'll try to work out how build the tuple outside the loop.Thank you once again!
EDIT:
Done it! Thank you.
I'm sure my code it structured incorrectly but so far it's doing what I wanted. The database stuff is in the method
on_save_button
. No doubt vendor will cause problems because there aren't enough ?'s. from tkinter import * from tkinter.ttk import * import sqlite3 ### Create SQLite database and connect to it ### conn = sqlite3.connect('Bookkeeping_Database.sqlite3') cur = conn.cursor() ### Create tkinter window ### root = Tk() root.title("Bookkeeping") root.geometry("800x600") ### Add a Notebook to window ### home_screen = Notebook(root) home_screen.pack(fill="both", expand=1, pady=15) ### Class to add tabs to Notebook with a method to add entry boxes and a save button ### class Contacts_tab: def __init__(self, tab): self.tab_name = str(tab) self.tab = tab self.tab = Frame(home_screen) self.tab.pack(fill="both", expand=1) home_screen.add(self.tab, text=self.tab_name) self.detail_name_dictionary = {} self.detail_name_list_keys = [] def create_entry_boxes(self, detail_name): self.detail_name = detail_name self.entry_box = Entry(self.tab) self.entry_box.grid(column=1) self.entry_box.insert(0, detail_name) self.detail_name_dictionary[self.detail_name] = self.entry_box self.detail_name_list_keys.append(detail_name) def save_button(self): self.save_button = Button(self.tab, text="Save") self.save_button.grid(column=1) def on_save_button(self, event): inputted_data=[] for detail_name in self.detail_name_list_keys: entry_value = self.detail_name_dictionary[detail_name].get() print(f' {detail_name} = {entry_value}') inputted_data.append(entry_value) inputted_data=tuple(inputted_data) print(inputted_data) print(type(inputted_data)) print(self.detail_name_list_keys) cur.execute("CREATE TABLE IF NOT EXISTS " + self.tab_name + " (" + " TEXT, ".join(self.detail_name_list_keys) +" TEXT)") cur.execute("INSERT INTO "+ self.tab_name +"(" + ", ".join(self.detail_name_list_keys) + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", inputted_data,) conn.commit() ### Create Customers tab ### def create_customer_tab(): customer_tab = Contacts_tab("Customers") entry_boxes = [ "Customer_ID", "Company", "Name", "Address_1", "Address_2", "Address_3", "Address_4", "Email", "Phone_1", "Phone_2", ] for detail_name in entry_boxes: customer_tab.create_entry_boxes(detail_name) customer_tab.save_button() customer_tab.save_button.bind('<Button-1>', customer_tab.on_save_button) def create_vendor_tab(): vendor_tab = Contacts_tab("Vendors") entry_boxes = [ "Vendor_ID", "Company", "Name", "Address_1", "Address_2", "Address_3", "Address_4", "Email", "Phone_1", "Phone_2", "VAT_number" ] for detail_name in entry_boxes: vendor_tab.create_entry_boxes(detail_name) vendor_tab.save_button() vendor_tab.save_button.bind('<Button-1>', vendor_tab.on_save_button) #Main create_customer_tab() create_vendor_tab() root.mainloop() conn.close()