I'm following a video on youtube that creates a bunch of entry boxes in tkinter and an associated label. The video has a fixed number of entry boxes whereas my code auto-generates the entry boxes because the number I need changes depending on which notebook frame/tab the user is in. I'm struggling with positioning that labels/entry in the frame. It's easy when you have a fixed number but not so easy when they are auto-generated.
My code just creates a couple of columns for each entry and label pair. I'm trying to find a way so they are paired in rows that fit the size of the window.
Here is the video with roughly what I am trying to create
https://www.youtube.com/watch?v=G9seoA3Mv4Y
And here is my code that I have been trying to make work. It's the part I have commented as "# Add Entry boxes to record and edit records". Thank you for any help.
My code just creates a couple of columns for each entry and label pair. I'm trying to find a way so they are paired in rows that fit the size of the window.
Here is the video with roughly what I am trying to create
https://www.youtube.com/watch?v=G9seoA3Mv4Y
And here is my code that I have been trying to make work. It's the part I have commented as "# Add Entry boxes to record and edit records". Thank you for any help.
from tkinter import *
from tkinter.ttk import *
import sqlite3
### Create tkinter window ##
root = Tk()
root.title("Bookkeeping")
root.geometry("1024x768")
#root.attributes("-fullscreen",1)
### Add a Notebook to window ###
notebook = Notebook(root) # Adds a Notebook to the root windo
notebook.pack(fill="both", expand=1, pady=0) # Packs the Notebook to the root window
### Class to add different types of tab to the notebook ###
class Create_tab_frame:
# Makes a blank tab in the notebook with the object name and tab name 'notebook_tab_name'
def __init__(self, notebook_tab_name, contact_details):
self.notebook_tab_name = notebook_tab_name
self.contact_details = contact_details
self.notebook_frame = Frame(notebook) # Makes a frame in notebook called notebook_frame
self.notebook_frame.pack(fill="both", expand=1) # Packs the notebook_frame
notebook.add(self.notebook_frame, text=str(self.notebook_tab_name)) # Adds the frame to the notebook frame and calls it text=...
self.contact_tab()
# Makes a tab for administration of contacts
def contact_tab(self):
# Set style parameters
self.style = Style() # Adds the style widget from ttk to use in tkinter
self.style.map("Treeview", # sets the colour of the selected row
background=[('selected', "#347083")])
# Make a frame in the notebook frame and adds a scrollbar
self.tree_frame = Frame(self.notebook_frame) # Makes a frame inside notebook_frame
self.tree_frame.pack(fill="both", expand=1, pady=0) # Packs tree_frame
self.tree_frame_scroll = Scrollbar(self.tree_frame) # Adds a scrollbar to tree_frame
self.tree_frame_scroll.pack(side=RIGHT, fill=Y) # Packs the scrollbar
# Add Treeview to the frame inside the notebook frame
self.tree = Treeview(self.tree_frame, yscrollcommand=self.tree_frame_scroll.set, selectmode="extended") # Makes the Treeview table inside the tree_frame, which is inside the Notebook frame
self.tree.pack(fill="both", expand=1) # Packs the Treeview table
self.tree_frame_scroll.config(command=self.tree.yview) #Configures the scrollbar
# Add columns to Treeview and gives them names/headings
self.tree['columns'] = self.contact_details # contact_detail taken from __init__ are used to make columns
self.tree.column("#0", width=0, stretch=NO) # sets the width of the first (null) column to 0 to make it invisible
for i in self.contact_details: # Loops through the number of columns the tab has and sets each to 50px
self.tree.column(i, minwidth=25, width=50)
self.tree.heading("#0", text="") # Sets the heading of each column to the names defined by the tab constructor
for i in self.contact_details:
self.tree.heading(i, text=i)
# Colour stripe the rows in the Treeview table
#self.tree.tag_configure('oddrow', background="white")
#self.tree.tag_configure('evenrow', background="lightblue")
# Add Entry boxes to record and edit records
data_frame = LabelFrame(self.notebook_frame, text="Add/Edit " + self.notebook_tab_name + " Information") # Make a frame to add Entry boxes
data_frame.pack(fill="x", expand="1", pady=10)
for i in self.contact_details:
i = Label(data_frame, text=i) # For each item in contact_details a Label is created
i.grid(column=1)
i = Entry(data_frame) # For each item in contact_details an Entry box is created
i.grid(column=2)
# Add functional buttons
button_frame = LabelFrame(self.notebook_frame)
button_frame.pack(fill="x", expand="1", pady=10)
add_button = Button(button_frame, text="Add " + self.notebook_tab_name)
add_button.grid(row=1, column=1, padx=10, pady=10)
update_button = Button(button_frame, text="Update " + self.notebook_tab_name + " Details")
update_button.grid(row=1, column=2, padx=10, pady=10)
remove_button = Button(button_frame, text="Remove " + self.notebook_tab_name + " Details")
remove_button.grid(row=1, column=3, padx=10, pady=10)
view_history_button = Button(button_frame, text="View " + self.notebook_tab_name + " History")
view_history_button.grid(row=1, column=4, padx=10, pady=10)
### Create customer tab ###
customer_tab_name = "Customers"
customer_details = ("ID",
"Name",
"Company",
"Street",
"Town",
"City",
"County",
"Postcode",
"Email",
"Phone",
)
customer_tab = Create_tab_frame(customer_tab_name, customer_details)
### Create vendor tab ###
vendor_tab_name = "Vendors"
vendor_details = ( "ID",
"Name",
"Company",
"VAT",
"Street",
"Town",
"City",
"County",
"Postcode",
"Email",
"Phone",
)
vendor_tab = Create_tab_frame(vendor_tab_name, vendor_details)
root.mainloop()
