May-02-2021, 04:19 PM
I've been learning about classes and databases and thought I'd have a go at writing something that uses both features. Its a tkinter notebook that aims to collect details like name, address, email etc and save them to a sql database.
I've made a class that has (so far) three methods. __init__ creates the tab, request_details creates the entry boxes and save_button is hopefully going to .get() the inputted data and save it to the database.
I've got two problems I'd like to sort out. I can't work out how to use .get() to pull data from each entry box because each box is created using a for loop. I don't know if I need to aim to create a method in the class to use.get() or a function outside the class.
My other problem is related to the database and for a different forum. Ultimately, I want to create a table in my database using a list for column names instead of typing them in like I have done in my code. I think this problem is kinda related to how I'm hoping to add the data from the entry boxes.
I've tried sooo many things but all have failed so my code is back to where I started yesterday morning! I'm trying to keep it simple because even reading my own code can sometimes send me into a spin!
Thank you for any help.
I've made a class that has (so far) three methods. __init__ creates the tab, request_details creates the entry boxes and save_button is hopefully going to .get() the inputted data and save it to the database.
I've got two problems I'd like to sort out. I can't work out how to use .get() to pull data from each entry box because each box is created using a for loop. I don't know if I need to aim to create a method in the class to use.get() or a function outside the class.
My other problem is related to the database and for a different forum. Ultimately, I want to create a table in my database using a list for column names instead of typing them in like I have done in my code. I think this problem is kinda related to how I'm hoping to add the data from the entry boxes.
I've tried sooo many things but all have failed so my code is back to where I started yesterday morning! I'm trying to keep it simple because even reading my own code can sometimes send me into a spin!
Thank you for any help.
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 Create_tab: def __init__(self, tab): 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=tab_name) def request_details(self, detail_name): entry_box = Entry(self.tab) entry_box.grid(column=1) entry_box.insert(0, detail_name) def save_button(self): save_button = Button(self.tab, text="Save") save_button.grid(column=1) ### Create Customers tab ### def customer_tab(): customer_tab = Create_tab("Customers") entry_boxes = ["Customer ID","Name","Address 1","Address 2","Address 3","Address 4", "Email Address", "Phone 1", "Phone 2"] cur.execute('CREATE TABLE IF NOT EXISTS Customers (customer_id TEXT, name TEXT, address_1 TEXT, address_2 TEXT, address_3 TEXT, address_4 TEXT, email Address TEXT, phone_1 TEXT, phone_2 TEXT)') for detail in entry_boxes: customer_details = customer_tab.request_details(detail) save_button = customer_tab.save_button() ### Create Vendors tab ### def vendor_tab(): vendor_tab = Create_tab("Vendors") for detail in ["Name","Address 1","Address 2","Address 3","Address 4", "Email Address", "Phone 1", "Phone 2"]: vendor_details = vendor_tab.request_details(detail) #Main customer_tab() vendor_tab() root.mainloop()