Python Forum
.get() from generated Entry widgets in tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
.get() from generated Entry widgets in tkinter
#5
Wow, a huge "thank you!"

I guess the order your code is written is far better than the way I wrote mine but I struggled to follow all of it. I did however manage to pull the parts I needed and add them to my code. It now all works the way I wanted. I've also managed to get it adapt depending on which Entry boxes you want in each tab.

I'm now just working out how to enter it to a sql database.

Thank you!

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):
        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):
            for detail_name in self.detail_name_list_keys:
                entry_value = self.detail_name_dictionary[detail_name].get()
                print(f' {detail_name} = {entry_value}') 
        
            cur.execute("CREATE TABLE IF NOT EXISTS " + self.tab_name + " (" + " TEXT, ".join(self.detail_name_list_keys) +" TEXT)")
            cur.executemany("INSERT INTO "+ self.tab_name +"(" + ", ".join(self.detail_name_list_keys) + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", str(entry_value,))
            conn.commit()
            

### Create Customers tab ###
def create_customer_tab():
    customer_tab = Create_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)

#Main
create_customer_tab()

root.mainloop()
conn.close()
Reply


Messages In This Thread
RE: .get() from generated Entry widgets in tkinter - by snakes - May-03-2021, 11:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  make widgets disappear from tkinter jacksfrustration 12 1,189 Feb-06-2024, 03:58 PM
Last Post: deanhystad
  [Tkinter] Making entry global in tkinter with multiprocessing luckyingermany 2 2,344 Jan-21-2022, 03:46 PM
Last Post: deanhystad
  Tkinter Exit Code based on Entry Widget Nu2Python 6 3,020 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  [Tkinter] Update variable using tkinter entry methon drSlump 6 5,246 Oct-15-2021, 08:01 AM
Last Post: drSlump
  Tkinter | entry output. Sap2ch 1 2,016 Sep-25-2021, 12:38 AM
Last Post: Yoriz
  Entry Validation in tkinter shahulvk 4 16,445 Oct-28-2020, 10:12 PM
Last Post: joe_momma
  [Tkinter] Getting Input from Tkinter Entry juliabrushett 6 21,434 May-30-2020, 03:29 PM
Last Post: Larz60+
  Converting Entry field value to integer in tkinter scratchmyhead 2 4,984 May-11-2020, 03:41 PM
Last Post: scratchmyhead
  [Tkinter] Tkinter adding entry values scratchmyhead 1 2,215 May-04-2020, 05:21 AM
Last Post: Yoriz
  [Tkinter] Connect Toplevel Radiobuttons to root Label/Entry widgets iconit 2 2,469 Apr-28-2020, 06:50 AM
Last Post: iconit

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020