Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import tkinter as tk
- from tkinter import filedialog, messagebox, colorchooser
- class SettingsWindow:
- def __init__(self, root, config_file):
- self.root = root
- self.config_file = config_file
- self.root.title("Settings")
- # Create a Canvas and Scrollbars
- self.canvas = tk.Canvas(self.root)
- self.scrollbar_y = tk.Scrollbar(self.root, orient="vertical", command=self.canvas.yview)
- self.scrollbar_x = tk.Scrollbar(self.root, orient="horizontal", command=self.canvas.xview)
- # Configure the Canvas to be scrollable
- self.scrollable_frame = tk.Frame(self.canvas)
- self.scrollable_frame.bind(
- "<Configure>",
- lambda e: self.canvas.configure(
- scrollregion=self.canvas.bbox("all")
- )
- )
- self.canvas.create_window((0, 0), window=self.scrollable_frame, anchor="nw")
- self.canvas.configure(yscrollcommand=self.scrollbar_y.set, xscrollcommand=self.scrollbar_x.set)
- # Configure the mouse wheel event
- self.canvas.bind_all("<MouseWheel>", self._on_mousewheel)
- # Place the Canvas and Scrollbars in the window
- self.canvas.grid(row=0, column=0, sticky="nsew")
- self.scrollbar_y.grid(row=0, column=1, sticky="ns")
- self.scrollbar_x.grid(row=1, column=0, sticky="ew")
- # Configure window expansion
- self.root.grid_rowconfigure(0, weight=1)
- self.root.grid_columnconfigure(0, weight=1)
- # Variables to store values
- self.launchbox_path = tk.StringVar(value="C:/LaunchBox")
- self.alternative_path = tk.StringVar(value="D:/LaunchBox")
- self.image_cache_ratio_w = tk.StringVar(value="200")
- self.image_cache_ratio_h = tk.StringVar(value="200")
- self.text_cell_w = tk.StringVar(value="150")
- self.text_cell_h = tk.StringVar(value="50")
- self.image_cell_w = tk.StringVar(value="200")
- self.image_cell_h = tk.StringVar(value="200")
- self.max_games_per_page_imagemode = tk.StringVar(value="200")
- self.color_media_yes_title = tk.StringVar(value="#80FF00")
- self.color_media_yes_rom = tk.StringVar(value="#80AA00")
- self.color_media_no = tk.StringVar(value="#FF0000")
- self.color_no_trans = tk.StringVar(value="#DBDBDB")
- self.selected_filters = ["Box - Front", "Clear Logo"]
- self.make_cache = tk.BooleanVar()
- # Load current values from the config.txt file
- self.load_config()
- # Graphical interface
- self.setup_ui()
- # Adjust window size to the farthest content without additional margin
- self.root.update_idletasks()
- width = self.scrollable_frame.winfo_reqwidth()
- height = self.scrollable_frame.winfo_reqheight()
- self.root.geometry(f"{width}x{height}")
- # Bind the window resize event
- self.root.bind("<Configure>", self._on_window_resize)
- def _on_mousewheel(self, event):
- """Scroll the Canvas with the mouse wheel."""
- if event.delta:
- self.canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")
- def _on_window_resize(self, event):
- """Update the scroll region when the window is resized."""
- self.canvas.configure(scrollregion=self.canvas.bbox("all"))
- def setup_ui(self):
- """Configure the graphical interface elements."""
- main_frame = tk.Frame(self.scrollable_frame)
- main_frame.pack(fill="both", expand=True, padx=10, pady=10)
- # LaunchBox Path
- tk.Label(main_frame, text="LaunchBox Path:").grid(row=0, column=0, sticky="w")
- self.path_entry = tk.Entry(main_frame, textvariable=self.launchbox_path, width=40)
- self.path_entry.grid(row=0, column=1, sticky="w")
- tk.Button(main_frame, text="Browse", command=self.browse_launchbox_path).grid(row=0, column=2)
- # Selected Filters
- tk.Label(main_frame, text="Selected Filters:").grid(row=1, column=0, sticky="w", pady=(10, 0))
- self.filters = [
- "Arcade - Marquee", "Banner", "Advertisement Flyer - Back", "Advertisement Flyer - Front", "Box - Front",
- "Box - Front - Reconstructed", "Fanart - Box - Front", "Box - 3D", "Box - Back",
- "Box - Back - Reconstructed", "Fanart - Box - Back", "Box - Spine", "Box - Full", "Clear Logo",
- "Cart - Front", "Disc", "Fanart - Cart - Front", "Fanart - Disc", "Cart - Back", "Fanart - Cart - Back",
- "Cart - 3D", "Fanart - Background", "Screenshot - Game Over", "Screenshot - Game Select",
- "Screenshot - Game Title", "Screenshot - Gameplay", "Screenshot - High Scores", "Epic Games Background",
- "Epic Games Poster", "Epic Games Screenshot", "GOG Poster", "GOG Screenshot", "Origin Background",
- "Origin Poster", "Origin Screenshot", "Steam Banner", "Steam Poster", "Steam Screenshot",
- "Uplay Background", "Uplay Thumbnail", "Arcade - Cabinet", "Arcade - Circuit Board",
- "Arcade - Control Panel", "Arcade - Controls Information", "Amazon Background", "Amazon Poster",
- "Amazon Screenshot", "Videos", "Manual"
- ]
- self.filter_vars = []
- num_columns = 3 # Number of columns for filters
- max_filters_per_column = (len(self.filters) // num_columns) + (1 if len(self.filters) % num_columns != 0 else 0)
- for i, filter_name in enumerate(self.filters):
- var = tk.BooleanVar(value=(filter_name in self.selected_filters))
- row = 2 + (i % max_filters_per_column)
- column = i // max_filters_per_column
- tk.Checkbutton(main_frame, text=filter_name, variable=var).grid(row=row, column=column, sticky="w")
- self.filter_vars.append(var)
- # Adjust the width of the scrollable_frame to the widest content
- self.scrollable_frame.update_idletasks()
- self.canvas.configure(scrollregion=self.canvas.bbox("all"))
- row_offset = 2 + max_filters_per_column
- # Add a line break
- tk.Label(main_frame, text="").grid(row=row_offset, column=0)
- row_offset += 1
- # Maximum games listed per page
- tk.Label(main_frame, text="Maximum games listed per page:").grid(row=row_offset, column=0, sticky="w")
- self.max_games_entry = tk.Entry(main_frame, textvariable=self.max_games_per_page_imagemode, width=5)
- self.max_games_entry.grid(row=row_offset, column=1, padx=(0, 0))
- row_offset += 1
- # Add a line break
- tk.Label(main_frame, text="").grid(row=row_offset, column=0)
- row_offset += 1
- # Cell size in Text mode
- self.create_size_input(main_frame, "Cell Size in Text Mode", row_offset, self.text_cell_w, self.text_cell_h)
- row_offset += 1
- # Cell size in Image mode
- self.create_size_input(main_frame, "Cell Size in Image Mode", row_offset, self.image_cell_w, self.image_cell_h)
- row_offset += 1
- # Add double line break
- tk.Label(main_frame, text="").grid(row=row_offset, column=0)
- tk.Label(main_frame, text="").grid(row=row_offset + 1, column=0)
- row_offset += 2
- # Alternative Media Path
- tk.Label(main_frame, text="Alternative Media Path (Videos/Manuals):").grid(row=row_offset, column=0, sticky="w")
- self.alt_path_entry = tk.Entry(main_frame, textvariable=self.alternative_path, width=40)
- self.alt_path_entry.grid(row=row_offset, column=1, sticky="w")
- tk.Button(main_frame, text="Browse", command=self.browse_alternative_path).grid(row=row_offset, column=2)
- # Only for Video and Manual, with the format [Platform]/Video/[Game Name].mp4 or [Platform]/Manuals/[Game Name].pdf
- tk.Label(main_frame, text="Only for Video and Manual, with the format [Platform]/Videos/[Game Name].mp4 and [Platform]/Manuals/[Game Name].pdf").grid(row=row_offset + 1, column=0, columnspan=3, sticky="w")
- row_offset += 2
- # Add double line break
- tk.Label(main_frame, text="").grid(row=row_offset, column=0)
- tk.Label(main_frame, text="").grid(row=row_offset + 1, column=0)
- row_offset += 2
- # Color Selectors
- # Color for Media found with Title
- tk.Label(main_frame, text="Color for Media found with Title:").grid(row=row_offset, column=0, sticky="w")
- self.color_media_yes_title_display = tk.Label(main_frame, textvariable=self.color_media_yes_title, width=10,
- bg=self.color_media_yes_title.get())
- self.color_media_yes_title_display.grid(row=row_offset, column=1, sticky="w", padx=(0, 0))
- self.color_media_yes_title_button = tk.Button(main_frame, text="Select Color",
- command=self.choose_color_media_yes_title)
- self.color_media_yes_title_button.grid(row=row_offset, column=1, sticky="e")
- row_offset += 1
- # Color for Media found with RomName
- tk.Label(main_frame, text="Color for Media found with RomName:").grid(row=row_offset, column=0, sticky="w")
- self.color_media_yes_rom_display = tk.Label(main_frame, textvariable=self.color_media_yes_rom, width=10,
- bg=self.color_media_yes_rom.get())
- self.color_media_yes_rom_display.grid(row=row_offset, column=1, sticky="w", padx=(0, 0))
- self.color_media_yes_rom_button = tk.Button(main_frame, text="Select Color",
- command=self.choose_color_media_yes_rom)
- self.color_media_yes_rom_button.grid(row=row_offset, column=1, sticky="e")
- row_offset += 1
- # Color for Media found with Title/RomName
- tk.Label(main_frame, text="Color for Media found with Both:").grid(row=row_offset, column=0, sticky="w")
- self.color_no_trans_display = tk.Label(main_frame, textvariable=self.color_no_trans, width=10,
- bg=self.color_no_trans.get())
- self.color_no_trans_display.grid(row=row_offset, column=1, sticky="w", padx=(0, 30))
- self.color_no_trans_button = tk.Button(main_frame, text="Select Color", command=self.choose_color_no_trans)
- self.color_no_trans_button.grid(row=row_offset, column=1, sticky="e")
- row_offset += 1
- # Color for Media NOT found
- tk.Label(main_frame, text="Color for Media NOT found:").grid(row=row_offset, column=0, sticky="w")
- self.color_media_no_display = tk.Label(main_frame, textvariable=self.color_media_no, width=10,
- bg=self.color_media_no.get())
- self.color_media_no_display.grid(row=row_offset, column=1, sticky="w", padx=(0, 30))
- self.color_media_no_button = tk.Button(main_frame, text="Select Color", command=self.choose_color_media_no)
- self.color_media_no_button.grid(row=row_offset, column=1, sticky="e")
- row_offset += 1
- # Add double line break
- tk.Label(main_frame, text="").grid(row=row_offset, column=0)
- tk.Label(main_frame, text="").grid(row=row_offset + 1, column=0)
- row_offset += 2
- # Option to make cache
- tk.Checkbutton(main_frame, text="Make Local Cache (Better performance)", variable=self.make_cache, onvalue=True,
- offvalue=False).grid(row=row_offset, column=0, sticky="w")
- row_offset += 1
- # Cache image ratio
- self.create_size_input(main_frame, "Cache Image Ratio", row_offset, self.image_cache_ratio_w,
- self.image_cache_ratio_h)
- row_offset += 1
- # Save Button
- tk.Button(main_frame, text="Save", command=self.save_config).grid(row=row_offset, column=0, columnspan=3,
- pady=(20, 0))
- def create_size_input(self, frame, label_text, row, var_w, var_h):
- """Create a row with a label, two entries, and an 'x' in between, with adjusted spacing."""
- tk.Label(frame, text=label_text).grid(row=row, column=0, sticky="w")
- tk.Entry(frame, textvariable=var_w, width=5).grid(row=row, column=1, padx=(0, 0)) # Remove right padding
- tk.Label(frame, text="x").grid(row=row, column=1, padx=(32, 0)) # Remove padding
- tk.Entry(frame, textvariable=var_h, width=5).grid(row=row, column=1, padx=(80, 0)) # Remove left padding
- def browse_launchbox_path(self):
- folder_path = filedialog.askdirectory(title="Select LaunchBox Folder")
- if folder_path:
- self.launchbox_path.set(folder_path)
- def browse_alternative_path(self):
- folder_path = filedialog.askdirectory(title="Select Alternative Media Folder")
- if folder_path:
- self.alternative_path.set(folder_path)
- def choose_color_media_yes_title(self):
- color_code = colorchooser.askcolor(title="Select a Color")
- if color_code[1]: # Check if a color was selected (color_code[1] is the hex code)
- self.color_media_yes_title.set(color_code[1])
- self.color_media_yes_title_display.config(bg=color_code[1])
- def choose_color_media_yes_rom(self):
- color_code = colorchooser.askcolor(title="Select a Color")
- if color_code[1]: # Check if a color was selected
- self.color_media_yes_rom.set(color_code[1])
- self.color_media_yes_rom_display.config(bg=color_code[1])
- def choose_color_media_no(self):
- color_code = colorchooser.askcolor(title="Select a Color")
- if color_code[1]: # Check if a color was selected
- self.color_media_no.set(color_code[1])
- self.color_media_no_display.config(bg=color_code[1])
- def choose_color_no_trans(self):
- color_code = colorchooser.askcolor(title="Select a Color")
- if color_code[1]: # Check if a color was selected
- self.color_no_trans.set(color_code[1])
- self.color_no_trans_display.config(bg=color_code[1])
- def load_config(self):
- """Load current values from the config.txt file."""
- if os.path.exists(self.config_file):
- with open(self.config_file, "r") as file:
- for line in file:
- key, value = line.strip().split('=', 1)
- value = value.strip('"')
- if key == "path":
- self.launchbox_path.set(value)
- elif key == "filters":
- self.selected_filters = value.split(',')
- elif key == "image_cache_ratio":
- try:
- w, h = value.split('x')
- self.image_cache_ratio_w.set(w)
- self.image_cache_ratio_h.set(h)
- except ValueError:
- self.image_cache_ratio_w.set(0)
- self.image_cache_ratio_h.set(0)
- elif key == "text_cell":
- try:
- w, h = value.split('x')
- self.text_cell_w.set(w)
- self.text_cell_h.set(h)
- except ValueError:
- self.text_cell_w.set(0)
- self.text_cell_h.set(0)
- elif key == "image_cell":
- try:
- w, h = value.split('x')
- self.image_cell_w.set(w)
- self.image_cell_h.set(h)
- except ValueError:
- self.image_cell_w.set(0)
- self.image_cell_h.set(0)
- elif key == "alternative_path":
- self.alternative_path.set(value)
- elif key == "max_games_per_page_imagemode":
- self.max_games_per_page_imagemode.set(value)
- elif key == "color_media_yes_title":
- self.color_media_yes_title.set(value if value else "#80FF00")
- elif key == "color_media_yes_rom":
- self.color_media_yes_rom.set(value if value else "80AA00")
- elif key == "color_media_no":
- self.color_media_no.set(value if value else "#FF0000")
- elif key == "color_no_trans":
- self.color_no_trans.set(value if value else "#E2E2E2")
- elif key == "make_cache":
- self.make_cache.set(value.lower() == "true")
- def save_config(self):
- """Save the selected values to the config.txt file."""
- selected_filters = [self.filters[i] for i, var in enumerate(self.filter_vars) if var.get()]
- with open(self.config_file, "w") as file:
- file.write(f"path={self.launchbox_path.get()}\n")
- file.write(f'filters="{",".join(selected_filters)}"\n')
- file.write(f"image_cache_ratio={self.image_cache_ratio_w.get()}x{self.image_cache_ratio_h.get()}\n")
- file.write(f"text_cell={self.text_cell_w.get()}x{self.text_cell_h.get()}\n")
- file.write(f"image_cell={self.image_cell_w.get()}x{self.image_cell_h.get()}\n")
- file.write(f"alternative_path={self.alternative_path.get()}\n")
- file.write(f"max_games_per_page_imagemode={self.max_games_per_page_imagemode.get()}\n")
- file.write(f"color_media_yes_title={self.color_media_yes_title.get()}\n")
- file.write(f"color_media_yes_rom={self.color_media_yes_rom.get()}\n")
- file.write(f"color_media_no={self.color_media_no.get()}\n")
- file.write(f"color_no_trans={self.color_no_trans.get()}\n")
- file.write(f"make_cache={self.make_cache.get()}\n")
- messagebox.showinfo("Save", "Settings saved successfully.")
- self.root.destroy()
- if __name__ == "__main__":
- root = tk.Tk()
- config_file = os.path.join(os.path.dirname(__file__), "config.txt")
- app = SettingsWindow(root, config_file)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment