Change the color of the database header border (treeview) to the following: #ededee. The…
- User
Change the color of the database header border (treeview) to the following: #ededee. The border color not the backround color. Python 3.12 code: import tkinter as tk from tkinter import ttk, messagebox, font import sqlite3 import sys
if sys.platform.startswith('win'): try: import ctypes ctypes.windll.shcore.SetProcessDpiAwareness(1) except Exception as e: print("Error setting DPI awareness:", e)
def run_query(query, parameters=()): with sqlite3.connect('media.db') as conn: cursor = conn.cursor() result = cursor.execute(query, parameters) if query.strip().lower().startswith("select"): return result.fetchall() conn.commit()
def create_tables(): create_movies_table_sql = ''' CREATE TABLE IF NOT EXISTS movies ( id INTEGER PRIMARY KEY AUTOINCREMENT, movie TEXT, year TEXT, present BOOLEAN, watched BOOLEAN, notes TEXT ); ''' create_series_table_sql = ''' CREATE TABLE IF NOT EXISTS series ( id INTEGER PRIMARY KEY AUTOINCREMENT, series TEXT, seasons TEXT, year TEXT, present BOOLEAN, seasons_present TEXT, complete BOOLEAN, watched BOOLEAN, seasons_seen TEXT, notes TEXT ); ''' run_query(create_movies_table_sql) run_query(create_series_table_sql)
class MediaTracker(tk.Tk): def init(self): super().init()
self.configure(bg='white')
if sys.platform.startswith('win'): try: scaling = self.winfo_fpixels('1i') / 72 self.tk.call('tk', 'scaling', scaling) except Exception as e: print("Error setting scaling:", e)
self.title("Movies and Series Tracker") self.geometry("1200x600")
try: self.custom_font = font.Font(family='Literata', size=12) except: self.custom_font = font.Font(family='Arial', size=12)
font_line_space = self.custom_font.metrics('linespace') desired_padding = 4 row_height = font_line_space + desired_padding
style = ttk.Style() style.theme_use("default")
style.layout( 'Custom.Treeview', [('Custom.Treeview.treearea', {'sticky': 'nsew'})] ) style.configure( "Custom.Treeview", font=self.custom_font, rowheight=row_height, borderwidth=0, relief='flat', highlightthickness=0, background='white', fieldbackground='white' )
style.configure( "Custom.Treeview.Heading", font=( self.custom_font.cget("family"), self.custom_font.cget("size"), "bold" ), background='white', relief='solid', borderwidth=1, anchor='center' ) style.map( "Custom.Treeview.Heading", background=[('active', 'white')], relief=[('active', 'solid')] )
style.configure("Custom.TButton", font=self.custom_font)
self.sidebar_bg = '#F7F8FC'
self.grid_rowconfigure(1, weight=1) self.grid_columnconfigure(2, weight=1)
self.header = tk.Frame(self, bg='white', height=50) self.header.grid(row=0, column=1, columnspan=3, sticky='ew') self.header.grid_propagate(False)
self.header_label = tk.Label( self.header, text="Movies", bg='white', font=self.custom_font, anchor='w', padx=10 ) self.header_label.pack(fill='x', pady=10)
self.sidebar = tk.Frame(self, width=150, bg=self.sidebar_bg) self.sidebar.grid(row=0, column=0, rowspan=2, sticky='ns') self.sidebar.grid_propagate(False)
self.spacer = tk.Frame(self, width=60, bg='white') self.spacer.grid(row=1, column=1, sticky='ns') self.spacer.grid_propagate(False)
self.content = tk.Frame(self, bg='white') self.content.grid(row=1, column=2, sticky='nsew')
self.button_bar = tk.Frame(self, width=50, bg='white') self.button_bar.grid(row=1, column=3, sticky='ns') self.button_bar.grid_propagate(False)
self.create_sidebar_label("Movies", self.show_movies) self.create_sidebar_label("Series", self.show_series)
self.add_button = ttk.Button( self.button_bar, text="+", command=self.add_entry, width=3, style="Custom.TButton" ) self.add_button.pack(pady=20)
self.delete_button = ttk.Button( self.button_bar, text="-", command=self.delete_entry, width=3, style="Custom.TButton" ) self.delete_button.pack(pady=10)
self.tree = None self.scrollbar = None self.current_table = None self.current_row_id = None
self.boolean_columns = { 'movies': ['Present', 'Watched'], 'series': ['Present', 'Complete', 'Watched'] }
self.show_movies()
def create_sidebar_label(self, text, command): label = tk.Label( self.sidebar, text=text, bg=self.sidebar_bg, font=self.custom_font, cursor="hand2", anchor='w', padx=10, pady=5 ) label.pack(pady=10, padx=0, fill='x') label.bind("", lambda e: label.config(bg='#EAEDEF')) label.bind("", lambda e: label.config(bg=self.sidebar_bg)) label.bind("", lambda e: command())
def clear_content(self): if self.tree: self.tree.destroy() self.tree = None if self.scrollbar: self.scrollbar.destroy() self.scrollbar = None
def setup_treeview(self, columns, headings): self.tree = ttk.Treeview( self.content, columns=columns, show='headings', selectmode='browse', style="Custom.Treeview" ) for col, head in zip(columns, headings): self.tree.heading(col, text=head, anchor='center') if head.lower() == 'notes': self.tree.column(col, width=300, anchor='w') elif head in self.boolean_columns.get(self.current_table, []): self.tree.column(col, width=100, anchor='center') else: self.tree.column(col, width=150, anchor='center')
self.scrollbar = ttk.Scrollbar( self.content, orient="vertical", command=self.tree.yview ) self.tree.configure(yscrollcommand=self.scrollbar.set) self.scrollbar.pack(side='right', fill='y') self.tree.pack(fill='both', expand=True)
self.tree.bind('<>', self.on_row_select) self.tree.bind('', self.on_double_click) self.tree.bind('', self.on_single_click) self.tree.bind("", self.treeview_click_cursor)
def treeview_click_cursor(self, event): region = self.tree.identify("region", event.x, event.y) if region == "cell": column = self.tree.identify_column(event.x) try: col_index = int(column.replace('#', '')) - 1 column_name = self.tree['columns'][col_index] if column_name in self.boolean_columns.get(self.current_table, []): self.tree.configure(cursor="hand2") else: self.tree.configure(cursor="") except (IndexError, ValueError): self.tree.configure(cursor="") else: self.tree.configure(cursor="")
def load_movie_data(self): self.clear_content() columns = ["Movie", "Year", "Present", "Watched", "Notes"] headings = ["Movie", "Year", "Present", "Watched", "Notes"] self.setup_treeview(columns, headings) self.current_table = 'movies' self.header_label.config(text="Movies") for row in run_query('SELECT * FROM movies'): record_id = row[0] movie, year, present, watched, notes = row[1], row[2], row[3], row[4], row[5] display_row = [ movie, year, self.bool_to_checkbox(present), self.bool_to_checkbox(watched), notes ] self.tree.insert('', 'end', iid=str(record_id), values=display_row)
def load_series_data(self): self.clear_content() columns = [ "Series", "Seasons", "Year", "Present", "Seasons Present", "Complete", "Watched", "Seasons Seen", "Notes" ] headings = [ "Series", "Seasons", "Year", "Present", "Seasons Present", "Complete", "Watched", "Seasons Seen", "Notes" ] self.setup_treeview(columns, headings) self.current_table = 'series' self.header_label.config(text="Series") for row in run_query('SELECT * FROM series'): record_id = row[0] ( series, seasons, year, present, seasons_present, complete, watched, seasons_seen, notes ) = row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9] display_row = [ series, seasons, year, self.bool_to_checkbox(present), seasons_present, self.bool_to_checkbox(complete), self.bool_to_checkbox(watched), seasons_seen, notes ] self.tree.insert('', 'end', iid=str(record_id), values=display_row)
def show_movies(self): self.load_movie_data()
def show_series(self): self.load_series_data()
def add_entry(self): if self.current_table == 'movies': run_query( "INSERT INTO movies (movie, year, present, watched, notes) " "VALUES (?, ?, ?, ?, ?)", ('', '', False, False, '') ) self.load_movie_data() elif self.current_table == 'series': run_query( """INSERT INTO series (series, seasons, year, present, seasons_present, complete, watched, seasons_seen, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)""", ('', '', '', False, '', False, False, '', '') ) self.load_series_data()
def delete_entry(self): selected_item = self.tree.selection() if not selected_item: messagebox.showwarning("No selection", "Please select a row to delete.") return
row_id = selected_item[0] if self.current_table == 'movies': run_query("DELETE FROM movies WHERE id = ?", (row_id,)) self.load_movie_data() elif self.current_table == 'series': run_query("DELETE FROM series WHERE id = ?", (row_id,)) self.load_series_data()
def on_row_select(self, event): selected_item = self.tree.selection() if selected_item: self.current_row_id = selected_item[0] else: self.current_row_id = None
def on_single_click(self, event): region = self.tree.identify("region", event.x, event.y) if region != "cell": return row_id = self.tree.identify_row(event.y) column = self.tree.identify_column(event.x) if not row_id or not column: return try: col_index = int(column.replace('#', '')) - 1 except ValueError: return if col_index < 0 or col_index >= len(self.tree['columns']): return column_name = self.tree['columns'][col_index] if self.current_table and column_name in self.boolean_columns.get(self.current_table, []): current_value = self.tree.set(row_id, column_name) new_value = not self.checkbox_to_bool(current_value) self.tree.set(row_id, column_name, self.bool_to_checkbox(new_value)) row_values = self.tree.item(row_id)['values'] self.update_database(row_values)
def on_double_click(self, event): item = self.tree.identify_row(event.y) column = self.tree.identify_column(event.x) if not item: return try: col_num = int(column.replace('#', '')) - 1 except ValueError: return if ( (self.current_table == 'movies' and self.tree['columns'][col_num] in self.boolean_columns['movies']) or (self.current_table == 'series' and self.tree['columns'][col_num] in self.boolean_columns['series']) ): return if col_num < 0 or col_num >= len(self.tree['columns']): return column_name = self.tree['columns'][col_num] value = self.tree.set(item, column_name) if self.current_table and column_name in self.boolean_columns.get(self.current_table, []): return bbox = self.tree.bbox(item, column) if not bbox: return x, y, width, height = bbox entry = tk.Entry( self.content, font=self.custom_font, relief='flat', borderwidth=0, highlightthickness=0, bg='white' ) entry.place(x=x, y=y, width=width, height=height) entry.insert(0, value) entry.focus()
def save_edit(event_inner): new_value = entry.get() entry.destroy() self.tree.set(item, column_name, new_value) row_values = self.tree.item(item)['values'] self.update_database(row_values)
entry.bind('', save_edit) entry.bind('', save_edit)
def update_database(self, row_values): if self.current_table == 'movies': present = self.checkbox_to_bool(row_values[2]) watched = self.checkbox_to_bool(row_values[3]) query = ( "UPDATE movies SET movie = ?, year = ?, present = ?, " "watched = ?, notes = ? WHERE id = ?" ) parameters = ( row_values[0], row_values[1], present, watched, row_values[4], self.current_row_id ) elif self.current_table == 'series': present = self.checkbox_to_bool(row_values[3]) complete = self.checkbox_to_bool(row_values[5]) watched = self.checkbox_to_bool(row_values[6]) query = ( """UPDATE series SET series = ?, seasons = ?, year = ?, present = ?, seasons_present = ?, complete = ?, watched = ?, seasons_seen = ?, notes = ? WHERE id = ?""" ) parameters = ( row_values[0], row_values[1], row_values[2], present, row_values[4], complete, watched, row_values[7], row_values[8], self.current_row_id ) run_query(query, parameters)
@staticmethod def bool_to_checkbox(value): return '☑' if value else '☐'
@staticmethod def checkbox_to_bool(value): return True if value == '☑' else False
if name == "main": create_tables() app = MediaTracker() app.mainloop()
- Assistant
To change the border color of the
Treeviewheader to#ededee, you need to adjust the style configuration for theTreeview.Headingin yourttk.Style()setup. You can do this by modifying thestyle.configuremethod for"Custom.Treeview.Heading"to set thebordercoloroption. Note that some options might not be directly supported depending on the platform and the theme you are using, but you can attempt to customize it like this:Here's the portion of your code that you need to modify to achieve this:
style.configure( "Custom.Treeview.Heading", font=( self.custom_font.cget("family"), self.custom_font.cget("size"), "bold" ), background='white', relief='solid', borderwidth=1, bordercolor='#ededee', # Set custom border color anchor='center' )However, keep in mind that
bordercoloror color-related options may not be explicitly available in all themes or platforms when usingttk. If settingbordercolordoesn’t work as expected because it's not supported by your current theme, consider alternative methods such as adding a frame around the header or experimenting with different themes that allow more customization.For a strictly
ttkapproach wherebordercolormight not be directly modifiable, one way to handle such limitations is by hacking around it, though this might not be straightforward and has limitations depending on the native styling of the operating system'sttkwidgets. - User
Use the following information to accomplish the task: How do I change the border color of a tkinter widget? Asked 4 years, 9 months ago Modified 4 years, 9 months ago Viewed 4k times -1
I've been trying to configure the border colour of a widget in Tkinter, and I was wondering how to do that....
I've checked on StackOverflow, and it says that I should use the configure option and then set highlightbackgroundcolor = {insert color here}. I've tried that and nothing has worked. Can someone please show me a working sample of code so I can figure it out?
pythontkinterwidgetttkShare Improve this question Follow asked Jan 4, 2020 at 17:14 siddsp 12744 silver badges1111 bronze badges
stackoverflow.com/questions/4320725/…. This says highlightbackground not highlightbackgroundcolor – sshashank124 Commented Jan 4, 2020 at 17:19 @sshashank124 the highlight color is not the same as the border color. – Bryan Oakley Commented Jan 4, 2020 at 17:29Add a comment 1 Answer Sorted by: 5
There is no way to change the border color of a widget, the border color is tied to the background color of the widget. Instead, you can turn off the border, and then use a frame widget where you can set the background color of the frame.
screenshot
import tkinter as tk
root = tk.Tk()
label_border = tk.Frame(root, background="red") label = tk.Label(label_border, text="This has a red border", bd=0) label.pack(fill="both", expand=True, padx=1, pady=1 )
label_border.pack(padx=20, pady=20)
root.mainloop()
Share Improve this answer Follow answered Jan 4, 2020 at 17:42 Bryan Oakley's user avatar Bryan Oakley 384k5353 gold badges566566 silver badges722722 bronze badges
Thank you. What is the reason for the difference between the background and the border? – doofx Commented Sep 29, 2020 at 11:15 If the border was the same color as the background, you wouldn't be able to see it. – Bryan Oakley Commented Sep 29, 2020 at 13:50 i want to change only the border / highlight color. is it possible? – doofx Commented Oct 1, 2020 at 2:44I still don't understand why it's not possible. changing only the border could be useful – doofx Commented Oct 2, 2020 at 4:15 @doofx: it's not possible because the widget simply doesn't have any options or functions to do it. – Bryan Oakley Commented Oct 2, 2020 at 13:48
- Assistant
The conversation you provided highlights a common limitation with Tkinter: the border color of a widget is often tied to the background color or cannot be changed directly. However, a workaround is to use a
Framewidget to create a border effect, giving you more control over the appearance by setting the background color of theFrame.Here’s how you could apply this technique to the
Treeviewwidget to simulate changing the border color of the header:import tkinter as tk from tkinter import ttk class App(tk.Tk): def __init__(self): super().__init__() # Create a frame to act as a border with the desired color border_color = "#ededee" self.title("Treeview Header Border Example") # Create a frame for the Treeview header header_frame = tk.Frame(self, background=border_color) header_frame.pack(padx=1, pady=1, fill='x') # Create the Treeview inside the header frame tree = ttk.Treeview(header_frame, columns=('A', 'B', 'C'), show='headings') tree.pack(fill='both', expand=True) for col in tree['columns']: tree.heading(col, text=col) # Inserting some sample data tree.insert('', 'end', values=('Data A1', 'Data B1', 'Data C1')) tree.insert('', 'end', values=('Data A2', 'Data B2', 'Data C2')) # Create another frame for the rest of the Treeview (data view) data_frame = tk.Frame(self, background='white') data_frame.pack(padx=1, pady=(0, 1), fill='both', expand=True) # Move Treeview into the data frame tree.master = data_frame tree.pack_forget() tree.pack(fill='both', expand=True) if __name__ == "__main__": app = App() app.mainloop()Explanation:
-
Frames as Borders: We use two
Framewidgets to create a visual separation between different parts of theTreeview. One frame (header_frame) acts as the header, and the other (data_frame) contains the data rows. -
Simulated Border Color: By setting the background color of the
header_frameto#ededee, the header of theTreeviewvisually appears to have a border color of#ededee.
This approach works by visually grouping the
Treeviewinto two frames: one for the top header and one for the data. This adds a colored border-like visual around the headers, effectively achieving the look of a custom border color. -
- User
implemetn this change in the code
- Assistant
To integrate the frame border solution into your existing code, we need to modify the
setup_treeviewmethod in theMediaTrackerclass to include the frames that simulate the border. Here's how you can do it:class MediaTracker(tk.Tk): # ... [other parts of your class remain unchanged] ... def clear_content(self): if self.tree: self.tree.destroy() self.tree = None if self.scrollbar: self.scrollbar.destroy() self.scrollbar = None if self.tree_frame: # Clear tree_frame if it exists self.tree_frame.destroy() self.tree_frame = None def setup_treeview(self, columns, headings): border_color = "#ededee" # Create a frame to act as a border self.tree_frame = tk.Frame(self.content, background=border_color) self.tree_frame.pack(padx=1, pady=(1, 0), fill='both', expand=True) # Create the Treeview inside the border frame self.tree = ttk.Treeview( self.tree_frame, columns=columns, show='headings', selectmode='browse', style="Custom.Treeview" ) for col, head in zip(columns, headings): self.tree.heading(col, text=head, anchor='center') if head.lower() == 'notes': self.tree.column(col, width=300, anchor='w') elif head in self.boolean_columns.get(self.current_table, []): self.tree.column(col, width=100, anchor='center') else: self.tree.column(col, width=150, anchor='center') self.scrollbar = ttk.Scrollbar( self.tree_frame, orient="vertical", command=self.tree.yview ) self.tree.configure(yscrollcommand=self.scrollbar.set) self.scrollbar.pack(side='right', fill='y') self.tree.pack(fill='both', expand=True) # Other bindings for events self.tree.bind('<>', self.on_row_select) self.tree.bind('', self.on_double_click) self.tree.bind('', self.on_single_click) self.tree.bind("", self.treeview_click_cursor) # ... [rest of your class remains unchanged] ... # Start the application if __name__ == "__main__": create_tables() app = MediaTracker() app.mainloop()Key Changes:
- Border Frame: In the
setup_treeviewmethod, a newFrame,tree_frame, is created to simulate the border color withbackground=border_color. - Packing: The new frame is packed first (
self.tree_frame.pack()), and theTreeviewwidget is nested inside this frame (self.tree = ttk.Treeview(self.tree_frame, ...)). - Clear Frame: The
clear_contentmethod is modified to also destroyself.tree_frameif it exists, ensuring that the frame is removed when the content is switched.
This will visually add a border around the
Treeviewheadings with the desired color. - Border Frame: In the
- User
Print the new code in full
- Assistant
Certainly! Below is the updated full code with the changes to simulate a border color around the
Treeviewheadings by using aFrame.import tkinter as tk from tkinter import ttk, messagebox, font import sqlite3 import sys def run_query(query, parameters=()): with sqlite3.connect('media.db') as conn: cursor = conn.cursor() result = cursor.execute(query, parameters) if query.strip().lower().startswith("select"): return result.fetchall() conn.commit() def create_tables(): create_movies_table_sql = ''' CREATE TABLE IF NOT EXISTS movies ( id INTEGER PRIMARY KEY AUTOINCREMENT, movie TEXT, year TEXT, present BOOLEAN, watched BOOLEAN, notes TEXT ); ''' create_series_table_sql = ''' CREATE TABLE IF NOT EXISTS series ( id INTEGER PRIMARY KEY AUTOINCREMENT, series TEXT, seasons TEXT, year TEXT, present BOOLEAN, seasons_present TEXT, complete BOOLEAN, watched BOOLEAN, seasons_seen TEXT, notes TEXT ); ''' run_query(create_movies_table_sql) run_query(create_series_table_sql) class MediaTracker(tk.Tk): def __init__(self): super().__init__() self.configure(bg='white') if sys.platform.startswith('win'): try: scaling = self.winfo_fpixels('1i') / 72 self.tk.call('tk', 'scaling', scaling) except Exception as e: print("Error setting scaling:", e) self.title("Movies and Series Tracker") self.geometry("1200x600") try: self.custom_font = font.Font(family='Literata', size=12) except: self.custom_font = font.Font(family='Arial', size=12) font_line_space = self.custom_font.metrics('linespace') desired_padding = 4 row_height = font_line_space + desired_padding style = ttk.Style() style.theme_use("default") style.layout( 'Custom.Treeview', [('Custom.Treeview.treearea', {'sticky': 'nsew'})] ) style.configure( "Custom.Treeview", font=self.custom_font, rowheight=row_height, borderwidth=0, relief='flat', highlightthickness=0, background='white', fieldbackground='white' ) style.configure( "Custom.Treeview.Heading", font=( self.custom_font.cget("family"), self.custom_font.cget("size"), "bold" ), background='white', relief='solid', borderwidth=1, anchor='center' ) style.map( "Custom.Treeview.Heading", background=[('active', 'white')], relief=[('active', 'solid')] ) style.configure("Custom.TButton", font=self.custom_font) self.sidebar_bg = '#F7F8FC' self.grid_rowconfigure(1, weight=1) self.grid_columnconfigure(2, weight=1) self.header = tk.Frame(self, bg='white', height=50) self.header.grid(row=0, column=1, columnspan=3, sticky='ew') self.header.grid_propagate(False) self.header_label = tk.Label( self.header, text="Movies", bg='white', font=self.custom_font, anchor='w', padx=10 ) self.header_label.pack(fill='x', pady=10) self.sidebar = tk.Frame(self, width=150, bg=self.sidebar_bg) self.sidebar.grid(row=0, column=0, rowspan=2, sticky='ns') self.sidebar.grid_propagate(False) self.spacer = tk.Frame(self, width=60, bg='white') self.spacer.grid(row=1, column=1, sticky='ns') self.spacer.grid_propagate(False) self.content = tk.Frame(self, bg='white') self.content.grid(row=1, column=2, sticky='nsew') self.button_bar = tk.Frame(self, width=50, bg='white') self.button_bar.grid(row=1, column=3, sticky='ns') self.button_bar.grid_propagate(False) self.create_sidebar_label("Movies", self.show_movies) self.create_sidebar_label("Series", self.show_series) self.add_button = ttk.Button( self.button_bar, text="+", command=self.add_entry, width=3, style="Custom.TButton" ) self.add_button.pack(pady=20) self.delete_button = ttk.Button( self.button_bar, text="-", command=self.delete_entry, width=3, style="Custom.TButton" ) self.delete_button.pack(pady=10) self.tree = None self.scrollbar = None self.current_table = None self.current_row_id = None self.tree_frame = None # New frame to act as a border self.boolean_columns = { 'movies': ['Present', 'Watched'], 'series': ['Present', 'Complete', 'Watched'] } self.show_movies() def create_sidebar_label(self, text, command): label = tk.Label( self.sidebar, text=text, bg=self.sidebar_bg, font=self.custom_font, cursor="hand2", anchor='w', padx=10, pady=5 ) label.pack(pady=10, padx=0, fill='x') label.bind("<Enter>", lambda e: label.config(bg='#EAEDEF')) label.bind("<Leave>", lambda e: label.config(bg=self.sidebar_bg)) label.bind("<Button-1>", lambda e: command()) def clear_content(self): if self.tree: self.tree.destroy() self.tree = None if self.scrollbar: self.scrollbar.destroy() self.scrollbar = None if self.tree_frame: self.tree_frame.destroy() self.tree_frame = None def setup_treeview(self, columns, headings): border_color = "#ededee" # Create a frame to simulate a border self.tree_frame = tk.Frame(self.content, background=border_color) self.tree_frame.pack(padx=1, pady=(1, 0), fill='both', expand=True) self.tree = ttk.Treeview( self.tree_frame, columns=columns, show='headings', selectmode='browse', style="Custom.Treeview" ) for col, head in zip(columns, headings): self.tree.heading(col, text=head, anchor='center') if head.lower() == 'notes': self.tree.column(col, width=300, anchor='w') elif head in self.boolean_columns.get(self.current_table, []): self.tree.column(col, width=100, anchor='center') else: self.tree.column(col, width=150, anchor='center') self.scrollbar = ttk.Scrollbar( self.tree_frame, orient="vertical", command=self.tree.yview ) self.tree.configure(yscrollcommand=self.scrollbar.set) self.scrollbar.pack(side='right', fill='y') self.tree.pack(fill='both', expand=True) self.tree.bind('<ButtonRelease-1>', self.on_row_select) self.tree.bind('<Double-1>', self.on_double_click) self.tree.bind('<Button-1>', self.on_single_click) self.tree.bind("<Motion>", self.treeview_click_cursor) def treeview_click_cursor(self, event): region = self.tree.identify("region", event.x, event.y) if region == "cell": column = self.tree.identify_column(event.x) try: col_index = int(column.replace('#', '')) - 1 column_name = self.tree['columns'][col_index] if column_name in self.boolean_columns.get(self.current_table, []): self.tree.configure(cursor="hand2") else: self.tree.configure(cursor="") except (IndexError, ValueError): self.tree.configure(cursor="") else: self.tree.configure(cursor="") def load_movie_data(self): self.clear_content() columns = ["Movie", "Year", "Present", "Watched", "Notes"] headings = ["Movie", "Year", "Present", "Watched", "Notes"] self.setup_treeview(columns, headings) self.current_table = 'movies' self.header_label.config(text="Movies") for row in run_query('SELECT * FROM movies'): record_id = row[0] movie, year, present, watched, notes = row[1], row[2], row[3], row[4], row[5] display_row = [ movie, year, self.bool_to_checkbox(present), self.bool_to_checkbox(watched), notes ] self.tree.insert('', 'end', iid=str(record_id), values=display_row) def load_series_data(self): self.clear_content() columns = [ "Series", "Seasons", "Year", "Present", "Seasons Present", "Complete", "Watched", "Seasons Seen", "Notes" ] headings = [ "Series", "Seasons", "Year", "Present", "Seasons Present", "Complete", "Watched", "Seasons Seen", "Notes" ] self.setup_treeview(columns, headings) self.current_table = 'series' self.header_label.config(text="Series") for row in run_query('SELECT * FROM series'): record_id = row[0] ( series, seasons, year, present, seasons_present, complete, watched, seasons_seen, notes ) = row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9] display_row = [ series, seasons, year, self.bool_to_checkbox(present), seasons_present, self.bool_to_checkbox(complete), self.bool_to_checkbox(watched), seasons_seen, notes ] self.tree.insert('', 'end', iid=str(record_id), values=display_row) def show_movies(self): self.load_movie_data() def show_series(self): self.load_series_data() def add_entry(self): if self.current_table == 'movies': run_query( "INSERT INTO movies (movie, year, present, watched, notes) " "VALUES (?, ?, ?, ?, ?)", ('', '', False, False, '') ) self.load_movie_data() elif self.current_table == 'series': run_query( """INSERT INTO series (series, seasons, year, present, seasons_present, complete, watched, seasons_seen, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)""", ('', '', '', False, '', False, False, '', '') ) self.load_series_data() def delete_entry(self): selected_item = self.tree.selection() if not selected_item: messagebox.showwarning("No selection", "Please select a row to delete.") return row_id = selected_item[0] if self.current_table == 'movies': run_query("DELETE FROM movies WHERE id = ?", (row_id,)) self.load_movie_data() elif self.current_table == 'series': run_query("DELETE FROM series WHERE id = ?", (row_id,)) self.load_series_data() def on_row_select(self, event): selected_item = self.tree.selection() if selected_item: self.current_row_id = selected_item[0] else: self.current_row_id = None def on_single_click(self, event): region = self.tree.identify("region", event.x, event.y) if region != "cell": return row_id = self.tree.identify_row(event.y) column = self.tree.identify_column(event.x) if not row_id or not column: return try: col_index = int(column.replace('#', '')) - 1 except ValueError: return if col_index < 0 or col_index >= len(self.tree['columns']): return column_name = self.tree['columns'][col_index] if self.current_table and column_name in self.boolean_columns.get(self.current_table, []): current_value = self.tree.set(row_id, column_name) new_value = not self.checkbox_to_bool(current_value) self.tree.set(row_id, column_name, self.bool_to_checkbox(new_value)) row_values = self.tree.item(row_id)['values'] self.update_database(row_values) def on_double_click(self, event): item = self.tree.identify_row(event.y) column = self.tree.identify_column(event.x) if not item: return try: col_num = int(column.replace('#', '')) - 1 except ValueError: return if ( (self.current_table == 'movies' and self.tree['columns'][col_num] in self.boolean_columns['movies']) or (self.current_table == 'series' and self.tree['columns'][col_num] in self.boolean_columns['series']) ): return if col_num < 0 or col_num >= len(self.tree['columns']): return column_name = self.tree['columns'][col_num] value = self.tree.set(item, column_name) if self.current_table and column_name in self.boolean_columns.get(self.current_table, []): return bbox = self.tree.bbox(item, column) if not bbox: return x, y, width, height = bbox entry = tk.Entry( self.content, font=self.custom_font, relief='flat', borderwidth=0, highlightthickness=0, bg='white' ) entry.place(x=x, y=y, width=width, height=height) entry.insert(0, value) entry.focus() def save_edit(event_inner): new_value = entry.get() entry.destroy() self.tree.set(item, column_name, new_value) row_values = self.tree.item(item)['values'] self.update_database(row_values) entry.bind('<Return>', save_edit) entry.bind('<FocusOut>', save_edit) def update_database(self, row_values): if self.current_table == 'movies': present = self.checkbox_to_bool(row_values[2]) watched = self.checkbox_to_bool(row_values[3]) query = ( "UPDATE movies SET movie = ?, year = ?, present = ?, " "watched = ?, notes = ? WHERE id = ?" ) parameters = ( row_values[0], row_values[1], present, watched, row_values[4], self.current_row_id ) elif self.current_table == 'series': present = self.checkbox_to_bool(row_values[3]) complete = self.checkbox_to_bool(row_values[5]) watched = self.checkbox_to_bool(row_values[6]) query = ( """UPDATE series SET series = ?, seasons = ?, year = ?, present = ?, seasons_present = ?, complete = ?, watched = ?, seasons_seen = ?, notes = ? WHERE id = ?""" ) parameters = ( row_values[0], row_values[1], row_values[2], present, row_values[4], complete, watched, row_values[7], row_values[8], self.current_row_id ) run_query(query, parameters) @staticmethod def bool_to_checkbox(value): return '☑' if value else '☐' @staticmethod def checkbox_to_bool(value): return True if value == '☑' else False if __name__ == "__main__": create_tables() app = MediaTracker() app.mainloop()Summary of Changes
- Frame as Border: Added
self.tree_frameto act as a border with thebackgroundcolor set to#ededee. - Packing Frame: Used
self.tree_frame.pack()to add it in the layout before placing theTreeviewinside it. - Clear Frame: Ensured the clearing of
self.tree_frameinclear_contentto manage switching views.
This modification keeps the
Treeviewheaders within a visual border defined by the frame’s background color. - Frame as Border: Added
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.