Dunkler2Engel

Settings

Feb 21st, 2025
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.03 KB | None | 0 0
  1. import os
  2. import tkinter as tk
  3. from tkinter import filedialog, messagebox, colorchooser
  4.  
  5.  
  6. class SettingsWindow:
  7. def __init__(self, root, config_file):
  8. self.root = root
  9. self.config_file = config_file
  10. self.root.title("Settings")
  11.  
  12. # Create a Canvas and Scrollbars
  13. self.canvas = tk.Canvas(self.root)
  14. self.scrollbar_y = tk.Scrollbar(self.root, orient="vertical", command=self.canvas.yview)
  15. self.scrollbar_x = tk.Scrollbar(self.root, orient="horizontal", command=self.canvas.xview)
  16.  
  17. # Configure the Canvas to be scrollable
  18. self.scrollable_frame = tk.Frame(self.canvas)
  19. self.scrollable_frame.bind(
  20. "<Configure>",
  21. lambda e: self.canvas.configure(
  22. scrollregion=self.canvas.bbox("all")
  23. )
  24. )
  25.  
  26. self.canvas.create_window((0, 0), window=self.scrollable_frame, anchor="nw")
  27. self.canvas.configure(yscrollcommand=self.scrollbar_y.set, xscrollcommand=self.scrollbar_x.set)
  28.  
  29. # Configure the mouse wheel event
  30. self.canvas.bind_all("<MouseWheel>", self._on_mousewheel)
  31.  
  32. # Place the Canvas and Scrollbars in the window
  33. self.canvas.grid(row=0, column=0, sticky="nsew")
  34. self.scrollbar_y.grid(row=0, column=1, sticky="ns")
  35. self.scrollbar_x.grid(row=1, column=0, sticky="ew")
  36.  
  37. # Configure window expansion
  38. self.root.grid_rowconfigure(0, weight=1)
  39. self.root.grid_columnconfigure(0, weight=1)
  40.  
  41. # Variables to store values
  42. self.launchbox_path = tk.StringVar(value="C:/LaunchBox")
  43. self.alternative_path = tk.StringVar(value="D:/LaunchBox")
  44. self.image_cache_ratio_w = tk.StringVar(value="200")
  45. self.image_cache_ratio_h = tk.StringVar(value="200")
  46. self.text_cell_w = tk.StringVar(value="150")
  47. self.text_cell_h = tk.StringVar(value="50")
  48. self.image_cell_w = tk.StringVar(value="200")
  49. self.image_cell_h = tk.StringVar(value="200")
  50. self.max_games_per_page_imagemode = tk.StringVar(value="200")
  51. self.color_media_yes_title = tk.StringVar(value="#80FF00")
  52. self.color_media_yes_rom = tk.StringVar(value="#80AA00")
  53. self.color_media_no = tk.StringVar(value="#FF0000")
  54. self.color_no_trans = tk.StringVar(value="#DBDBDB")
  55. self.selected_filters = ["Box - Front", "Clear Logo"]
  56. self.make_cache = tk.BooleanVar()
  57.  
  58. # Load current values from the config.txt file
  59. self.load_config()
  60.  
  61. # Graphical interface
  62. self.setup_ui()
  63.  
  64. # Adjust window size to the farthest content without additional margin
  65. self.root.update_idletasks()
  66. width = self.scrollable_frame.winfo_reqwidth()
  67. height = self.scrollable_frame.winfo_reqheight()
  68. self.root.geometry(f"{width}x{height}")
  69.  
  70. # Bind the window resize event
  71. self.root.bind("<Configure>", self._on_window_resize)
  72.  
  73. def _on_mousewheel(self, event):
  74. """Scroll the Canvas with the mouse wheel."""
  75. if event.delta:
  76. self.canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")
  77.  
  78. def _on_window_resize(self, event):
  79. """Update the scroll region when the window is resized."""
  80. self.canvas.configure(scrollregion=self.canvas.bbox("all"))
  81.  
  82. def setup_ui(self):
  83. """Configure the graphical interface elements."""
  84. main_frame = tk.Frame(self.scrollable_frame)
  85. main_frame.pack(fill="both", expand=True, padx=10, pady=10)
  86.  
  87. # LaunchBox Path
  88. tk.Label(main_frame, text="LaunchBox Path:").grid(row=0, column=0, sticky="w")
  89. self.path_entry = tk.Entry(main_frame, textvariable=self.launchbox_path, width=40)
  90. self.path_entry.grid(row=0, column=1, sticky="w")
  91. tk.Button(main_frame, text="Browse", command=self.browse_launchbox_path).grid(row=0, column=2)
  92.  
  93. # Selected Filters
  94. tk.Label(main_frame, text="Selected Filters:").grid(row=1, column=0, sticky="w", pady=(10, 0))
  95. self.filters = [
  96. "Arcade - Marquee", "Banner", "Advertisement Flyer - Back", "Advertisement Flyer - Front", "Box - Front",
  97. "Box - Front - Reconstructed", "Fanart - Box - Front", "Box - 3D", "Box - Back",
  98. "Box - Back - Reconstructed", "Fanart - Box - Back", "Box - Spine", "Box - Full", "Clear Logo",
  99. "Cart - Front", "Disc", "Fanart - Cart - Front", "Fanart - Disc", "Cart - Back", "Fanart - Cart - Back",
  100. "Cart - 3D", "Fanart - Background", "Screenshot - Game Over", "Screenshot - Game Select",
  101. "Screenshot - Game Title", "Screenshot - Gameplay", "Screenshot - High Scores", "Epic Games Background",
  102. "Epic Games Poster", "Epic Games Screenshot", "GOG Poster", "GOG Screenshot", "Origin Background",
  103. "Origin Poster", "Origin Screenshot", "Steam Banner", "Steam Poster", "Steam Screenshot",
  104. "Uplay Background", "Uplay Thumbnail", "Arcade - Cabinet", "Arcade - Circuit Board",
  105. "Arcade - Control Panel", "Arcade - Controls Information", "Amazon Background", "Amazon Poster",
  106. "Amazon Screenshot", "Videos", "Manual"
  107. ]
  108.  
  109. self.filter_vars = []
  110. num_columns = 3 # Number of columns for filters
  111. max_filters_per_column = (len(self.filters) // num_columns) + (1 if len(self.filters) % num_columns != 0 else 0)
  112. for i, filter_name in enumerate(self.filters):
  113. var = tk.BooleanVar(value=(filter_name in self.selected_filters))
  114. row = 2 + (i % max_filters_per_column)
  115. column = i // max_filters_per_column
  116. tk.Checkbutton(main_frame, text=filter_name, variable=var).grid(row=row, column=column, sticky="w")
  117. self.filter_vars.append(var)
  118.  
  119. # Adjust the width of the scrollable_frame to the widest content
  120. self.scrollable_frame.update_idletasks()
  121. self.canvas.configure(scrollregion=self.canvas.bbox("all"))
  122.  
  123. row_offset = 2 + max_filters_per_column
  124.  
  125. # Add a line break
  126. tk.Label(main_frame, text="").grid(row=row_offset, column=0)
  127. row_offset += 1
  128.  
  129. # Maximum games listed per page
  130. tk.Label(main_frame, text="Maximum games listed per page:").grid(row=row_offset, column=0, sticky="w")
  131. self.max_games_entry = tk.Entry(main_frame, textvariable=self.max_games_per_page_imagemode, width=5)
  132. self.max_games_entry.grid(row=row_offset, column=1, padx=(0, 0))
  133. row_offset += 1
  134.  
  135. # Add a line break
  136. tk.Label(main_frame, text="").grid(row=row_offset, column=0)
  137. row_offset += 1
  138.  
  139. # Cell size in Text mode
  140. self.create_size_input(main_frame, "Cell Size in Text Mode", row_offset, self.text_cell_w, self.text_cell_h)
  141. row_offset += 1
  142.  
  143. # Cell size in Image mode
  144. self.create_size_input(main_frame, "Cell Size in Image Mode", row_offset, self.image_cell_w, self.image_cell_h)
  145. row_offset += 1
  146.  
  147. # Add double line break
  148. tk.Label(main_frame, text="").grid(row=row_offset, column=0)
  149. tk.Label(main_frame, text="").grid(row=row_offset + 1, column=0)
  150. row_offset += 2
  151.  
  152. # Alternative Media Path
  153. tk.Label(main_frame, text="Alternative Media Path (Videos/Manuals):").grid(row=row_offset, column=0, sticky="w")
  154. self.alt_path_entry = tk.Entry(main_frame, textvariable=self.alternative_path, width=40)
  155. self.alt_path_entry.grid(row=row_offset, column=1, sticky="w")
  156. tk.Button(main_frame, text="Browse", command=self.browse_alternative_path).grid(row=row_offset, column=2)
  157. # Only for Video and Manual, with the format [Platform]/Video/[Game Name].mp4 or [Platform]/Manuals/[Game Name].pdf
  158. 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")
  159. row_offset += 2
  160.  
  161. # Add double line break
  162. tk.Label(main_frame, text="").grid(row=row_offset, column=0)
  163. tk.Label(main_frame, text="").grid(row=row_offset + 1, column=0)
  164. row_offset += 2
  165.  
  166. # Color Selectors
  167. # Color for Media found with Title
  168. tk.Label(main_frame, text="Color for Media found with Title:").grid(row=row_offset, column=0, sticky="w")
  169. self.color_media_yes_title_display = tk.Label(main_frame, textvariable=self.color_media_yes_title, width=10,
  170. bg=self.color_media_yes_title.get())
  171. self.color_media_yes_title_display.grid(row=row_offset, column=1, sticky="w", padx=(0, 0))
  172. self.color_media_yes_title_button = tk.Button(main_frame, text="Select Color",
  173. command=self.choose_color_media_yes_title)
  174. self.color_media_yes_title_button.grid(row=row_offset, column=1, sticky="e")
  175. row_offset += 1
  176. # Color for Media found with RomName
  177. tk.Label(main_frame, text="Color for Media found with RomName:").grid(row=row_offset, column=0, sticky="w")
  178. self.color_media_yes_rom_display = tk.Label(main_frame, textvariable=self.color_media_yes_rom, width=10,
  179. bg=self.color_media_yes_rom.get())
  180. self.color_media_yes_rom_display.grid(row=row_offset, column=1, sticky="w", padx=(0, 0))
  181. self.color_media_yes_rom_button = tk.Button(main_frame, text="Select Color",
  182. command=self.choose_color_media_yes_rom)
  183. self.color_media_yes_rom_button.grid(row=row_offset, column=1, sticky="e")
  184. row_offset += 1
  185.  
  186. # Color for Media found with Title/RomName
  187. tk.Label(main_frame, text="Color for Media found with Both:").grid(row=row_offset, column=0, sticky="w")
  188. self.color_no_trans_display = tk.Label(main_frame, textvariable=self.color_no_trans, width=10,
  189. bg=self.color_no_trans.get())
  190. self.color_no_trans_display.grid(row=row_offset, column=1, sticky="w", padx=(0, 30))
  191. self.color_no_trans_button = tk.Button(main_frame, text="Select Color", command=self.choose_color_no_trans)
  192. self.color_no_trans_button.grid(row=row_offset, column=1, sticky="e")
  193. row_offset += 1
  194.  
  195. # Color for Media NOT found
  196. tk.Label(main_frame, text="Color for Media NOT found:").grid(row=row_offset, column=0, sticky="w")
  197. self.color_media_no_display = tk.Label(main_frame, textvariable=self.color_media_no, width=10,
  198. bg=self.color_media_no.get())
  199. self.color_media_no_display.grid(row=row_offset, column=1, sticky="w", padx=(0, 30))
  200. self.color_media_no_button = tk.Button(main_frame, text="Select Color", command=self.choose_color_media_no)
  201. self.color_media_no_button.grid(row=row_offset, column=1, sticky="e")
  202. row_offset += 1
  203.  
  204.  
  205. # Add double line break
  206. tk.Label(main_frame, text="").grid(row=row_offset, column=0)
  207. tk.Label(main_frame, text="").grid(row=row_offset + 1, column=0)
  208. row_offset += 2
  209.  
  210. # Option to make cache
  211. tk.Checkbutton(main_frame, text="Make Local Cache (Better performance)", variable=self.make_cache, onvalue=True,
  212. offvalue=False).grid(row=row_offset, column=0, sticky="w")
  213. row_offset += 1
  214.  
  215. # Cache image ratio
  216. self.create_size_input(main_frame, "Cache Image Ratio", row_offset, self.image_cache_ratio_w,
  217. self.image_cache_ratio_h)
  218. row_offset += 1
  219.  
  220. # Save Button
  221. tk.Button(main_frame, text="Save", command=self.save_config).grid(row=row_offset, column=0, columnspan=3,
  222. pady=(20, 0))
  223.  
  224. def create_size_input(self, frame, label_text, row, var_w, var_h):
  225. """Create a row with a label, two entries, and an 'x' in between, with adjusted spacing."""
  226. tk.Label(frame, text=label_text).grid(row=row, column=0, sticky="w")
  227. tk.Entry(frame, textvariable=var_w, width=5).grid(row=row, column=1, padx=(0, 0)) # Remove right padding
  228. tk.Label(frame, text="x").grid(row=row, column=1, padx=(32, 0)) # Remove padding
  229. tk.Entry(frame, textvariable=var_h, width=5).grid(row=row, column=1, padx=(80, 0)) # Remove left padding
  230.  
  231. def browse_launchbox_path(self):
  232. folder_path = filedialog.askdirectory(title="Select LaunchBox Folder")
  233. if folder_path:
  234. self.launchbox_path.set(folder_path)
  235.  
  236. def browse_alternative_path(self):
  237. folder_path = filedialog.askdirectory(title="Select Alternative Media Folder")
  238. if folder_path:
  239. self.alternative_path.set(folder_path)
  240.  
  241. def choose_color_media_yes_title(self):
  242. color_code = colorchooser.askcolor(title="Select a Color")
  243. if color_code[1]: # Check if a color was selected (color_code[1] is the hex code)
  244. self.color_media_yes_title.set(color_code[1])
  245. self.color_media_yes_title_display.config(bg=color_code[1])
  246.  
  247. def choose_color_media_yes_rom(self):
  248. color_code = colorchooser.askcolor(title="Select a Color")
  249. if color_code[1]: # Check if a color was selected
  250. self.color_media_yes_rom.set(color_code[1])
  251. self.color_media_yes_rom_display.config(bg=color_code[1])
  252.  
  253. def choose_color_media_no(self):
  254. color_code = colorchooser.askcolor(title="Select a Color")
  255. if color_code[1]: # Check if a color was selected
  256. self.color_media_no.set(color_code[1])
  257. self.color_media_no_display.config(bg=color_code[1])
  258.  
  259. def choose_color_no_trans(self):
  260. color_code = colorchooser.askcolor(title="Select a Color")
  261. if color_code[1]: # Check if a color was selected
  262. self.color_no_trans.set(color_code[1])
  263. self.color_no_trans_display.config(bg=color_code[1])
  264.  
  265. def load_config(self):
  266. """Load current values from the config.txt file."""
  267. if os.path.exists(self.config_file):
  268. with open(self.config_file, "r") as file:
  269. for line in file:
  270. key, value = line.strip().split('=', 1)
  271. value = value.strip('"')
  272. if key == "path":
  273. self.launchbox_path.set(value)
  274. elif key == "filters":
  275. self.selected_filters = value.split(',')
  276. elif key == "image_cache_ratio":
  277. try:
  278. w, h = value.split('x')
  279. self.image_cache_ratio_w.set(w)
  280. self.image_cache_ratio_h.set(h)
  281. except ValueError:
  282. self.image_cache_ratio_w.set(0)
  283. self.image_cache_ratio_h.set(0)
  284. elif key == "text_cell":
  285. try:
  286. w, h = value.split('x')
  287. self.text_cell_w.set(w)
  288. self.text_cell_h.set(h)
  289. except ValueError:
  290. self.text_cell_w.set(0)
  291. self.text_cell_h.set(0)
  292. elif key == "image_cell":
  293. try:
  294. w, h = value.split('x')
  295. self.image_cell_w.set(w)
  296. self.image_cell_h.set(h)
  297. except ValueError:
  298. self.image_cell_w.set(0)
  299. self.image_cell_h.set(0)
  300. elif key == "alternative_path":
  301. self.alternative_path.set(value)
  302. elif key == "max_games_per_page_imagemode":
  303. self.max_games_per_page_imagemode.set(value)
  304. elif key == "color_media_yes_title":
  305. self.color_media_yes_title.set(value if value else "#80FF00")
  306. elif key == "color_media_yes_rom":
  307. self.color_media_yes_rom.set(value if value else "80AA00")
  308. elif key == "color_media_no":
  309. self.color_media_no.set(value if value else "#FF0000")
  310. elif key == "color_no_trans":
  311. self.color_no_trans.set(value if value else "#E2E2E2")
  312. elif key == "make_cache":
  313. self.make_cache.set(value.lower() == "true")
  314.  
  315. def save_config(self):
  316. """Save the selected values to the config.txt file."""
  317. selected_filters = [self.filters[i] for i, var in enumerate(self.filter_vars) if var.get()]
  318. with open(self.config_file, "w") as file:
  319. file.write(f"path={self.launchbox_path.get()}\n")
  320. file.write(f'filters="{",".join(selected_filters)}"\n')
  321. file.write(f"image_cache_ratio={self.image_cache_ratio_w.get()}x{self.image_cache_ratio_h.get()}\n")
  322. file.write(f"text_cell={self.text_cell_w.get()}x{self.text_cell_h.get()}\n")
  323. file.write(f"image_cell={self.image_cell_w.get()}x{self.image_cell_h.get()}\n")
  324. file.write(f"alternative_path={self.alternative_path.get()}\n")
  325. file.write(f"max_games_per_page_imagemode={self.max_games_per_page_imagemode.get()}\n")
  326. file.write(f"color_media_yes_title={self.color_media_yes_title.get()}\n")
  327. file.write(f"color_media_yes_rom={self.color_media_yes_rom.get()}\n")
  328. file.write(f"color_media_no={self.color_media_no.get()}\n")
  329. file.write(f"color_no_trans={self.color_no_trans.get()}\n")
  330. file.write(f"make_cache={self.make_cache.get()}\n")
  331. messagebox.showinfo("Save", "Settings saved successfully.")
  332. self.root.destroy()
  333.  
  334.  
  335. if __name__ == "__main__":
  336. root = tk.Tk()
  337. config_file = os.path.join(os.path.dirname(__file__), "config.txt")
  338. app = SettingsWindow(root, config_file)
  339. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment