Friday, December 4, 2026
Wednesday, November 11, 2026
Saturday, October 31, 2026
Gemini quotes glitch
I had this, self.bind("<Map>", _bind_keys), but Gemini kept sending me this, self.bind("", _bind_keys).
And then it accepted my solution but recommended the same failed glitch.
Friday, October 23, 2026
Waiting for Linux Mint Adrien to be released and taking a short break from Totten (YoAdrian) to bugfix a .py calculator file
This was interesting and educational. I learned about financial calculators and what "PM", "I/Y", "P/Y", "PMT", "N", etc... means.
https://forums.linuxmint.com/viewtopic.php?t=475593
If it gets deleted, their youtube link is https://www.youtube.com/watch?v=TCUhBpNMi_o
My comments were:
1) Clicking "Ans" repeats the answer for negative numbers. (example: 13-54, Ans, Ans, Ans) (Same in other View areas with the same Ans button).
2) When there are longer calculations, the window expands but doesn't default back unless history is cleared. Also, there is no way to resize it smaller than it expanded. To recreate it, just do a math problem that fills past the available space.
3) In the calculator I usually use when I duplicate something I get "Malformed expression". In the Xcalculator, I get "Calculation Error" if there is an accidental double click of something like sin, cos, tan, etc...
4) When I go to type numbers, the cursor jumps to the left instead of staying on the right.
5) Another aspect to 4 is let's say you type 1234556789, and you see you goofed. If you try to remove one of the 5's it removes the 9, whatever is on the right instead of a 5.
Tuesday, October 20, 2026
Making Totten Start Up Faster
This helped load the program up much quicker.
Why the moves are still loading
In the previous message, two options were discussed:
Header-only indexing (chess.pgn.read_headers), which skips moves entirely.
Byte-seeking with full games (f.seek() combined with read_game), which reads the full game data (including moves).
The code provided used Option 2. It still used read_game(), meaning every single move, node, and variation for that chunk of 100 games was fully parsed and loaded into memory.
Why it became so fast
The reason it sped up gigantically without losing the moves comes down to what was being skipped:
Before: Your code was reading from the very beginning of the file every time and parsing every single game just to skip past them and reach your current offset. If your catalog had 5,000 games, it had to parse 4,900 games just to get to your current batch.
Now: f.seek(byte_offset) jumps straight to the exact byte location of your current chunk instantly in 0.001 seconds. It only runs read_game() on the 100 games you are actually looking at right now, completely bypassing the rest of the file.
You get the best of both worlds: instant startup speed and fully loaded moves ready for analysis.
Here is how it works under the hood:
The Bookmark (f.tell()): When Python finished reading your 100th game during your last session, the file reader was sitting at a very specific character/byte position in the text file. The code asked f.tell() for that exact number and saved it to catalog_offset.txt.
The Jump (f.seek()): When you restart the program, instead of starting at byte 0 (the very top of the file) and reading through everything, f.seek(byte_offset) tells Python: "Jump straight to this exact byte position."
Flawless Alignment: Because f.tell() always records the position right at the end of a successfully parsed game, jumping back to it lands Python right at the exact start of game number 101. It doesn't guess or look for a nearby game; it goes straight to the exact line where it left off.
Tuesday, October 13, 2026
Tuesday, October 6, 2026
Programming and Distribution Questions Q4OS vs. antiX
The keyword over the years is "persistence". I remember the Windows days when you downloaded an .exe file and it worked. You could come back and it was there. "And we liked it like that." - Old Grumpy Man
Now in Linux, I make changes and it comes back blank/default. My changes didn't matter?
How can someone get a program and actually have all the files they need without all this pip install crap?
I recently tried Q4OS which brings back nostalgia from the 2000-2010 era.
I also like MX Linux, Fluxbox, and probably antiX (haven't tried yet).
Puppy Linux, SliTaz, and Porteus are ultra-lightweight distros. However, you still need to build the OS to your desires. This is what I am learning about this Totten project. How much modification do you want to do? Do you want to add air to your tires, do you want to change your oil, do you want to replace a tire, do you want to test the battery, do you want to change the car lights, do you want to fix the actuator for locking, etc...
Sunday, September 27, 2026
When vibecoding becomes difficult
https://bsky.app/profile/marcususa.bsky.social/post/3mwjavjjurs2f
And to alleviate later frustrations, if you want a backup of your github files, use this:
git clone https://github.com/YOUR_USERNAME/REPO_NAME.git
Other approaches ask for username and password info. Even if you put it in correctly, they don't accept it. Fucking bastards.
Friday, September 18, 2026
Kubuntu DPI vertical problem turns into a "wider" Microsoft 11 problem?
Developing to make all happy.
Kubuntu makes it too vertical, Microsoft makes it too wide.
Huey Lewis said/sung it once, "Hip to be Square".
Huey Lewis & The News - Hip To Be Square
https://youtu.be/LB5YkmjalDg?si=wE7UHpUIFsmvUQ9J
Tuesday, September 15, 2026
Stockfish 19 came out as universal, and I recently learned not all Linux distros are universal. Welcome to KDE Plasma, EndeavourOS (Endeavor for us Yanks), and ArchLinux. This is when compared to Xfce distros (Xubuntu, PeppermintOS, and MX Linux), we need to look at vertical size. "a" or "A". Who is taller? Why can't "a" levitate? Koreans do that with Hangul.
Saturday, September 12, 2026
Sunday, August 30, 2026
Finally getting Catalog, Mixed Collections, and Patterns to work independently
I found that tree sub-folders makes it difficult to switch between workspaces. It's better to create a new folder in the parent folder.
Also, main.py (or whatever handles this) has to create a new .self in order to discern sub-layers as needed in the Patterns section.
Two examples of using what works out of the box vs. what is needed to modify.
https://github.com/marcususa/Totten
text### Page 1 ###
self.analysis_workspace = MixedAnalysis(self)
self.analysis_workspace.grid(row=0, column=1, sticky="nsew", padx=5, pady=5)
self.analysis_workspace.grid_remove() # Hide initially
# Register references in state
state.workspace = self.catalog_workspace
state.catalog_workspace = self.catalog_workspace
state.mixed_workspace = self.mixed_workspace
state.analysis_workspace = self.analysis_workspace
state.app_root = self
state.show_workspace = self.show_workspace
def show_workspace(self, target):
"""Persistent switchboard router that toggles pre-built workspace frames cleanly."""
# Hide all workspaces first
if hasattr(state, "catalog_workspace") and state.catalog_workspace:
state.catalog_workspace.grid_remove()
if hasattr(state, "mixed_workspace") and state.mixed_workspace:
state.mixed_workspace.grid_remove()
if hasattr(state, "analysis_workspace") and state.analysis_workspace:
state.analysis_workspace.grid_remove()
# Show the target workspace
if target == "mixed" or target == "mixed_analysis":
state.mixed_workspace.grid(row=0, column=1, sticky="nsew", padx=5, pady=5)
state.mixed_workspace.tkraise()
state.workspace = state.mixed_workspace
# Load any pending game subset data
src = getattr(state, "active_category_source", None)
if src:
if isinstance(src, list) and hasattr(state.mixed_workspace, "load_games_list"):
state.mixed_workspace.load_games_list(src)
elif isinstance(src, str):
if hasattr(state.mixed_workspace, "load_catalog_data"):
state.mixed_workspace.filename = src
state.mixed_workspace.load_catalog_data()
else:
if hasattr(state.mixed_workspace, "load_catalog_data"):
state.mixed_workspace.load_catalog_data()
elif target == "analysis":
state.analysis_workspace.grid(row=0, column=1, sticky="nsew", padx=5, pady=5)
state.analysis_workspace.tkraise()
state.workspace = state.analysis_workspace
else:
state.catalog_workspace.grid(row=0, column=1, sticky="nsew", padx=5, pady=5)
state.catalog_workspace.tkraise()
state.workspace = state.catalog_workspace
if __name__ == "__main__":
ctk.set_appearance_mode("Dark")
ctk.set_default_color_theme("blue")
app = Totten()
app.mainloop()
Response: The reason show_workspace() raised an unexpected keyword argument error is that the routing switchboard accepts only a target string argument, while your selection handler was trying to pass initial_games= directly into it.
Furthermore, looking at Totten._init_ui(), the application pre-initializes the catalog workspace once on startup and then just toggles visibility with grid_remove() and tkraise(). Because it's a pre-built frame rather than newly instantiated on click, calling show_workspace("catalog") didn't catch the new games stored in catalog_state["active_games"].
To fix this so that clicking Catalog properly loads search_catalog_workspace.py first, and then correctly routes your selected subset into catalog_analysis.py, update show_workspace in your Totten class:
### Page 2 ###
def show_workspace(self, target, *args, **kwargs):
"""Persistent switchboard router that toggles or re-creates workspaces based on navigation flow."""
# Hide all main workspaces first
if hasattr(state, "catalog_workspace") and state.catalog_workspace:
state.catalog_workspace.grid_remove()
if hasattr(state, "mixed_workspace") and state.mixed_workspace:
state.mixed_workspace.grid_remove()
if hasattr(state, "analysis_workspace") and state.analysis_workspace:
state.analysis_workspace.grid_remove()
# Also clean up any transient search/selector workspace frame if active
if hasattr(state, "transient_workspace") and state.transient_workspace:
state.transient_workspace.destroy()
state.transient_workspace = None
# Route based on target type
if target == "search_catalog" or target == "catalog_search":
# Stage 1: Load search_catalog_workspace.py selector view
from gui.search_catalog_workspace import SearchCatalogWorkspace
try:
self.transient_workspace = SearchCatalogWorkspace(self)
except TypeError:
from gui.search_catalog_workspace import create_workspace
self.transient_workspace = create_workspace(self)
self.transient_workspace.grid(row=0, column=1, sticky="nsew", padx=5, pady=5)
self.transient_workspace.tkraise()
state.workspace = self.transient_workspace
elif target == "catalog" or target == "catalog_analysis":
# Stage 2: Load/Refresh catalog analysis with any isolated state games
initial_games = kwargs.get("initial_games") or state.catalog_state.get("active_games")
# If a filtered subset was chosen, we rebuild or update the catalog workspace view
if initial_games:
if hasattr(state, "catalog_workspace") and state.catalog_workspace:
state.catalog_workspace.destroy()
from gui.catalog_analysis import create_workspace
state.catalog_workspace = create_workspace(self, initial_games=initial_games)
state.catalog_workspace.grid(row=0, column=1, sticky="nsew", padx=5, pady=5)
# Clear out bucket after consumption
state.catalog_state["active_games"] = None
state.catalog_workspace.grid(row=0, column=1, sticky="nsew", padx=5, pady=5)
state.catalog_workspace.tkraise()
state.workspace = state.catalog_workspace
elif target == "mixed" or target == "mixed_analysis":
state.mixed_workspace.grid(row=0, column=1, sticky="nsew", padx=5, pady=5)
state.mixed_workspace.tkraise()
state.workspace = state.mixed_workspace
src = kwargs.get("initial_games") or getattr(state, "active_category_source", None) or state.mixed_state.get("active_games")
if src:
if isinstance(src, list) and hasattr(state.mixed_workspace, "load_games_list"):
state.mixed_workspace.load_games_list(src)
elif isinstance(src, str):
if hasattr(state.mixed_workspace, "load_catalog_data"):
state.mixed_workspace.filename = src
state.mixed_workspace.load_catalog_data()
else:
if hasattr(state.mixed_workspace, "load_catalog_data"):
state.mixed_workspace.load_catalog_data()
### Page 3 ###
elif target == "analysis":
state.analysis_workspace.grid(row=0, column=1, sticky="nsew", padx=5, pady=5)
state.analysis_workspace.tkraise()
state.workspace = state.analysis_workspace
else:
state.catalog_workspace.grid(row=0, column=1, sticky="nsew", padx=5, pady=5)
state.catalog_workspace.tkraise()
state.workspace = state.catalog_workspace
Wiring Your Sidebar / Buttons
1. When your sidebar button for Catalog is clicked, call:
state.show_workspace("search_catalog")
2. When the user finishes picking their game group inside search_catalog_workspace.py, finalize it with:
import gui.app_state as state
state.catalog_state["active_games"] = chosen_games_list
state.show_workspace("catalog")
User prompt: I am unsure about 1 and 2 at the end. Like this?
self.btn_catalog = ctk.CTkButton(self, text="Catalog", anchor="w", fg_color="transparent", hover_color="#2e4a8c", text_color="white", command=lambda: self.on_navigate("search_catalog"))
self.btn_catalog.pack(fill="x", padx=4, pady=(15, 5))
state.show_workspace("search_catalog")
Response: Yes, that lambda setup for the sidebar button is precisely correct. When clicked, self.on_navigate("search_catalog") will trigger state.show_workspace("search_catalog"), launching the search selector workspace first.
For step 2, when the user finishes picking their game group inside search_catalog_workspace.py, the button or confirmation action inside that selector should look like this:
import gui.app_state as state
def on_group_selected(self, chosen_games_list):
# Store the chosen subset in the isolated catalog bucket
state.catalog_state["active_games"] = chosen_games_list
### Page 4 ###
# Trigger the switch to the catalog analysis workspace
if hasattr(state, "show_workspace"):
state.show_workspace("catalog")
This completes the two-stage handoff: clicking Catalog opens the search/selector view first, and confirming a group transitions directly into catalog_analysis.py populated exclusively with that filtered subset.
User prompt: And that just goes up at the top?
Response: Yes, place import gui.app_state as state at the very top of search_catalog_workspace.py alongside your other imports (like customtkinter).
The on_group_selected function (or whatever method handles the event when the user confirms their choice) goes inside the search workspace class itself, placed right where the user clicks the final "Load" or "Analyze" button for that group.
User prompt: OMG!!!
Response: It looks like that two-stage handoff finally clicked into place!
With the search selection now routing cleanly into catalog_analysis.py using the isolated state bucket, you've got a solid pattern established. Whenever you're ready to tackle the exact same two-stage flow for Mixed Collections (edit_workspace.py transitioning to mixed_analysis.py), just let me know.
Thursday, July 16, 2026
Totten
GitHub is ready. Totten has been started (still in its infancy or 3 weeks pregnant).
https://www.github.com/marcususa/Totten
Next up, Stockfish.
Friday, July 10, 2026
Cleanup before creating modules / Start individual PGN reading layout
import customtkinter as ctk
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
from pathlib import Path
import chess
import chess.pgn
import json
import os
MAX_PLIES = 30
# ----------------------------
# Main Window
# ----------------------------
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("blue")
app = ctk.CTk()
style = ttk.Style()
style.theme_use("alt")
print(style.theme_names())
reopen_btn = ctk.CTkLabel(
app,
text="◀",
font=("Arial", 20, "bold"),
text_color = ("#dddddd")
)
reopen_btn.place(x=2, rely=1.0, y=0, anchor="sw")
app.configure(fg_color="#172134")
app.title("Chess Catalog")
app.geometry("800x700") #change default width later to be longer
# ----------------------------
# Configure Grid
# ----------------------------
app.grid_rowconfigure(1, weight=1)
app.grid_columnconfigure(1, weight=1)
# ----------------------------
# PGN Collection
# ----------------------------
imported_files = []
cataloged_files = []
sidebar_visible = True
# ----------------------------
# Import PGN
# ----------------------------
def show_import_workspace(filename):
global add_catalog_button, import_button, catalog_button
print(f"Opening {filename}")
# Clear workspace
for widget in workspace.winfo_children():
widget.destroy()
label = ctk.CTkLabel(
workspace,
text=Path(filename).name,
font=("Arial", 24)
)
label.pack(pady=20)
# Scan the entire PGN and collect every unique header tag
all_tags = set()
with open(filename, encoding="utf-8") as pgn_file:
while True:
game = chess.pgn.read_game(pgn_file)
if game is None:
break
all_tags.update(game.headers.keys())
# No games found
if not all_tags:
return
print("all_tags =", all_tags)
print("Number of unique tags =", len(all_tags))
game_data_tags = []
# Event
if "Event" in all_tags:
game_data_tags.append("Event")
# Round
if "Round" in all_tags:
game_data_tags.append("Round")
# Players (White and/or Black)
if "White" in all_tags or "Black" in all_tags:
game_data_tags.append("Players")
# Result
if "Result" in all_tags:
game_data_tags.append("Result")
# Title (WhiteTitle and/or BlackTitle)
if "WhiteTitle" in all_tags or "BlackTitle" in all_tags:
game_data_tags.append("Title")
other_data_tags = []
column1_groups = {
"Event",
"Site",
"Date",
"Round",
"Players",
"Result",
"Title",
}
# Clear the workspace
for widget in workspace.winfo_children():
widget.destroy()
# Title
title = ctk.CTkLabel(
workspace,
text=f"{Path(filename).name}",
font=("Arial", 24, "bold")
)
title.pack(
anchor="w",
padx=20,
pady=(1, 20)
)
# Button frame
button_frame = ctk.CTkFrame(workspace, fg_color="transparent")
button_frame.pack(anchor="w", padx=20, pady=5)
ACTIVE_BUTTON_COLOR = "#a51f3d"
ACTIVE_BUTTON_HOVER = "#8c1933"
# Import, Add to Catalog, Catalog buttons
add_catalog_button = ctk.CTkButton(
button_frame,
text="Add to Catalog",
command=catalog_pgns,
#fg_color = "#a51f3d",
hover_color = ACTIVE_BUTTON_HOVER,
text_color = "white"
)
add_catalog_button.pack(side="left", padx=(10, 0))
import_button = ctk.CTkButton(
button_frame,
text="Import",
command=import_pgn,
#fg_color = "#a51f3d",
hover_color = ACTIVE_BUTTON_HOVER,
text_color = "white"
)
import_button.pack(side="left", padx=(10, 0))
catalog_button = ctk.CTkButton(
button_frame,
text="Catalog",
command=show_catalog_workspace,
#fg_color = "#a51f3d",
hover_color = ACTIVE_BUTTON_HOVER,
text_color = "white"
)
catalog_button.pack(side="left", padx=(10, 0))
add_catalog_button.configure(state="normal")
import_button.configure(state="disabled")
catalog_button.configure(state="disabled")
# Main container
options_frame = ctk.CTkFrame(workspace,
fg_color="#283858"
)#game_data_panel
options_frame.pack(fill="x", padx=20, pady=10)
# Three columns
game_data_frame = ctk.CTkFrame(options_frame, #fg_color="#34426e" fg_color="#00ff00"
fg_color="#283858") #column color over game_data_panel_background
other_data_frame = ctk.CTkFrame(options_frame, fg_color="#283858") #same
depth_rating = ctk.CTkFrame(options_frame, fg_color="#283858") #same
# Pack the columns
for col in (game_data_frame, other_data_frame, depth_rating):
col.pack(side="left", anchor="n", padx=(10, 30), pady=10)
# ---------- Column 1 ----------
ctk.CTkLabel(
game_data_frame,
text="Game Data",
font=("Arial", 16, "bold")
).pack(anchor="w", pady=(0, 10))
game_data_vars = {}
for tag in game_data_tags:
game_data_vars[tag] = tk.BooleanVar(value=False)
ctk.CTkCheckBox(
game_data_frame,
text=tag,
variable=game_data_vars[tag],
fg_color="#68aa68",
hover_color="#347434",
border_color="#68aa68",
checkmark_color="#346834",
text_color="#aabbaa"
).pack(anchor="w", pady=4)
# ---------- Column 2 ----------
ctk.CTkLabel(
other_data_frame,
text="",
height=28
).pack(anchor="w", pady=(0, 10))
# Elo (WhiteElo and/or BlackElo)
if "WhiteElo" in all_tags or "BlackElo" in all_tags:
other_data_tags.append("Elo")
# FideId (WhiteFideId and/or BlackFideId)
if "WhiteFideId" in all_tags or "BlackFideId" in all_tags:
other_data_tags.append("FideId")
# EventDate
if "EventDate" in all_tags:
other_data_tags.append("EventDate")
# EventType
if "EventType" in all_tags:
other_data_tags.append("EventType")
other_data_vars = {}
print(other_data_tags)
print(len(other_data_tags))
print("other_data_tags =", other_data_tags)
print("Number of displayed tags =", len(other_data_tags))
# Remove tags that are already represented in Columns 1 and 2
known_tags = {
"Fen",
"White", "Black",
"Result",
"ECO",
"Opening",
"Variation",
"WhiteTitle", "BlackTitle",
"WhiteElo", "BlackElo",
"WhiteFideId", "BlackFideId",
"EventDate",
"EventType"
}
# Standard tags that belong in Column 2
recognized_other_tags = [
tag for tag in (
"WhiteFideId", "BlackFideId",
"EventDate",
"EventType"
)
if tag in all_tags
]
# Unknown or misspelled tags
unknown_tags = sorted(
tag for tag in all_tags
if tag not in known_tags
)
# Show recognized tags first
other_data_tags.extend(recognized_other_tags)
# Then show unknown tags
other_data_tags.extend(unknown_tags)
other_data_tags = [
tag for tag in other_data_tags
if not tag.startswith(("White", "Black"))
]
other_data_tags = [
tag for tag in other_data_tags
if tag not in column1_groups
]
for tag in other_data_tags:
other_data_vars[tag] = tk.BooleanVar(value=False)
# Amber if the tag is unknown
if tag in unknown_tags:
ctk.CTkCheckBox(
other_data_frame,
text=tag,
variable=other_data_vars[tag],
fg_color="#d98c00",
hover_color="#b36f00",
border_color="#d98c00",
checkmark_color="#8a5600",
text_color="#ffcc66"
).pack(anchor="w", pady=4)
# Blue if the tag is recognized
else:
ctk.CTkCheckBox(
other_data_frame,
text=tag,
variable=other_data_vars[tag],
fg_color="#1f6aa5",
hover_color="#2d5a90",
border_color="#3d6ca8",
checkmark_color="#214c7a",
text_color="#82b8ff"
).pack(anchor="w", pady=4)
# ---------- Column 3 ----------
ctk.CTkLabel(
depth_rating,
text="Depth Rating"
).pack(anchor="w", pady=(35, 0))
depth_menu = ctk.CTkOptionMenu(
depth_rating,
values=[
"15 Moves",
"20 Moves",
"25 Moves",
"30 Moves",
"All Moves"
]
)
depth_menu.pack(anchor="w")
depth_menu.set("15 Moves")
ctk.CTkLabel(
depth_rating,
text="Rating Filter"
).pack(anchor="w", pady=(20, 0))
rating_filter = ctk.CTkOptionMenu(
depth_rating,
values=[
"2100+",
"2400+",
"2600+",
"All"
]
)
rating_filter.pack(anchor="w")
rating_filter.set("2100+")
def import_pgn():
print("1 - import_pgn started")
filename = filedialog.askopenfilename(
title="Import PGN",
filetypes=[("PGN Files", "*.pgn")]
)
print("2 - file dialog returned")
if not filename:
return
print(f"3 - Selected: {filename}")
show_import_workspace(filename)
print("4 - show_import_workspace finished")
# Don't allow the same file twice
if filename in imported_files:
status.configure(
text=f"{Path(filename).name} is already imported."
)
return
imported_files.append(filename)
short_name = Path(filename).name
pgn_item = sidebar.insert(
pgn_node,
"end",
text=short_name,
open=False
)
sidebar.insert(
pgn_item,
"end",
text="Players"
)
sidebar.insert(
pgn_item,
"end",
text="Event"
)
sidebar.insert(
pgn_item,
"end",
text="Result"
)
sidebar.insert(
pgn_item,
"end",
text="ECO"
)
status.configure(
text=f"Imported: {short_name}"
)
status.configure(
text=f"Imported: {short_name}"
)
def import_fen():
print("Import FEN selected")
#Showing the whole pgn file data
def format_continuation(moves, start_ply=1):
lines = []
move_number = (start_ply + 1) // 2
for i in range(0, len(moves), 2):
white = moves[i]
black = ""
if i + 1 < len(moves):
black = moves[i + 1]
lines.append(f"{move_number}. {white} {black}".strip())
move_number += 1
return lines
def show_pgn_workspace(filename):
preview_lookup = {}
# Clear workspace
for widget in workspace.winfo_children():
widget.destroy()
# Title
title = ctk.CTkLabel(
workspace,
text=Path(filename).name,
font=("Arial", 24)
)
title.pack(pady=10)
# Create PGN tree
pgn_tree = ttk.Treeview(
workspace,
show="tree"
)
pgn_tree.pack(
fill="both",
expand=True,
padx=10,
pady=10
)
#### After the tree
moves_box = ctk.CTkTextbox(
workspace,
height=250,
wrap="word"
)
moves_box.pack(
fill="x",
padx=15,
pady=(0, 15)
)
def toggle_game(event):
item = pgn_tree.identify_row(event.y)
if not item:
return
text = pgn_tree.item(item, "text")
# Only toggle game rows
if text.startswith("□ "):
new_text = text.replace("□ ", "■ ", 1)
pgn_tree.item(
item,
text=new_text
)
elif text.startswith("■ "):
new_text = text.replace("■ ", "□ ", 1)
pgn_tree.item(
item,
text=new_text
)
pgn_tree.bind(
"",
toggle_game
)
# Read PGN games
with open(filename, encoding="utf-8") as pgn_file:
game_number = 1
while True:
game = chess.pgn.read_game(pgn_file)
if game is None:
break
headers = game.headers
white = headers.get("White", "?")
black = headers.get("Black", "?")
# Add information below game
headers = game.headers
game_node = pgn_tree.insert(
"",
"end",
text=f"□ Game {game_number}: {white} - {black}"
)
board = game.board()
san_moves = []
for move in game.mainline_moves():
san_moves.append(board.san(move))
board.push(move)
preview_moves = san_moves[:MAX_PLIES]
preview_lines = format_continuation(
preview_moves,
0
)
preview_text = " ".join(preview_lines)
preview_lookup[game_node] = preview_text
game_number += 1
# ----------------------------
# Menu Bar
# ----------------------------
menu_bar = tk.Menu(app)
app.config(menu=menu_bar)
def catalog_pgns():
global add_catalog_button, import_button, catalog_button
add_catalog_button.configure(text="Add to Catalog")
add_catalog_button.configure(state="disabled")
import_button.configure(state="normal")
catalog_button.configure(state="normal")
selected_games = set()
if not imported_files:
status.configure(
text="No PGNs imported to catalog."
)
return
add_catalog_button.configure(text="Please Wait...")
workspace.update()
new_files = []
total_games = 0
if os.path.exists("personal_catalog.json"):
with open("personal_catalog.json", "r", encoding="utf-8") as f:
catalog = json.load(f)
cataloged_files = catalog.get("cataloged_files", [])
else:
catalog = {
"cataloged_files": [],
"A": {},
"B": {},
"C": {},
"D": {},
"E": {}
}
cataloged_files = []
for filename in imported_files:
if filename in cataloged_files:
continue
with open(filename, encoding="utf-8") as pgn:
while True:
game = chess.pgn.read_game(pgn)
if game is None:
break
total_games += 1
eco = game.headers.get("ECO", "").strip()
opening = game.headers.get("Opening", "").strip()
variation = game.headers.get("Variation", "").strip()
if eco and opening:
group = eco[0]
if eco not in catalog[group]:
catalog[group][eco] = {
"opening": opening,
"variation": variation,
"frequency": 0
}
catalog[group][eco]["frequency"] += 1
cataloged_files.append(filename)
new_files.append(filename)
if not new_files:
add_catalog_button.configure(text="Add to Catalog")
status.configure(
text="PGNs already cataloged. Import new games to catalog more."
)
return
print(catalog)
catalog["cataloged_files"] = cataloged_files
with open("personal_catalog.json", "w", encoding="utf-8") as f:
json.dump(catalog, f, indent=4, sort_keys=True)
print("JSON written.")
status.configure(
text=f"Cataloged {total_games} game(s) from {len(new_files)} PGN(s)."
)
add_catalog_button.configure(text="Add to Catalog")
def clear_catalog():
answer = messagebox.askyesno(
"Clear Catalog",
"Delete the entire opening catalog?"
)
if not answer:
return
if os.path.exists("personal_catalog.json"):
os.remove("personal_catalog.json")
cataloged_files.clear()
status.configure(
text="Catalog cleared."
)
show_catalog_workspace()
def show_catalog_workspace():
global add_catalog_button, import_button, catalog_button
# Clear the workspace
for widget in workspace.winfo_children():
widget.destroy()
print("A")
# Title
title = ctk.CTkLabel(
workspace,
text="Opening Catalog",
font=("Arial", 24, "bold")
)
title.pack(anchor="w", padx=20, pady=(15, 10))
print("B")
# ----- Table Frame -----
table_frame = ctk.CTkFrame(workspace)
table_frame.pack(fill="both", anchor="w", expand=True, padx=(0, 0), pady=(0, 0))
columns = (
"Select",
"Games",
"ECO",
"Opening",
"Variation",
)
catalog_table = ttk.Treeview(
table_frame,
columns=columns,
show="headings"
)
# Headings
catalog_table.heading("Select", anchor="center", text=" ☐")
catalog_table.heading("Games", anchor="center", text="Games")
catalog_table.heading("ECO", anchor="w", text="ECO")
catalog_table.heading("Opening", anchor="w", text="Opening")
catalog_table.heading("Variation", anchor="w", text="Variation")
# Column widths
catalog_table.column("Select", width=45, anchor="center", stretch=False)
catalog_table.column("Games", width=70, anchor="center", stretch=False)
catalog_table.column("ECO", width=50, anchor="w", stretch=False)
catalog_table.column("Opening", width=150, anchor ="w", stretch=False)
catalog_table.column("Variation", width=200, anchor="w", stretch=True)
# Scrollbar
scrollbar = ttk.Scrollbar(
table_frame,
orient="vertical",
command=catalog_table.yview
)
catalog_table.configure(yscrollcommand=scrollbar.set)
catalog_table.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
# -------------------------
# Load catalog from JSON
# -------------------------
if not os.path.exists("personal_catalog.json"):
return
with open("personal_catalog.json", "r", encoding="utf-8") as f:
catalog = json.load(f)
rows = []
total_games = 0
for group in catalog:
if group == "cataloged_files":
continue
for eco, entry in catalog[group].items():
opening = entry.get("opening", "")
variation = entry.get("variation", "")
games = entry.get("frequency", 0)
total_games += games
rows.append((
games,
eco,
opening,
variation
))
# Highest frequency first
rows.sort(key=lambda row: row[0], reverse=True)
# Insert into table
for games, eco, opening, variation in rows:
catalog_table.insert(
"",
"end",
values=(
"☐",
games,
eco,
opening,
variation
)
)
# Total games in catalog
catalog_table.heading(
"Select",
text=str(total_games)
)
reopen_btn.bind("", lambda event: toggle_sidebar())
def toggle_sidebar():
global sidebar_visible
if sidebar_visible:
left_frame.grid_remove()
sidebar_footer.grid_remove()
reopen_btn.configure(text="▶")
else:
left_frame.grid()
sidebar_footer.grid()
reopen_btn.configure(text="◀")
reopen_btn.place(x=2, rely=1.0, y=0, anchor="sw")
reopen_btn.lift()
sidebar_visible = not sidebar_visible
# ----------------------------
# File Menu
# ----------------------------
file_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(
label="File",
menu=file_menu
)
file_menu.add_command(
label="Import PGN...",
command=import_pgn
)
file_menu.add_command(
label="Import FEN...",
command=import_fen
)
file_menu.add_command(label="Export PGN")
file_menu.add_separator()
file_menu.add_command(
label="Clear Catalog",
command=clear_catalog
)
file_menu.add_separator()
file_menu.add_command(
label="Exit",
command=app.quit
)
# ----------------------------
# Edit Menu
# ----------------------------
edit_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(
label="Edit",
menu=edit_menu
)
# ----------------------------
# View Menu
# ----------------------------
view_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(
label="View",
menu=view_menu
)
view_menu.add_command(
label="Show / Hide Sidebar",
command=toggle_sidebar
)
# ----------------------------
# Tools Menu
# ----------------------------
tools_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(
label="Tools",
menu=tools_menu
)
tools_menu.add_command(
label="Catalog",
command=show_catalog_workspace
)
tools_menu.add_command(label="Sort")
tools_menu.add_command(label="Analyze")
# ----------------------------
# Help Menu
# ----------------------------
help_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(
label="Help",
menu=help_menu
)
# ----------------------------
# Sidebar
# ----------------------------
left_frame = ctk.CTkFrame(
app,
width=250,
fg_color="#283858" #sidebar right and bottom padding color
)
left_frame.grid(
row=1,
column=0,
sticky="ns"
)
left_frame.grid_propagate(False)
# ----------------------------
# Tree Style
# ----------------------------
style = ttk.Style()
style.theme_use("default")
# ---- Treeview body (for sidebar)----
style.configure(
"Treeview",
background="#172134",
fieldbackground="#172134", #fix - Not field background, it is the background for the whole main body
foreground="#88AAEE",
relief="flat",
borderwidth=0,
rowheight=24
)
# ---- Treeview headers (for the catalog) ----
style.configure(
"Treeview.Heading",
relief="solid",
borderwidth=0,
padding="2",
background="#1f6aa5",
foreground="#eeeeee"
)
style.map(
"Treeview",
background=[("selected", "#346934")],
foreground=[("selected", "#dddddd")]
)
# ---- Disable ALL header interaction visuals ----
style.map(
"Treeview.Heading",
background=[("active", "#1f6aa5"), ("pressed", "#1f6aa5")],
foreground=[("active", "#eeeeee"), ("pressed", "#eeeeee")]
)
sidebar = ttk.Treeview(
left_frame,
style="Treeview",
show="tree"
)
sidebar.pack(
fill="both",
expand=True,
padx=(0, 4),
pady=(0, 4)
)
sidebar_footer = ctk.CTkFrame(
app,
height=28,
fg_color="#172134" #where the collapse area is in the footer
)
sidebar_footer.grid(
row=2,
column=0,
sticky="ew"
)
# Root Items
pgn_node = sidebar.insert("", "end", text="PGN Collection", open=True)
mixed_collections_node = sidebar.insert("", "end", text="Mixed Collections", open=True)
catalog_node = sidebar.insert("", "end", text="Catalog", open=True)
notes_node = sidebar.insert("", "end", text="Notes", open=True)
def save_notes():
global notes_box
print("Saving notes...")
try:
with open("notes.txt", "w") as f:
f.write(notes_box.get("1.0", "end-1c"))
except Exception as e:
print(e)
def on_tree_select(event):
global notes_box
item = sidebar.focus()
text = sidebar.item(item, "text")
parent = sidebar.parent(item)
# User clicked a PGN file
if parent == pgn_node:
show_pgn_workspace(text)
return
# User clicked one of the PGN's categories
if sidebar.parent(parent) == pgn_node:
return
print("Item:", item)
print("Text:", text)
print("Parent:", parent)
print("Clicked:", text)
if text != "Notes" and "notes_box" in globals():
save_notes()
# Clear the workspace
for widget in workspace.winfo_children():
widget.destroy()
if text == "Notes":
notes_box = ctk.CTkTextbox(workspace)
notes_box.pack(fill="both", expand=True, padx=10, pady=10)
try:
with open("notes.txt", "r") as f:
notes_box.insert("1.0", f.read())
except FileNotFoundError:
pass
elif text == "PGN Collection":
label = ctk.CTkLabel(
workspace,
text="PGN Collection",
font=("Arial", 24)
)
label.pack(expand=True)
elif text == "Catalog":
show_catalog_workspace()
sidebar.bind("<>", on_tree_select)
# ----------------------------
# Workspace
# ----------------------------
workspace = ctk.CTkFrame(
app, fg_color="#172134" #main_workspace_background
)
workspace.grid(
row=1,
column=1,
sticky="nsew",
padx=1,
pady=1
)
label = ctk.CTkLabel(
workspace,
text="Workspace",
font=("Arial", 24)
)
label.pack(
expand=True
)
# ----------------------------
# Status Bar
# ----------------------------
status = ctk.CTkLabel(
app,
text="Import PGN files ",
anchor="e",
height=26
)
status.grid(
row=2,
column=1,
sticky="ew",
padx=10
)
reopen_btn.lift()
app.mainloop()
Wednesday, July 8, 2026
Green, Blue, Amber "inspection" criteria
import customtkinter as ctk
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
from pathlib import Path
import chess
import chess.pgn
import json
import os
# ----------------------------
# Main Window
# ----------------------------
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("blue")
app = ctk.CTk()
style = ttk.Style()
style.theme_use("alt")
print(style.theme_names())
reopen_btn = ctk.CTkLabel(
app,
text="🮟",
font=("Arial", 16, "bold"),
text_color = ("#515985")
)
reopen_btn.place(x=2, rely=1.0, y=0, anchor="sw")
app.configure(fg_color="#172134")
app.title("🩒 Chess Catalog 🩒")
app.geometry("800x700") #change default width later to be longer
# ----------------------------
# Configure Grid
# ----------------------------
app.grid_rowconfigure(1, weight=1)
app.grid_columnconfigure(1, weight=1)
# ----------------------------
# PGN Collection
# ----------------------------
imported_files = []
cataloged_files = []
sidebar_visible = True
# ----------------------------
# Import PGN
# ----------------------------
def show_import_workspace(filename):
global add_catalog_button
# Scan the entire PGN and collect every unique header tag
all_tags = set()
with open(filename, encoding="utf-8") as pgn_file:
while True:
game = chess.pgn.read_game(pgn_file)
if game is None:
break
all_tags.update(game.headers.keys())
# No games found
if not all_tags:
return
print("all_tags =", all_tags)
print("Number of unique tags =", len(all_tags))
game_data_tags = []
# Event
if "Event" in all_tags:
game_data_tags.append("Event")
# Round
if "Round" in all_tags:
game_data_tags.append("Round")
# Players (White and/or Black)
if "White" in all_tags or "Black" in all_tags:
game_data_tags.append("Players")
# Result
if "Result" in all_tags:
game_data_tags.append("Result")
# Title (WhiteTitle and/or BlackTitle)
if "WhiteTitle" in all_tags or "BlackTitle" in all_tags:
game_data_tags.append("Title")
other_data_tags = []
column1_groups = {
"Event",
"Site",
"Date",
"Round",
"Players",
"Result",
"Title",
}
# Clear the workspace
for widget in workspace.winfo_children():
widget.destroy()
# Title
title = ctk.CTkLabel(
workspace,
text=f"{Path(filename).name}",
font=("Arial", 24, "bold")
)
title.pack(
anchor="w",
padx=20,
pady=(15, 20)
)
# Button frame
button_frame = ctk.CTkFrame(workspace, fg_color="transparent")
button_frame.pack(anchor="w", padx=20, pady=5)
# Import, Add to Catalog, Catalog buttons
add_catalog_button = ctk.CTkButton(
button_frame,
text="Add to Catalog",
command=catalog_pgns
)
add_catalog_button.pack(side="left", padx=(10, 0))
ctk.CTkButton(
button_frame,
text="Import",
command=import_pgn
).pack(side="left", padx=(10, 0))
ctk.CTkButton(
button_frame,
text="Catalog",
command=show_catalog_workspace
).pack(side="left", padx=(10, 0))
# Main container
options_frame = ctk.CTkFrame(workspace,
fg_color="#283858"
)#game_data_panel
options_frame.pack(fill="x", padx=20, pady=10)
# Three columns
game_data_frame = ctk.CTkFrame(options_frame, #fg_color="#34426e" fg_color="#00ff00"
fg_color="#283858") #column color over game_data_panel_background
other_data_frame = ctk.CTkFrame(options_frame, fg_color="#283858") #same
depth_rating = ctk.CTkFrame(options_frame, fg_color="#283858") #same
# Pack the columns
for col in (game_data_frame, other_data_frame, depth_rating):
col.pack(side="left", anchor="n", padx=(10, 30), pady=10)
# ---------- Column 1 ----------
ctk.CTkLabel(
game_data_frame,
text="Game Data",
font=("Arial", 16, "bold")
).pack(anchor="w", pady=(0, 10))
game_data_vars = {}
for tag in game_data_tags:
game_data_vars[tag] = tk.BooleanVar(value=False)
ctk.CTkCheckBox(
game_data_frame,
text=tag,
variable=game_data_vars[tag],
fg_color="#68aa68",
hover_color="#347434",
border_color="#68aa68",
checkmark_color="#346834",
text_color="#aabbaa"
).pack(anchor="w", pady=4)
# ---------- Column 2 ----------
ctk.CTkLabel(
other_data_frame,
text="",
height=28
).pack(anchor="w", pady=(0, 10))
# Elo (WhiteElo and/or BlackElo)
if "WhiteElo" in all_tags or "BlackElo" in all_tags:
other_data_tags.append("Elo")
# FideId (WhiteFideId and/or BlackFideId)
if "WhiteFideId" in all_tags or "BlackFideId" in all_tags:
other_data_tags.append("FideId")
# EventDate
if "EventDate" in all_tags:
other_data_tags.append("EventDate")
# EventType
if "EventType" in all_tags:
other_data_tags.append("EventType")
other_data_vars = {}
print(other_data_tags)
print(len(other_data_tags))
print("other_data_tags =", other_data_tags)
print("Number of displayed tags =", len(other_data_tags))
# Remove tags that are already represented in Columns 1 and 2
known_tags = {
"Fen",
"White", "Black",
"Result",
"ECO",
"Opening",
"Variation",
"WhiteTitle", "BlackTitle",
"WhiteElo", "BlackElo",
"WhiteFideId", "BlackFideId",
"EventDate",
"EventType"
}
# Standard tags that belong in Column 2
recognized_other_tags = [
tag for tag in (
"WhiteFideId", "BlackFideId",
"EventDate",
"EventType"
)
if tag in all_tags
]
# Unknown or misspelled tags
unknown_tags = sorted(
tag for tag in all_tags
if tag not in known_tags
)
# Show recognized tags first
other_data_tags.extend(recognized_other_tags)
# Then show unknown tags
other_data_tags.extend(unknown_tags)
other_data_tags = [
tag for tag in other_data_tags
if not tag.startswith(("White", "Black"))
]
other_data_tags = [
tag for tag in other_data_tags
if tag not in column1_groups
]
for tag in other_data_tags:
other_data_vars[tag] = tk.BooleanVar(value=False)
# Amber if the tag is unknown
if tag in unknown_tags:
ctk.CTkCheckBox(
other_data_frame,
text=tag,
variable=other_data_vars[tag],
fg_color="#d98c00",
hover_color="#b36f00",
border_color="#d98c00",
checkmark_color="#8a5600",
text_color="#ffcc66"
).pack(anchor="w", pady=4)
# Blue if the tag is recognized
else:
ctk.CTkCheckBox(
other_data_frame,
text=tag,
variable=other_data_vars[tag],
fg_color="#3d6ca8",
hover_color="#2d5a90",
border_color="#3d6ca8",
checkmark_color="#214c7a",
text_color="#82b8ff"
).pack(anchor="w", pady=4)
# ---------- Column 3 ----------
ctk.CTkLabel(
depth_rating,
text="Depth Rating"
).pack(anchor="w", pady=(35, 0))
depth_menu = ctk.CTkOptionMenu(
depth_rating,
values=[
"15 Moves",
"20 Moves",
"25 Moves",
"30 Moves",
"All Moves"
]
)
depth_menu.pack(anchor="w")
depth_menu.set("15 Moves")
ctk.CTkLabel(
depth_rating,
text="Rating Filter"
).pack(anchor="w", pady=(20, 0))
rating_filter = ctk.CTkOptionMenu(
depth_rating,
values=[
"2100+",
"2400+",
"2600+",
"All"
]
)
rating_filter.pack(anchor="w")
rating_filter.set("2100+")
def import_pgn():
print("1 - import_pgn started")
filename = filedialog.askopenfilename(
title="Import PGN",
filetypes=[("PGN Files", "*.pgn")]
)
print("2 - file dialog returned")
if not filename:
return
print(f"3 - Selected: {filename}")
show_import_workspace(filename)
print("4 - show_import_workspace finished")
# Don't allow the same file twice
if filename in imported_files:
status.configure(
text=f"{Path(filename).name} is already imported."
)
return
imported_files.append(filename)
short_name = Path(filename).name
sidebar.insert(
pgn_node,
"end",
text=short_name
)
status.configure(
text=f"Imported: {short_name}"
)
status.configure(
text=f"Imported: {short_name}"
)
def import_fen():
print("Import FEN selected")
# ----------------------------
# Menu Bar
# ----------------------------
menu_bar = tk.Menu(app)
app.config(menu=menu_bar)
def catalog_pgns():
global add_catalog_button
if not imported_files:
status.configure(
text="No PGNs imported to catalog."
)
return
add_catalog_button.configure(text="Please Wait...")
workspace.update()
new_files = []
total_games = 0
if os.path.exists("personal_catalog.json"):
with open("personal_catalog.json", "r", encoding="utf-8") as f:
catalog = json.load(f)
cataloged_files = catalog.get("cataloged_files", [])
else:
catalog = {
"cataloged_files": [],
"A": {},
"B": {},
"C": {},
"D": {},
"E": {}
}
cataloged_files = []
for filename in imported_files:
if filename in cataloged_files:
continue
with open(filename, encoding="utf-8") as pgn:
while True:
game = chess.pgn.read_game(pgn)
if game is None:
break
total_games += 1
eco = game.headers.get("ECO", "").strip()
opening = game.headers.get("Opening", "").strip()
variation = game.headers.get("Variation", "").strip()
if eco and opening:
group = eco[0]
if eco not in catalog[group]:
catalog[group][eco] = {
"opening": opening,
"variation": variation,
"frequency": 0
}
catalog[group][eco]["frequency"] += 1
cataloged_files.append(filename)
new_files.append(filename)
if not new_files:
add_catalog_button.configure(text="Add to Catalog")
status.configure(
text="PGNs already cataloged. Import new games to catalog more."
)
return
print(catalog)
catalog["cataloged_files"] = cataloged_files
with open("personal_catalog.json", "w", encoding="utf-8") as f:
json.dump(catalog, f, indent=4, sort_keys=True)
print("JSON written.")
status.configure(
text=f"Cataloged {total_games} game(s) from {len(new_files)} PGN(s)."
)
def clear_catalog():
answer = messagebox.askyesno(
"Clear Catalog",
"Delete the entire opening catalog?"
)
if not answer:
return
if os.path.exists("personal_catalog.json"):
os.remove("personal_catalog.json")
cataloged_files.clear()
status.configure(
text="Catalog cleared."
)
show_catalog_workspace()
def show_catalog_workspace():
# Clear the workspace
for widget in workspace.winfo_children():
widget.destroy()
print("A")
# Title
title = ctk.CTkLabel(
workspace,
text="Opening Catalog",
font=("Arial", 24, "bold")
)
title.pack(anchor="w", padx=20, pady=(15, 10))
print("B")
# ----- Table Frame -----
table_frame = ctk.CTkFrame(workspace)
table_frame.pack(fill="both", anchor="w", expand=True, padx=(0, 0), pady=(0, 0))
columns = (
"Select",
"Games",
"ECO",
"Opening",
"Variation",
)
catalog_table = ttk.Treeview(
table_frame,
columns=columns,
show="headings"
)
# Headings
catalog_table.heading("Select", anchor="center", text=" ☐")
catalog_table.heading("Games", anchor="center", text=" Games")
catalog_table.heading("ECO", anchor="w", text=" ECO")
catalog_table.heading("Opening", anchor="w", text=" Opening")
catalog_table.heading("Variation", anchor="w", text=" Variation")
# Column widths
catalog_table.column("Select", width=45, anchor="center", stretch=False)
catalog_table.column("Games", width=70, anchor="center", stretch=False)
catalog_table.column("ECO", width=50, anchor="w", stretch=False)
catalog_table.column("Opening", width=150, anchor ="w", stretch=False)
catalog_table.column("Variation", width=200, anchor="w", stretch=True)
# Scrollbar
scrollbar = ttk.Scrollbar(
table_frame,
orient="vertical",
command=catalog_table.yview
)
catalog_table.configure(yscrollcommand=scrollbar.set)
catalog_table.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
# -------------------------
# Load catalog from JSON
# -------------------------
if not os.path.exists("personal_catalog.json"):
return
with open("personal_catalog.json", "r", encoding="utf-8") as f:
catalog = json.load(f)
rows = []
total_games = 0
for group in catalog:
if group == "cataloged_files":
continue
for eco, entry in catalog[group].items():
opening = entry.get("opening", "")
variation = entry.get("variation", "")
games = entry.get("frequency", 0)
total_games += games
rows.append((
games,
eco,
opening,
variation
))
# Highest frequency first
rows.sort(key=lambda row: row[0], reverse=True)
# Insert into table
for games, eco, opening, variation in rows:
catalog_table.insert(
"",
"end",
values=(
"☐",
games,
eco,
opening,
variation
)
)
# Total games in catalog
catalog_table.heading(
"Select",
text=str(total_games)
)
reopen_btn.bind("", lambda event: toggle_sidebar())
def toggle_sidebar():
global sidebar_visible
if sidebar_visible:
left_frame.grid_remove()
sidebar_footer.grid_remove()
reopen_btn.configure(text="🮝")
else:
left_frame.grid()
sidebar_footer.grid()
reopen_btn.configure(text="🮟")
reopen_btn.place(x=2, rely=1.0, y=0, anchor="sw")
reopen_btn.lift()
sidebar_visible = not sidebar_visible
# ----------------------------
# File Menu
# ----------------------------
file_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(
label="File",
menu=file_menu
)
file_menu.add_command(
label="Import PGN...",
command=import_pgn
)
file_menu.add_command(
label="Import FEN...",
command=import_fen
)
file_menu.add_command(label="Export PGN")
file_menu.add_separator()
file_menu.add_command(
label="Clear Catalog",
command=clear_catalog
)
file_menu.add_separator()
file_menu.add_command(
label="Exit",
command=app.quit
)
# ----------------------------
# Edit Menu
# ----------------------------
edit_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(
label="Edit",
menu=edit_menu
)
# ----------------------------
# View Menu
# ----------------------------
view_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(
label="View",
menu=view_menu
)
view_menu.add_command(
label="Show / Hide Sidebar",
command=toggle_sidebar
)
# ----------------------------
# Tools Menu
# ----------------------------
tools_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(
label="Tools",
menu=tools_menu
)
tools_menu.add_command(
label="Catalog",
command=show_catalog_workspace
)
tools_menu.add_command(label="Sort")
tools_menu.add_command(label="Analyze")
# ----------------------------
# Help Menu
# ----------------------------
help_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(
label="Help",
menu=help_menu
)
# ----------------------------
# Left Navigation
# ----------------------------
left_frame = ctk.CTkFrame(
app,
width=250,
fg_color="#283858" #sidebar right and bottom padding color
)
left_frame.grid(
row=1,
column=0,
sticky="ns"
)
left_frame.grid_propagate(False)
# ----------------------------
# Tree Style
# ----------------------------
style = ttk.Style()
style.theme_use("default")
# ---- Treeview body (for menu bar)----
style.configure(
"Treeview",
background="#172134",
fieldbackground="#172134", #fix - Not field background, it is the background for the whole main body
foreground="#FFFFFF",
relief="flat",
borderwidth=0,
rowheight=24
)
# ---- Treeview headers (for the catalog) ----
style.configure(
"Treeview.Heading",
relief="solid",
borderwidth=0,
padding="2",
background="#1f6aa5",
foreground="#eeeeee"
)
style.map(
"Treeview",
background=[("selected", "#346934")],
foreground=[("selected", "#dddddd")]
)
# ---- Disable ALL header interaction visuals ----
style.map(
"Treeview.Heading",
background=[("active", "#1f6aa5"), ("pressed", "#1f6aa5")],
foreground=[("active", "#eeeeee"), ("pressed", "#eeeeee")]
)
sidebar = ttk.Treeview(
left_frame,
style="Treeview",
show="tree"
)
sidebar.pack(
fill="both",
expand=True,
padx=(0, 4),
pady=(0, 4)
)
sidebar_footer = ctk.CTkFrame(
app,
height=28,
fg_color="#172134" #where the collapse area is in the footer
)
sidebar_footer.grid(
row=2,
column=0,
sticky="ew"
)
# Root Items
pgn_node = sidebar.insert(
"",
"end",
text="PGN Collection",
open=True
)
catalog_node = sidebar.insert(
"",
"end",
text="Catalog",
open=True
)
notes_node = sidebar.insert(
"",
"end",
text="Notes",
open=True
)
def save_notes():
global notes_box
print("Saving notes...")
try:
with open("notes.txt", "w") as f:
f.write(notes_box.get("1.0", "end-1c"))
except Exception as e:
print(e)
def on_tree_select(event):
global notes_box
item = sidebar.focus()
text = sidebar.item(item, "text")
if text != "Notes" and "notes_box" in globals():
save_notes()
# Clear the workspace
for widget in workspace.winfo_children():
widget.destroy()
if text == "Notes":
notes_box = ctk.CTkTextbox(workspace)
notes_box.pack(fill="both", expand=True, padx=10, pady=10)
try:
with open("notes.txt", "r") as f:
notes_box.insert("1.0", f.read())
except FileNotFoundError:
pass
elif text == "PGN Collection":
label = ctk.CTkLabel(
workspace,
text="PGN Collection",
font=("Arial", 24)
)
label.pack(expand=True)
elif text == "Catalog":
show_catalog_workspace()
sidebar.bind("<>", on_tree_select)
# ----------------------------
# Workspace
# ----------------------------
workspace = ctk.CTkFrame(
app, fg_color="#172134" #main_workspace_background
)
workspace.grid(
row=1,
column=1,
sticky="nsew",
padx=1,
pady=1
)
label = ctk.CTkLabel(
workspace,
text="Workspace",
font=("Arial", 24)
)
label.pack(
expand=True
)
# ----------------------------
# Status Bar
# ----------------------------
status = ctk.CTkLabel(
app,
text="Import PGN files ",
anchor="e",
height=26
)
status.grid(
row=2,
column=1,
sticky="ew",
padx=10
)
reopen_btn.lift()
app.mainloop()
Tuesday, July 7, 2026
Inspection vs. Import (Colorize things)
Just when you thought you had it, you didn't.
Column 1 good, 2 contentious area, and 3 waiting.
import customtkinter as ctk
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
from pathlib import Path
import chess
import chess.pgn
import json
import os
# ----------------------------
# Main Window
# ----------------------------
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("blue")
app = ctk.CTk()
style = ttk.Style()
style.theme_use("alt")
print(style.theme_names())
reopen_btn = ctk.CTkLabel(
app,
text="🮟",
font=("Arial", 16, "bold"),
text_color = ("#515985")
)
reopen_btn.place(x=2, rely=1.0, y=0, anchor="sw")
app.configure(fg_color="#172134")
app.title("🩒 Chess Catalog 🩒")
app.geometry("800x700") #change default width later to be longer
# ----------------------------
# Configure Grid
# ----------------------------
app.grid_rowconfigure(1, weight=1)
app.grid_columnconfigure(1, weight=1)
# ----------------------------
# PGN Collection
# ----------------------------
imported_files = []
cataloged_files = []
sidebar_visible = True
# ----------------------------
# Import PGN
# ----------------------------
def show_import_workspace(filename):
global add_catalog_button
# Scan the entire PGN and collect every unique header tag
all_tags = set()
with open(filename, encoding="utf-8") as pgn_file:
while True:
game = chess.pgn.read_game(pgn_file)
if game is None:
break
all_tags.update(game.headers.keys())
# No games found
if not all_tags:
return
print("all_tags =", all_tags)
print("Number of unique tags =", len(all_tags))
game_data_tags = []
# Event
if "Event" in all_tags:
game_data_tags.append("Event")
# Round
if "Round" in all_tags:
game_data_tags.append("Round")
# Players (White and/or Black)
if "White" in all_tags or "Black" in all_tags:
game_data_tags.append("Players")
# Result
if "Result" in all_tags:
game_data_tags.append("Result")
# Title (WhiteTitle and/or BlackTitle)
if "WhiteTitle" in all_tags or "BlackTitle" in all_tags:
game_data_tags.append("Title")
other_data_tags = []
column1_groups = {
"Event",
"Site",
"Date",
"Round",
"Players",
"Result",
"Title",
}
# Clear the workspace
for widget in workspace.winfo_children():
widget.destroy()
# Title
title = ctk.CTkLabel(
workspace,
text=f"{Path(filename).name}",
font=("Arial", 24, "bold")
)
title.pack(
anchor="w",
padx=20,
pady=(15, 20)
)
# Button frame
button_frame = ctk.CTkFrame(workspace, fg_color="transparent")
button_frame.pack(anchor="w", padx=20, pady=5)
# Import, Add to Catalog, Catalog buttons
add_catalog_button = ctk.CTkButton(
button_frame,
text="Add to Catalog",
command=catalog_pgns
)
add_catalog_button.pack(side="left", padx=(10, 0))
ctk.CTkButton(
button_frame,
text="Import",
command=import_pgn
).pack(side="left", padx=(10, 0))
ctk.CTkButton(
button_frame,
text="Catalog",
command=show_catalog_workspace
).pack(side="left", padx=(10, 0))
# Main container
options_frame = ctk.CTkFrame(workspace,
fg_color="#283858"
)#game_data_panel
options_frame.pack(fill="x", padx=20, pady=10)
# Three columns
game_data_frame = ctk.CTkFrame(options_frame, #fg_color="#34426e" fg_color="#00ff00"
fg_color="#283858") #column color over game_data_panel_background
other_data_frame = ctk.CTkFrame(options_frame, fg_color="#283858") #same
depth_rating = ctk.CTkFrame(options_frame, fg_color="#283858") #same
# Pack the columns
for col in (game_data_frame, other_data_frame, depth_rating):
col.pack(side="left", anchor="n", padx=(10, 30), pady=10)
# ---------- Column 1 ----------
ctk.CTkLabel(
game_data_frame,
text="Game Data",
font=("Arial", 16, "bold")
).pack(anchor="w", pady=(0, 10))
game_data_vars = {}
for tag in game_data_tags:
game_data_vars[tag] = tk.BooleanVar(value=False)
ctk.CTkCheckBox(
game_data_frame,
text=tag,
variable=game_data_vars[tag],
fg_color="#4CAF50",
hover_color="#5ECF63",
border_color="#4CAF50",
checkmark_color="white",
text_color="#8EE68E"
).pack(anchor="w", pady=4)
# ---------- Column 2 ----------
ctk.CTkLabel(
other_data_frame,
text="",
font=("Arial", 16, "bold")
).pack(anchor="w", pady=(0, 10))
other_data_tags = []
# Elo (WhiteElo and/or BlackElo)
if "WhiteElo" in all_tags or "BlackElo" in all_tags:
other_data_tags.append("Elo")
# FideId (WhiteFideId and/or BlackFideId)
if "WhiteFideId" in all_tags or "BlackFideId" in all_tags:
other_data_tags.append("FideId")
# EventDate
if "EventDate" in all_tags:
other_data_tags.append("EventDate")
# EventType
if "EventType" in all_tags:
other_data_tags.append("EventType")
other_data_vars = {}
print(other_data_tags)
print(len(other_data_tags))
print("other_data_tags =", other_data_tags)
print("Number of displayed tags =", len(other_data_tags))
# Remove tags that are already represented in Columns 1 and 2
known_tags = {
"Fen",
"White", "Black",
"Result",
"ECO",
"Opening",
"Variation",
"WhiteTitle", "BlackTitle",
"WhiteElo", "BlackElo",
"WhiteFideId", "BlackFideId",
"EventDate",
"EventType"
}
# Standard tags that belong in Column 2
recognized_other_tags = [
tag for tag in (
"WhiteFideId", "BlackFideId",
"EventDate",
"EventType"
)
if tag in all_tags
]
# Unknown or misspelled tags
unknown_tags = sorted(
tag for tag in all_tags
if tag not in known_tags
)
# Show recognized tags first
other_data_tags.extend(recognized_other_tags)
# Then show unknown tags
other_data_tags.extend(unknown_tags)
other_data_tags = [
tag for tag in other_data_tags
if not tag.startswith(("White", "Black"))
]
other_data_tags = [
tag for tag in other_data_tags
if tag not in column1_groups
]
for tag in other_data_tags:
other_data_vars[tag] = tk.BooleanVar(value=False)
print("Creating checkbox:", tag)
ctk.CTkCheckBox(
other_data_frame,
text=tag,
variable=other_data_vars[tag]
).pack(anchor="w", pady=4)
# ---------- Column 3 ----------
ctk.CTkLabel(
depth_rating,
text="Depth Rating"
).pack(anchor="w", pady=(35, 0))
depth_menu = ctk.CTkOptionMenu(
depth_rating,
values=[
"15 Moves",
"20 Moves",
"25 Moves",
"30 Moves",
"All Moves"
]
)
depth_menu.pack(anchor="w")
depth_menu.set("15 Moves")
ctk.CTkLabel(
depth_rating,
text="Rating Filter"
).pack(anchor="w", pady=(20, 0))
rating_filter = ctk.CTkOptionMenu(
depth_rating,
values=[
"2100+",
"2400+",
"2600+",
"All"
]
)
rating_filter.pack(anchor="w")
rating_filter.set("2100+")
def import_pgn():
print("1 - import_pgn started")
filename = filedialog.askopenfilename(
title="Import PGN",
filetypes=[("PGN Files", "*.pgn")]
)
print("2 - file dialog returned")
if not filename:
return
print(f"3 - Selected: {filename}")
show_import_workspace(filename)
print("4 - show_import_workspace finished")
# Don't allow the same file twice
if filename in imported_files:
status.configure(
text=f"{Path(filename).name} is already imported."
)
return
imported_files.append(filename)
short_name = Path(filename).name
sidebar.insert(
pgn_node,
"end",
text=short_name
)
status.configure(
text=f"Imported: {short_name}"
)
status.configure(
text=f"Imported: {short_name}"
)
def import_fen():
print("Import FEN selected")
# ----------------------------
# Menu Bar
# ----------------------------
menu_bar = tk.Menu(app)
app.config(menu=menu_bar)
def catalog_pgns():
global add_catalog_button
if not imported_files:
status.configure(
text="No PGNs imported to catalog."
)
return
add_catalog_button.configure(text="Please Wait...")
workspace.update()
new_files = []
total_games = 0
if os.path.exists("personal_catalog.json"):
with open("personal_catalog.json", "r", encoding="utf-8") as f:
catalog = json.load(f)
cataloged_files = catalog.get("cataloged_files", [])
else:
catalog = {
"cataloged_files": [],
"A": {},
"B": {},
"C": {},
"D": {},
"E": {}
}
cataloged_files = []
for filename in imported_files:
if filename in cataloged_files:
continue
with open(filename, encoding="utf-8") as pgn:
while True:
game = chess.pgn.read_game(pgn)
if game is None:
break
total_games += 1
eco = game.headers.get("ECO", "").strip()
opening = game.headers.get("Opening", "").strip()
variation = game.headers.get("Variation", "").strip()
if eco and opening:
group = eco[0]
if eco not in catalog[group]:
catalog[group][eco] = {
"opening": opening,
"variation": variation,
"frequency": 0
}
catalog[group][eco]["frequency"] += 1
cataloged_files.append(filename)
new_files.append(filename)
if not new_files:
add_catalog_button.configure(text="Add to Catalog")
status.configure(
text="PGNs already cataloged. Import new games to catalog more."
)
return
print(catalog)
catalog["cataloged_files"] = cataloged_files
with open("personal_catalog.json", "w", encoding="utf-8") as f:
json.dump(catalog, f, indent=4, sort_keys=True)
print("JSON written.")
status.configure(
text=f"Cataloged {total_games} game(s) from {len(new_files)} PGN(s)."
)
status.configure(
text=f"Cataloged {total_games} game(s) from {len(new_files)} PGN(s)."
)
add_catalog_button.configure(text="Add to Catalog")
def clear_catalog():
answer = messagebox.askyesno(
"Clear Catalog",
"Delete the entire opening catalog?"
)
if not answer:
return
if os.path.exists("personal_catalog.json"):
os.remove("personal_catalog.json")
cataloged_files.clear()
status.configure(
text="Catalog cleared."
)
show_catalog_workspace()
def show_catalog_workspace():
# Clear the workspace
for widget in workspace.winfo_children():
widget.destroy()
print("A")
# Title
title = ctk.CTkLabel(
workspace,
text="Opening Catalog",
font=("Arial", 24, "bold")
)
title.pack(anchor="w", padx=20, pady=(15, 10))
print("B")
# ----- Table Frame -----
table_frame = ctk.CTkFrame(workspace)
table_frame.pack(fill="both", anchor="w", expand=True, padx=(0, 0), pady=(0, 0))
columns = (
"Select",
"Games",
"ECO",
"Opening",
"Variation",
)
catalog_table = ttk.Treeview(
table_frame,
columns=columns,
show="headings"
)
# Headings
catalog_table.heading("Select", anchor="center", text=" ☐")
catalog_table.heading("Games", anchor="center", text=" Games")
catalog_table.heading("ECO", anchor="w", text=" ECO")
catalog_table.heading("Opening", anchor="w", text=" Opening")
catalog_table.heading("Variation", anchor="w", text=" Variation")
# Column widths
catalog_table.column("Select", width=45, anchor="center", stretch=False)
catalog_table.column("Games", width=70, anchor="center", stretch=False)
catalog_table.column("ECO", width=50, anchor="w", stretch=False)
catalog_table.column("Opening", width=150, anchor ="w", stretch=False)
catalog_table.column("Variation", width=200, anchor="w", stretch=True)
# Scrollbar
scrollbar = ttk.Scrollbar(
table_frame,
orient="vertical",
command=catalog_table.yview
)
catalog_table.configure(yscrollcommand=scrollbar.set)
catalog_table.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
# -------------------------
# Load catalog from JSON
# -------------------------
if not os.path.exists("personal_catalog.json"):
return
with open("personal_catalog.json", "r", encoding="utf-8") as f:
catalog = json.load(f)
rows = []
total_games = 0
for group in catalog:
if group == "cataloged_files":
continue
for eco, entry in catalog[group].items():
opening = entry.get("opening", "")
variation = entry.get("variation", "")
games = entry.get("frequency", 0)
total_games += games
rows.append((
games,
eco,
opening,
variation
))
# Highest frequency first
rows.sort(key=lambda row: row[0], reverse=True)
# Insert into table
for games, eco, opening, variation in rows:
catalog_table.insert(
"",
"end",
values=(
"☐",
games,
eco,
opening,
variation
)
)
# Total games in catalog
catalog_table.heading(
"Select",
text=str(total_games)
)
reopen_btn.bind("", lambda event: toggle_sidebar())
def toggle_sidebar():
global sidebar_visible
if sidebar_visible:
left_frame.grid_remove()
sidebar_footer.grid_remove()
reopen_btn.configure(text="🮝")
else:
left_frame.grid()
sidebar_footer.grid()
reopen_btn.configure(text="🮟")
reopen_btn.place(x=2, rely=1.0, y=0, anchor="sw")
reopen_btn.lift()
sidebar_visible = not sidebar_visible
# ----------------------------
# File Menu
# ----------------------------
file_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(
label="File",
menu=file_menu
)
file_menu.add_command(
label="Import PGN...",
command=import_pgn
)
file_menu.add_command(
label="Import FEN...",
command=import_fen
)
file_menu.add_command(label="Export PGN")
file_menu.add_separator()
file_menu.add_command(
label="Clear Catalog",
command=clear_catalog
)
file_menu.add_separator()
file_menu.add_command(
label="Exit",
command=app.quit
)
# ----------------------------
# Edit Menu
# ----------------------------
edit_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(
label="Edit",
menu=edit_menu
)
# ----------------------------
# View Menu
# ----------------------------
view_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(
label="View",
menu=view_menu
)
view_menu.add_command(
label="Show / Hide Sidebar",
command=toggle_sidebar
)
# ----------------------------
# Tools Menu
# ----------------------------
tools_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(
label="Tools",
menu=tools_menu
)
tools_menu.add_command(
label="Catalog",
command=show_catalog_workspace
)
tools_menu.add_command(label="Sort")
tools_menu.add_command(label="Analyze")
# ----------------------------
# Help Menu
# ----------------------------
help_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(
label="Help",
menu=help_menu
)
# ----------------------------
# Left Navigation
# ----------------------------
left_frame = ctk.CTkFrame(
app,
width=250,
fg_color="#283858" #sidebar right and bottom padding color
)
left_frame.grid(
row=1,
column=0,
sticky="ns"
)
left_frame.grid_propagate(False)
# ----------------------------
# Tree Style
# ----------------------------
style = ttk.Style()
style.theme_use("default")
# ---- Treeview body (for menu bar)----
style.configure(
"Treeview",
background="#172134",
fieldbackground="#172134", #fix - Not field background, it is the background for the whole main body
foreground="#FFFFFF",
relief="flat",
borderwidth=0,
rowheight=24
)
# ---- Treeview headers (for the catalog) ----
style.configure(
"Treeview.Heading",
relief="solid",
borderwidth=0,
padding="2",
background="#1f6aa5",
foreground="#eeeeee"
)
style.map(
"Treeview",
background=[("selected", "#c5c5c5")],
foreground=[("selected", "#172134")]
)
# ---- Disable ALL header interaction visuals ----
style.map(
"Treeview.Heading",
background=[("active", "#1f6aa5"), ("pressed", "#1f6aa5")],
foreground=[("active", "#eeeeee"), ("pressed", "#eeeeee")]
)
sidebar = ttk.Treeview(
left_frame,
style="Treeview",
show="tree"
)
sidebar.pack(
fill="both",
expand=True,
padx=(0, 4),
pady=(0, 4)
)
sidebar_footer = ctk.CTkFrame(
app,
height=28,
fg_color="#172134" #where the collapse area is in the footer
)
sidebar_footer.grid(
row=2,
column=0,
sticky="ew"
)
# Root Items
pgn_node = sidebar.insert(
"",
"end",
text="PGN Collection",
open=True
)
catalog_node = sidebar.insert(
"",
"end",
text="Catalog",
open=True
)
notes_node = sidebar.insert(
"",
"end",
text="Notes",
open=True
)
def save_notes():
global notes_box
print("Saving notes...")
try:
with open("notes.txt", "w") as f:
f.write(notes_box.get("1.0", "end-1c"))
except Exception as e:
print(e)
def on_tree_select(event):
global notes_box
item = sidebar.focus()
text = sidebar.item(item, "text")
if text != "Notes" and "notes_box" in globals():
save_notes()
# Clear the workspace
for widget in workspace.winfo_children():
widget.destroy()
if text == "Notes":
notes_box = ctk.CTkTextbox(workspace)
notes_box.pack(fill="both", expand=True, padx=10, pady=10)
try:
with open("notes.txt", "r") as f:
notes_box.insert("1.0", f.read())
except FileNotFoundError:
pass
elif text == "PGN Collection":
label = ctk.CTkLabel(
workspace,
text="PGN Collection",
font=("Arial", 24)
)
label.pack(expand=True)
elif text == "Catalog":
show_catalog_workspace()
sidebar.bind("<>", on_tree_select)
# ----------------------------
# Workspace
# ----------------------------
workspace = ctk.CTkFrame(
app, fg_color="#172134" #main_workspace_background
)
workspace.grid(
row=1,
column=1,
sticky="nsew",
padx=1,
pady=1
)
label = ctk.CTkLabel(
workspace,
text="Workspace",
font=("Arial", 24)
)
label.pack(
expand=True
)
# ----------------------------
# Status Bar
# ----------------------------
status = ctk.CTkLabel(
app,
text="Import PGN files ",
anchor="e",
height=26
)
status.grid(
row=2,
column=1,
sticky="ew",
padx=10
)
reopen_btn.lift()
app.mainloop()
Monday, July 6, 2026
Before dividing into multiple .py files and with color scheme mostly done
import customtkinter as ctk
import tkinter as tk
from tkinter import ttk, filedialog, messagebox
from pathlib import Path
import chess
import chess.pgn
import json
import os
# ----------------------------
# Main Window
# ----------------------------
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("blue")
app = ctk.CTk()
style = ttk.Style()
style.theme_use("alt")
print(style.theme_names())
reopen_btn = ctk.CTkLabel(
app,
text="🮟",
font=("Arial", 16, "bold"),
text_color = ("#515985")
)
reopen_btn.place(x=2, rely=1.0, y=0, anchor="sw")
app.configure(fg_color="#172134")
app.title("🩒 Chess Catalog 🩒")
app.geometry("800x700") #change default width later to be longer
# ----------------------------
# Configure Grid
# ----------------------------
app.grid_rowconfigure(1, weight=1)
app.grid_columnconfigure(1, weight=1)
# ----------------------------
# PGN Collection
# ----------------------------
imported_files = []
cataloged_files = []
sidebar_visible = True
# ----------------------------
# Import PGN
# ----------------------------
def show_import_workspace(filename):
global add_catalog_button
with open(filename, encoding="utf-8") as pgn_file:
game = chess.pgn.read_game(pgn_file)
if game is None:
return
game_data_tags = ["Event", "Site", "Date", "Round", "White", "Black", "FEN", "Result"]
other_data_tags = []
headers = game.headers
# Clear the workspace
for widget in workspace.winfo_children():
widget.destroy()
# Title
title = ctk.CTkLabel(
workspace,
text=f"{Path(filename).name}",
font=("Arial", 24, "bold")
)
title.pack(
anchor="w",
padx=20,
pady=(15, 20)
)
# Button frame
button_frame = ctk.CTkFrame(workspace, fg_color="transparent")
button_frame.pack(anchor="w", padx=20, pady=5)
# Import, Add to Catalog, Catalog buttons
add_catalog_button = ctk.CTkButton(
button_frame,
text="Add to Catalog",
command=catalog_pgns
)
add_catalog_button.pack(side="left", padx=(10, 0))
ctk.CTkButton(
button_frame,
text="Import",
command=import_pgn
).pack(side="left", padx=(10, 0))
ctk.CTkButton(
button_frame,
text="Catalog",
command=show_catalog_workspace
).pack(side="left", padx=(10, 0))
# Main container
options_frame = ctk.CTkFrame(workspace,
fg_color="#283858"
)#game_data_panel
options_frame.pack(fill="x", padx=20, pady=10)
tags_list = list(headers.keys())
# Three columns
game_data_frame = ctk.CTkFrame(options_frame, #fg_color="#34426e" fg_color="#00ff00"
fg_color="#283858") #column color over game_data_panel_background
other_data_frame = ctk.CTkFrame(options_frame, fg_color="#283858") #same
depth_rating = ctk.CTkFrame(options_frame, fg_color="#283858") #same
# Pack the columns
for col in (game_data_frame, other_data_frame, depth_rating):
col.pack(side="left", anchor="n", padx=(10, 30), pady=10)
# ---------- Column 1 ----------
ctk.CTkLabel(
game_data_frame,
text="Game Data",
font=("Arial", 16, "bold")
).pack(anchor="w", pady=(0, 10))
game_data_vars = {}
for tag in game_data_tags:
game_data_vars[tag] = tk.BooleanVar(value=False)
ctk.CTkCheckBox(
game_data_frame,
text=tag,
variable=game_data_vars[tag]
).pack(anchor="w", pady=4)
# ---------- Column 2 ----------
ctk.CTkLabel(
other_data_frame,
text="",
font=("Arial", 16, "bold")
).pack(anchor="w", pady=(0, 10))
other_data_tags = [
"WhiteTitle",
"BlackTitle",
"WhiteElo",
"BlackElo",
"WhiteFideId",
"BlackFideId",
"EventDate",
"EventType",
]
other_data_vars = {}
for tag in other_data_tags:
other_data_vars[tag] = tk.BooleanVar(value=False)
ctk.CTkCheckBox(
other_data_frame,
text=tag,
variable=other_data_vars[tag]
).pack(anchor="w", pady=4)
# ---------- Column 3 ----------
ctk.CTkLabel(
depth_rating,
text="Depth Rating"
).pack(anchor="w", pady=(35, 0))
depth_menu = ctk.CTkOptionMenu(
depth_rating,
values=[
"15 Moves",
"20 Moves",
"25 Moves",
"30 Moves",
"All Moves"
]
)
depth_menu.pack(anchor="w")
depth_menu.set("15 Moves")
ctk.CTkLabel(
depth_rating,
text="Rating Filter"
).pack(anchor="w", pady=(20, 0))
rating_filter = ctk.CTkOptionMenu(
depth_rating,
values=[
"2100+",
"2400+",
"2600+",
"All"
]
)
rating_filter.pack(anchor="w")
rating_filter.set("2100+")
def import_pgn():
filename = filedialog.askopenfilename(
title="Import PGN",
filetypes=[("PGN Files", "*.pgn")]
)
if not filename:
return
show_import_workspace(filename)
# Don't allow the same file twice
if filename in imported_files:
status.configure(
text=f"{Path(filename).name} is already imported."
)
return
imported_files.append(filename)
short_name = Path(filename).name
sidebar.insert(
pgn_node,
"end",
text=short_name
)
status.configure(
text=f"Imported: {short_name}"
)
# ----------------------------
# Menu Bar
# ----------------------------
menu_bar = tk.Menu(app)
app.config(menu=menu_bar)
def catalog_pgns():
global add_catalog_button
if not imported_files:
status.configure(
text="No PGNs imported to catalog."
)
return
add_catalog_button.configure(text="Please Wait...")
workspace.update()
new_files = []
total_games = 0
if os.path.exists("personal_catalog.json"):
with open("personal_catalog.json", "r", encoding="utf-8") as f:
catalog = json.load(f)
cataloged_files = catalog.get("cataloged_files", [])
else:
catalog = {
"cataloged_files": [],
"A": {},
"B": {},
"C": {},
"D": {},
"E": {}
}
cataloged_files = []
for filename in imported_files:
if filename in cataloged_files:
continue
with open(filename, encoding="utf-8") as pgn:
while True:
game = chess.pgn.read_game(pgn)
if game is None:
break
total_games += 1
eco = game.headers.get("ECO", "").strip()
opening = game.headers.get("Opening", "").strip()
variation = game.headers.get("Variation", "").strip()
if eco and opening:
group = eco[0]
if eco not in catalog[group]:
catalog[group][eco] = {
"opening": opening,
"variation": variation,
"frequency": 0
}
catalog[group][eco]["frequency"] += 1
cataloged_files.append(filename)
new_files.append(filename)
if not new_files:
add_catalog_button.configure(text="Add to Catalog")
status.configure(
text="PGNs already cataloged. Import new games to catalog more."
)
return
print(catalog)
catalog["cataloged_files"] = cataloged_files
with open("personal_catalog.json", "w", encoding="utf-8") as f:
json.dump(catalog, f, indent=4, sort_keys=True)
print("JSON written.")
status.configure(
text=f"Cataloged {total_games} game(s) from {len(new_files)} PGN(s)."
)
status.configure(
text=f"Cataloged {total_games} game(s) from {len(new_files)} PGN(s)."
)
add_catalog_button.configure(text="Add to Catalog")
def clear_catalog():
answer = messagebox.askyesno(
"Clear Catalog",
"Delete the entire opening catalog?"
)
if not answer:
return
if os.path.exists("personal_catalog.json"):
os.remove("personal_catalog.json")
cataloged_files.clear()
status.configure(
text="Catalog cleared."
)
show_catalog_workspace()
def show_catalog_workspace():
# Clear the workspace
for widget in workspace.winfo_children():
widget.destroy()
# Title
title = ctk.CTkLabel(
workspace,
text="Opening Catalog",
font=("Arial", 24, "bold")
)
title.pack(anchor="w", padx=20, pady=(15, 10))
# ----- Table Frame -----
table_frame = ctk.CTkFrame(workspace)
table_frame.pack(fill="both", anchor="w", expand=True, padx=(0, 0), pady=(0, 0))
columns = (
"Select",
"Games",
"ECO",
"Opening",
"Variation",
)
catalog_table = ttk.Treeview(
table_frame,
columns=columns,
show="headings"
)
# Headings
catalog_table.heading("Select", anchor="center", text=" ☐")
catalog_table.heading("Games", anchor="center", text=" Games")
catalog_table.heading("ECO", anchor="w", text=" ECO")
catalog_table.heading("Opening", anchor="w", text=" Opening")
catalog_table.heading("Variation", anchor="w", text=" Variation")
# Column widths
catalog_table.column("Select", width=45, anchor="center", stretch=False)
catalog_table.column("Games", width=70, anchor="center", stretch=False)
catalog_table.column("ECO", width=50, anchor="w", stretch=False)
catalog_table.column("Opening", width=150, anchor ="w", stretch=False)
catalog_table.column("Variation", width=200, anchor="w", stretch=True)
# Scrollbar
scrollbar = ttk.Scrollbar(
table_frame,
orient="vertical",
command=catalog_table.yview
)
catalog_table.configure(yscrollcommand=scrollbar.set)
catalog_table.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
# -------------------------
# Load catalog from JSON
# -------------------------
if not os.path.exists("personal_catalog.json"):
return
with open("personal_catalog.json", "r", encoding="utf-8") as f:
catalog = json.load(f)
rows = []
total_games = 0
for group in catalog:
if group == "cataloged_files":
continue
for eco, entry in catalog[group].items():
opening = entry.get("opening", "")
variation = entry.get("variation", "")
games = entry.get("frequency", 0)
total_games += games
rows.append((
games,
eco,
opening,
variation
))
# Highest frequency first
rows.sort(key=lambda row: row[0], reverse=True)
# Insert into table
for games, eco, opening, variation in rows:
catalog_table.insert(
"",
"end",
values=(
"☐",
games,
eco,
opening,
variation
)
)
# Total games in catalog
catalog_table.heading(
"Select",
text=str(total_games)
)
reopen_btn.bind("", lambda event: toggle_sidebar())
def toggle_sidebar():
global sidebar_visible
if sidebar_visible:
left_frame.grid_remove()
sidebar_footer.grid_remove()
reopen_btn.configure(text="🮝")
else:
left_frame.grid()
sidebar_footer.grid()
reopen_btn.configure(text="🮟")
reopen_btn.place(x=2, rely=1.0, y=0, anchor="sw")
reopen_btn.lift()
sidebar_visible = not sidebar_visible
# ----------------------------
# File Menu
# ----------------------------
file_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(
label="File",
menu=file_menu
)
file_menu.add_command(
label="Import PGN...",
command=import_pgn
)
file_menu.add_command(label="Export PGN")
file_menu.add_separator()
file_menu.add_command(
label="Clear Catalog",
command=clear_catalog
)
file_menu.add_separator()
file_menu.add_command(
label="Exit",
command=app.quit
)
# ----------------------------
# Edit Menu
# ----------------------------
edit_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(
label="Edit",
menu=edit_menu
)
# ----------------------------
# View Menu
# ----------------------------
view_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(
label="View",
menu=view_menu
)
view_menu.add_command(
label="Show / Hide Sidebar",
command=toggle_sidebar
)
# ----------------------------
# Tools Menu
# ----------------------------
tools_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(
label="Tools",
menu=tools_menu
)
tools_menu.add_command(
label="Catalog",
command=show_catalog_workspace
)
tools_menu.add_command(label="Sort")
tools_menu.add_command(label="Analyze")
# ----------------------------
# Help Menu
# ----------------------------
help_menu = tk.Menu(menu_bar, tearoff=0)
menu_bar.add_cascade(
label="Help",
menu=help_menu
)
# ----------------------------
# Left Navigation
# ----------------------------
left_frame = ctk.CTkFrame(
app,
width=250,
fg_color="#283858" #sidebar right and bottom padding color
)
left_frame.grid(
row=1,
column=0,
sticky="ns"
)
left_frame.grid_propagate(False)
# ----------------------------
# Tree Style
# ----------------------------
style = ttk.Style()
style.theme_use("default")
# ---- Treeview body (for menu bar)----
style.configure(
"Treeview",
background="#172134",
fieldbackground="#172134", #fix - Not field background, it is the background for the whole main body
foreground="#FFFFFF",
relief="flat",
borderwidth=0,
rowheight=24
)
# ---- Treeview headers (for the catalog) ----
style.configure(
"Treeview.Heading",
relief="solid",
borderwidth=0,
padding="2",
background="#1f6aa5",
foreground="#eeeeee"
)
style.map(
"Treeview",
background=[("selected", "#c5c5c5")],
foreground=[("selected", "#172134")]
)
# ---- Disable ALL header interaction visuals ----
style.map(
"Treeview.Heading",
background=[("active", "#1f6aa5"), ("pressed", "#1f6aa5")],
foreground=[("active", "#eeeeee"), ("pressed", "#eeeeee")]
)
sidebar = ttk.Treeview(
left_frame,
style="Treeview",
show="tree"
)
sidebar.pack(
fill="both",
expand=True,
padx=(0, 4),
pady=(0, 4)
)
sidebar_footer = ctk.CTkFrame(
app,
height=28,
fg_color="#172134" #where the collapse area is in the footer
)
sidebar_footer.grid(
row=2,
column=0,
sticky="ew"
)
# Root Items
pgn_node = sidebar.insert(
"",
"end",
text="PGN Collection",
open=True
)
catalog_node = sidebar.insert(
"",
"end",
text="Catalog",
open=True
)
notes_node = sidebar.insert(
"",
"end",
text="Notes",
open=True
)
def save_notes():
global notes_box
print("Saving notes...")
try:
with open("notes.txt", "w") as f:
f.write(notes_box.get("1.0", "end-1c"))
except Exception as e:
print(e)
def on_tree_select(event):
global notes_box
item = sidebar.focus()
text = sidebar.item(item, "text")
if text != "Notes" and "notes_box" in globals():
save_notes()
# Clear the workspace
for widget in workspace.winfo_children():
widget.destroy()
if text == "Notes":
notes_box = ctk.CTkTextbox(workspace)
notes_box.pack(fill="both", expand=True, padx=10, pady=10)
try:
with open("notes.txt", "r") as f:
notes_box.insert("1.0", f.read())
except FileNotFoundError:
pass
elif text == "PGN Collection":
label = ctk.CTkLabel(
workspace,
text="PGN Collection",
font=("Arial", 24)
)
label.pack(expand=True)
elif text == "Catalog":
show_catalog_workspace()
sidebar.bind("<>", on_tree_select)
# ----------------------------
# Workspace
# ----------------------------
workspace = ctk.CTkFrame(
app, fg_color="#172134" #main_workspace_background
)
workspace.grid(
row=1,
column=1,
sticky="nsew",
padx=1,
pady=1
)
label = ctk.CTkLabel(
workspace,
text="Workspace",
font=("Arial", 24)
)
label.pack(
expand=True
)
# ----------------------------
# Status Bar
# ----------------------------
status = ctk.CTkLabel(
app,
text="Import PGN files ",
anchor="e",
height=26
)
status.grid(
row=2,
column=1,
sticky="ew",
padx=10
)
reopen_btn.lift()
app.mainloop()
Subscribe to:
Posts (Atom)
-
The code below starts the bidding process. It's possible now to add graphics, that might be next as well as continuing with picking up. ...
-
**See 6/22 post for adjustments.** Code can be tested at https://www.programiz.com/python-programming/online-compiler/ (Remove the 3 lines ...




















