Modify the color of the database header to the following: #ededee. 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")
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 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()
To change the color of the database header to #ededee in your MediaTracker Tkinter application, you'll need to modify several parts of your code:
Treeview Header Background: Update the Treeview's heading background color.
Application Header Frame and Label: Change the background color of the header frame and label if necessary.
Below are the detailed steps and the modified code segments to achieve this.
1. Change the Treeview Header Background
Locate the section in your code where you configure the Custom.Treeview.Heading style. You'll need to update the background property to #ededee. Additionally, update the style.map to ensure the active state also uses the desired color.
style.configure(
"Custom.Treeview.Heading",
font=(
self.custom_font.cget("family"),
self.custom_font.cget("size"),
"bold"
),
background='#ededee', # Updated background color
relief='solid',
borderwidth=1,
anchor='center'
)
style.map(
"Custom.Treeview.Heading",
background=[('active', '#d5d5d5')], # Optional: Change active background color
relief=[('active', 'solid')]
)
Note: The active state background is set to a slightly darker shade (#d5d5d5) for better visibility when hovering. You can adjust this as needed.
2. Update the Application Header Frame and Label
If you also want to change the background color of the application's main header (not just the Treeview headers), update the bg parameter for both the header frame and the header label.
self.header_bg_color = '#ededee' # Define the new header background color
self.header = tk.Frame(self, bg=self.header_bg_color, height=50)
...
self.header_label = tk.Label(
self.header,
text="Movies",
bg=self.header_bg_color, # Updated background color
font=self.custom_font,
anchor='w',
padx=10
)
3. Complete Revised Code Segment
Below is the complete revised MediaTracker class with the necessary modifications to change the header colors:
python
4. Additional Recommendations
Consistent Color Usage: To make color management easier, define all your colors at the beginning of the class or in a separate configuration section. This way, if you need to change a color later, you only need to update it in one place.
python
self.header_bg_color = '#ededee' # Header background
self.sidebar_bg_color = '#F7F8FC' # Sidebar background
self.header_active_color = '#d5d5d5' # Active state for headers
Responsive Design: Ensure that the color changes look good across different operating systems and screen resolutions. Test the application on multiple platforms if possible.
Error Handling: Consider adding more robust error handling, especially around UI elements, to prevent the application from crashing unexpectedly.
By following these modifications, your application's database headers should now reflect the desired color #ededee. Ensure that you test the changes thoroughly to confirm that the appearance meets your expectations across different parts of the application.