add to script new ckeckbox button at the rigth of modes switch buttons that called "run…
- User
add to script new ckeckbox button at the rigth of modes switch buttons that called "run offset 1 time" and if enabled it adds info about it state to config_tg-.ini file that script would read at start to adjust state of those button. also next to those new button there shoul be also saveable input field. import tkinter as tk from tkinter import font, messagebox import configparser import os
class OffsetConfigEditor: def init(self, master): self.master = master self.master.title("TG - Offsets and GPU Mode Config Editor")
# Remove maximize button self.master.resizable(False, False) # Disable window resizing self.master.attributes("-toolwindow", True) # This removes the maximize button on Windows self.bold_font = font.Font(weight="bold") self.batch_folder = os.path.join("TG", "TGMAIN") self.ini_file = os.path.join(self.batch_folder, "config_tg-.ini") # Ensure batch folder exists if not os.path.exists(self.batch_folder): messagebox.showerror("Error", f"Batch folder not found: {self.batch_folder}") self.master.destroy() return # Read the config file self.config = configparser.ConfigParser() if not os.path.exists(self.ini_file): messagebox.showerror("Error", f"Configuration file not found: {self.ini_file}") self.master.destroy() return self.config.read(self.ini_file) self.entries = {} self.create_offset_fields() # Add buttons for mode switching at the bottom, centered self.mode_var = tk.StringVar() self.check_current_mode() mode_frame = tk.Frame(self.master) mode_frame.grid(row=1000, column=0, columnspan=6, pady=10) tk.Radiobutton(mode_frame, text="CUDA mode", variable=self.mode_var, value="CUDA", command=self.switch_mode).pack(side=tk.LEFT, padx=5) tk.Radiobutton(mode_frame, text="DirectML mode", variable=self.mode_var, value="DirectML", command=self.switch_mode).pack(side=tk.LEFT, padx=5) def create_offset_fields(self): """Populate offset fields in a multi-column, multi-row grid layout with aligned entry fields.""" row = 0 col = 0 max_columns = 3 for section in self.config.sections(): # Section Label (Bold) label = tk.Label(self.master, text=section, font=self.bold_font) label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_value = self.config.get(section, 'offset_x', fallback='0') offset_y_value = self.config.get(section, 'offset_y', fallback='0') # Label and aligned entry for offset_x offset_x_label = tk.Label(self.master, text="offset_x:") offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry = tk.Entry(self.master, width=10) offset_x_entry.insert(0, offset_x_value) offset_x_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_x_entry: self.update_value(sec, 'offset_x', entry.get())) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Label and aligned entry for offset_y offset_y_label = tk.Label(self.master, text="offset_y:") offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry = tk.Entry(self.master, width=10) offset_y_entry.insert(0, offset_y_value) offset_y_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_y_entry: self.update_value(sec, 'offset_y', entry.get())) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Store entries for later access self.entries[section] = (label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry) col += 1 if col >= max_columns: col = 0 row += 3 # Update last used row for later layout adjustments self.last_row = row + 2 def update_value(self, section, key, value): # Set the value with no spaces around `=` self.config.set(section, key, value) with open(self.ini_file, 'w') as configfile: for section in self.config.sections(): configfile.write(f'[{section}]\n') for key, val in self.config.items(section): configfile.write(f'{key}={val}\n') # No spaces around `=` configfile.write('\n') def check_current_mode(self): tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") if os.path.exists(tecogan_bat) and os.path.exists(tecogan_dml_bat): self.mode_var.set("CUDA") elif os.path.exists(tecogan_bat) and not os.path.exists(tecogan_dml_bat): self.mode_var.set("DirectML") self.hide_directml_fields() else: messagebox.showerror("Error", "Invalid batch file configuration for determining mode.") self.master.destroy() def switch_mode(self): if self.mode_var.get() == "DirectML": self.hide_directml_fields() else: self.show_all_fields() tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") tecogan_cuda_bat = os.path.join(self.batch_folder, "tecogan_cuda.bat") try: if self.mode_var.get() == "DirectML": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_cuda_bat) if os.path.exists(tecogan_dml_bat): os.rename(tecogan_dml_bat, tecogan_bat) elif self.mode_var.get() == "CUDA": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_dml_bat) if os.path.exists(tecogan_cuda_bat): os.rename(tecogan_cuda_bat, tecogan_bat) except FileNotFoundError as e: messagebox.showerror("Error", f"File not found during mode switching: {e}") except Exception as e: messagebox.showerror("Error", f"Unexpected error during mode switching: {e}") def hide_directml_fields(self): """Hide fields containing 'BVPP' or 'TG8Xa' in their name and reorganize layout.""" row = 0 col = 0 max_columns = 3 for section, widgets in self.entries.items(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets if "BVPP" in section or "TG8Xa" in section: label.grid_remove() offset_x_label.grid_remove() offset_x_entry.grid_remove() offset_y_label.grid_remove() offset_y_entry.grid_remove() else: # Re-grid remaining widgets to fill gaps label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the new layout for buttons self.last_row = row + 2 def show_all_fields(self): """Show all fields and reset layout.""" row = 0 col = 0 max_columns = 3 for widgets in self.entries.values(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the reset layout for buttons self.last_row = row + 2if name == "main": root = tk.Tk() editor = OffsetConfigEditor(root) root.mainloop()
- Assistant
To add a new checkbox button labeled "Run Offset 1 Time," and an associated input field to your existing Tkinter script, you should make the following modifications.
- Add a new checkbox with a label.
- Add a corresponding input field.
- Modify the configuration file handling to read and save the state of the checkbox and the value in the input field.
Here's how you can implement these changes:
import tkinter as tk from tkinter import font, messagebox import configparser import os class OffsetConfigEditor: def __init__(self, master): self.master = master self.master.title("TG - Offsets and GPU Mode Config Editor") # Remove maximize button self.master.resizable(False, False) # Disable window resizing self.master.attributes("-toolwindow", True) # This removes the maximize button on Windows self.bold_font = font.Font(weight="bold") self.batch_folder = os.path.join("TG", "TGMAIN") self.ini_file = os.path.join(self.batch_folder, "config_tg-.ini") # Ensure batch folder exists if not os.path.exists(self.batch_folder): messagebox.showerror("Error", f"Batch folder not found: {self.batch_folder}") self.master.destroy() return # Read the config file self.config = configparser.ConfigParser() if not os.path.exists(self.ini_file): messagebox.showerror("Error", f"Configuration file not found: {self.ini_file}") self.master.destroy() return self.config.read(self.ini_file) self.entries = {} self.create_offset_fields() # Add the "Run Offset 1 Time" checkbox and input field self.run_offset_var = tk.BooleanVar() self.input_var = tk.StringVar() # Load initial states self.run_offset_var.set(self.config.getboolean('Settings', 'run_offset_once', fallback=False)) self.input_var.set(self.config.get('Settings', 'input_value', fallback='')) offset_frame = tk.Frame(self.master) offset_frame.grid(row=self.last_row + 1, column=0, columnspan=6, pady=10) run_once_checkbox = tk.Checkbutton( offset_frame, text="Run Offset 1 Time", variable=self.run_offset_var, command=self.save_run_offset_state ) run_once_checkbox.pack(side=tk.LEFT, padx=5) input_entry = tk.Entry(offset_frame, textvariable=self.input_var) input_entry.pack(side=tk.LEFT, padx=5) input_entry.bind("<KeyRelease>", lambda e: self.save_input_value()) # Add buttons for mode switching at the bottom, centered self.mode_var = tk.StringVar() self.check_current_mode() mode_frame = tk.Frame(self.master) mode_frame.grid(row=1000, column=0, columnspan=6, pady=10) tk.Radiobutton(mode_frame, text="CUDA mode", variable=self.mode_var, value="CUDA", command=self.switch_mode).pack(side=tk.LEFT, padx=5) tk.Radiobutton(mode_frame, text="DirectML mode", variable=self.mode_var, value="DirectML", command=self.switch_mode).pack(side=tk.LEFT, padx=5) def create_offset_fields(self): """Populate offset fields in a multi-column, multi-row grid layout with aligned entry fields.""" row = 0 col = 0 max_columns = 3 for section in self.config.sections(): # Skip 'Settings' section which is not an offset if section == "Settings": continue # Section Label (Bold) label = tk.Label(self.master, text=section, font=self.bold_font) label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_value = self.config.get(section, 'offset_x', fallback='0') offset_y_value = self.config.get(section, 'offset_y', fallback='0') # Label and aligned entry for offset_x offset_x_label = tk.Label(self.master, text="offset_x:") offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry = tk.Entry(self.master, width=10) offset_x_entry.insert(0, offset_x_value) offset_x_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_x_entry: self.update_value(sec, 'offset_x', entry.get())) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Label and aligned entry for offset_y offset_y_label = tk.Label(self.master, text="offset_y:") offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry = tk.Entry(self.master, width=10) offset_y_entry.insert(0, offset_y_value) offset_y_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_y_entry: self.update_value(sec, 'offset_y', entry.get())) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Store entries for later access self.entries[section] = (label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry) col += 1 if col >= max_columns: col = 0 row += 3 # Update last used row for later layout adjustments self.last_row = row + 2 def update_value(self, section, key, value): # Set the value with no spaces around `=` self.config.set(section, key, value) with open(self.ini_file, 'w') as configfile: for section in self.config.sections(): configfile.write(f'[{section}]\n') for key, val in self.config.items(section): configfile.write(f'{key}={val}\n') # No spaces around `=` configfile.write('\n') def save_run_offset_state(self): """Save the state of the 'Run Offset 1 Time' checkbox.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'run_offset_once', str(self.run_offset_var.get())) self.write_config() def save_input_value(self): """Save the value of the input entry.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'input_value', self.input_var.get()) self.write_config() def write_config(self): with open(self.ini_file, 'w') as configfile: for section in self.config.sections(): configfile.write(f'[{section}]\n') for key, val in self.config.items(section): configfile.write(f'{key}={val}\n') # No spaces around `=` configfile.write('\n') def check_current_mode(self): tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") if os.path.exists(tecogan_bat) and os.path.exists(tecogan_dml_bat): self.mode_var.set("CUDA") elif os.path.exists(tecogan_bat) and not os.path.exists(tecogan_dml_bat): self.mode_var.set("DirectML") self.hide_directml_fields() else: messagebox.showerror("Error", "Invalid batch file configuration for determining mode.") self.master.destroy() def switch_mode(self): if self.mode_var.get() == "DirectML": self.hide_directml_fields() else: self.show_all_fields() tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") tecogan_cuda_bat = os.path.join(self.batch_folder, "tecogan_cuda.bat") try: if self.mode_var.get() == "DirectML": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_cuda_bat) if os.path.exists(tecogan_dml_bat): os.rename(tecogan_dml_bat, tecogan_bat) elif self.mode_var.get() == "CUDA": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_dml_bat) if os.path.exists(tecogan_cuda_bat): os.rename(tecogan_cuda_bat, tecogan_bat) except FileNotFoundError as e: messagebox.showerror("Error", f"File not found during mode switching: {e}") except Exception as e: messagebox.showerror("Error", f"Unexpected error during mode switching: {e}") def hide_directml_fields(self): """Hide fields containing 'BVPP' or 'TG8Xa' in their name and reorganize layout.""" row = 0 col = 0 max_columns = 3 for section, widgets in self.entries.items(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets if "BVPP" in section or "TG8Xa" in section: label.grid_remove() offset_x_label.grid_remove() offset_x_entry.grid_remove() offset_y_label.grid_remove() offset_y_entry.grid_remove() else: # Re-grid remaining widgets to fill gaps label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the new layout for buttons self.last_row = row + 2 def show_all_fields(self): """Show all fields and reset layout.""" row = 0 col = 0 max_columns = 3 for widgets in self.entries.values(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the reset layout for buttons self.last_row = row + 2 if __name__ == "__main__": root = tk.Tk() editor = OffsetConfigEditor(root) root.mainloop()Key Changes:
- Checkbox & Input Field: Added a checkbox and input field with a pack layout in a new frame instance.
- Configuration Handling: Added a 'Settings' section to the config file to remember the checkbox state and input value.
- Add Methods: Added methods
save_run_offset_state()andsave_input_value()to save changes made by the checkbox and input field.
Make sure to call these methods to save any changes whenever the state of the checkbox or the value of the input field changes. The checkbox and entry logic is handled by
run_offset_varandinput_var, and their states are saved in the configuration file. - User
make new input files 3 times more narrow and next to it inside () add text "enable when use multiple models" and after text and near new input field add offset_x label and also create same input filed but called offset_y
- Assistant
To modify the script to include a narrower input field for the "Run Offset 1 Time" checkbox, along with additional explanatory text and separate
offset_xandoffset_yinput fields, follow these steps:- Adjust the width of the new input fields.
- Add explanatory text next to the "Run Offset 1 Time" checkbox.
- Create additional
offset_xandoffset_yinput fields with labels for each.
Here is the revised code with these adjustments:
import tkinter as tk from tkinter import font, messagebox import configparser import os class OffsetConfigEditor: def __init__(self, master): self.master = master self.master.title("TG - Offsets and GPU Mode Config Editor") # Remove maximize button self.master.resizable(False, False) # Disable window resizing self.master.attributes("-toolwindow", True) # This removes the maximize button on Windows self.bold_font = font.Font(weight="bold") self.batch_folder = os.path.join("TG", "TGMAIN") self.ini_file = os.path.join(self.batch_folder, "config_tg-.ini") # Ensure batch folder exists if not os.path.exists(self.batch_folder): messagebox.showerror("Error", f"Batch folder not found: {self.batch_folder}") self.master.destroy() return # Read the config file self.config = configparser.ConfigParser() if not os.path.exists(self.ini_file): messagebox.showerror("Error", f"Configuration file not found: {self.ini_file}") self.master.destroy() return self.config.read(self.ini_file) self.entries = {} self.create_offset_fields() # Add the "Run Offset 1 Time" checkbox and input fields for offsets self.run_offset_var = tk.BooleanVar() self.input_var = tk.StringVar() self.offset_x_var = tk.StringVar() self.offset_y_var = tk.StringVar() # Load initial states self.run_offset_var.set(self.config.getboolean('Settings', 'run_offset_once', fallback=False)) self.input_var.set(self.config.get('Settings', 'input_value', fallback='')) self.offset_x_var.set(self.config.get('Settings', 'offset_x_value', fallback='')) self.offset_y_var.set(self.config.get('Settings', 'offset_y_value', fallback='')) offset_frame = tk.Frame(self.master) offset_frame.grid(row=self.last_row + 1, column=0, columnspan=6, pady=10) run_once_checkbox = tk.Checkbutton( offset_frame, text="Run Offset 1 Time", variable=self.run_offset_var, command=self.save_run_offset_state ) run_once_checkbox.pack(side=tk.LEFT, padx=5) # Explanatory label explanation_label = tk.Label(offset_frame, text="(enable when use multiple models)") explanation_label.pack(side=tk.LEFT, padx=5) # Narrow input field new_input_entry = tk.Entry(offset_frame, textvariable=self.input_var, width=4) new_input_entry.pack(side=tk.LEFT, padx=5) new_input_entry.bind("<KeyRelease>", lambda e: self.save_input_value()) # Offset X label and input offset_x_label = tk.Label(offset_frame, text="offset_x:") offset_x_label.pack(side=tk.LEFT, padx=5) offset_x_entry = tk.Entry(offset_frame, textvariable=self.offset_x_var, width=4) offset_x_entry.pack(side=tk.LEFT, padx=5) offset_x_entry.bind("<KeyRelease>", lambda e: self.save_offset_x_value()) # Offset Y label and input offset_y_label = tk.Label(offset_frame, text="offset_y:") offset_y_label.pack(side=tk.LEFT, padx=5) offset_y_entry = tk.Entry(offset_frame, textvariable=self.offset_y_var, width=4) offset_y_entry.pack(side=tk.LEFT, padx=5) offset_y_entry.bind("<KeyRelease>", lambda e: self.save_offset_y_value()) # Add buttons for mode switching at the bottom, centered self.mode_var = tk.StringVar() self.check_current_mode() mode_frame = tk.Frame(self.master) mode_frame.grid(row=1000, column=0, columnspan=6, pady=10) tk.Radiobutton(mode_frame, text="CUDA mode", variable=self.mode_var, value="CUDA", command=self.switch_mode).pack(side=tk.LEFT, padx=5) tk.Radiobutton(mode_frame, text="DirectML mode", variable=self.mode_var, value="DirectML", command=self.switch_mode).pack(side=tk.LEFT, padx=5) def create_offset_fields(self): """Populate offset fields in a multi-column, multi-row grid layout with aligned entry fields.""" row = 0 col = 0 max_columns = 3 for section in self.config.sections(): # Skip 'Settings' section which is not an offset if section == "Settings": continue # Section Label (Bold) label = tk.Label(self.master, text=section, font=self.bold_font) label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_value = self.config.get(section, 'offset_x', fallback='0') offset_y_value = self.config.get(section, 'offset_y', fallback='0') # Label and aligned entry for offset_x offset_x_label = tk.Label(self.master, text="offset_x:") offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry = tk.Entry(self.master, width=10) offset_x_entry.insert(0, offset_x_value) offset_x_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_x_entry: self.update_value(sec, 'offset_x', entry.get())) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Label and aligned entry for offset_y offset_y_label = tk.Label(self.master, text="offset_y:") offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry = tk.Entry(self.master, width=10) offset_y_entry.insert(0, offset_y_value) offset_y_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_y_entry: self.update_value(sec, 'offset_y', entry.get())) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Store entries for later access self.entries[section] = (label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry) col += 1 if col >= max_columns: col = 0 row += 3 # Update last used row for later layout adjustments self.last_row = row + 2 def update_value(self, section, key, value): # Set the value with no spaces around `=` self.config.set(section, key, value) self.write_config() def save_run_offset_state(self): """Save the state of the 'Run Offset 1 Time' checkbox.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'run_offset_once', str(self.run_offset_var.get())) self.write_config() def save_input_value(self): """Save the value of the input entry.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'input_value', self.input_var.get()) self.write_config() def save_offset_x_value(self): """Save the value of the offset_x input entry.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'offset_x_value', self.offset_x_var.get()) self.write_config() def save_offset_y_value(self): """Save the value of the offset_y input entry.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'offset_y_value', self.offset_y_var.get()) self.write_config() def write_config(self): with open(self.ini_file, 'w') as configfile: for section in self.config.sections(): configfile.write(f'[{section}]\n') for key, val in self.config.items(section): configfile.write(f'{key}={val}\n') # No spaces around `=` configfile.write('\n') def check_current_mode(self): tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") if os.path.exists(tecogan_bat) and os.path.exists(tecogan_dml_bat): self.mode_var.set("CUDA") elif os.path.exists(tecogan_bat) and not os.path.exists(tecogan_dml_bat): self.mode_var.set("DirectML") self.hide_directml_fields() else: messagebox.showerror("Error", "Invalid batch file configuration for determining mode.") self.master.destroy() def switch_mode(self): if self.mode_var.get() == "DirectML": self.hide_directml_fields() else: self.show_all_fields() tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") tecogan_cuda_bat = os.path.join(self.batch_folder, "tecogan_cuda.bat") try: if self.mode_var.get() == "DirectML": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_cuda_bat) if os.path.exists(tecogan_dml_bat): os.rename(tecogan_dml_bat, tecogan_bat) elif self.mode_var.get() == "CUDA": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_dml_bat) if os.path.exists(tecogan_cuda_bat): os.rename(tecogan_cuda_bat, tecogan_bat) except FileNotFoundError as e: messagebox.showerror("Error", f"File not found during mode switching: {e}") except Exception as e: messagebox.showerror("Error", f"Unexpected error during mode switching: {e}") def hide_directml_fields(self): """Hide fields containing 'BVPP' or 'TG8Xa' in their name and reorganize layout.""" row = 0 col = 0 max_columns = 3 for section, widgets in self.entries.items(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets if "BVPP" in section or "TG8Xa" in section: label.grid_remove() offset_x_label.grid_remove() offset_x_entry.grid_remove() offset_y_label.grid_remove() offset_y_entry.grid_remove() else: # Re-grid remaining widgets to fill gaps label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the new layout for buttons self.last_row = row + 2 def show_all_fields(self): """Show all fields and reset layout.""" row = 0 col = 0 max_columns = 3 for widgets in self.entries.values(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the reset layout for buttons self.last_row = row + 2 if __name__ == "__main__": root = tk.Tk() editor = OffsetConfigEditor(root) root.mainloop()Changes Made:
- Narrower Input Field: Adjusted the width of the
new_input_entry,offset_x_entry, andoffset_y_entryto be 4. - Explanatory Text: Added a label that explains the purpose of the checkbox.
- Offset Input Fields: Added separate
offset_xandoffset_yinput fields with associated labels.
This script will now include the specified features, allowing for the management of offset inputs alongside the enhanced checkbox functionality.
- User
remove input_value filed from code
- Assistant
To remove the
input_valuefield from the code, you need to eliminate references to it, including the variable, associated widget, and the logic for saving its value. Here is the modified script without theinput_valuefield:import tkinter as tk from tkinter import font, messagebox import configparser import os class OffsetConfigEditor: def __init__(self, master): self.master = master self.master.title("TG - Offsets and GPU Mode Config Editor") # Remove maximize button self.master.resizable(False, False) # Disable window resizing self.master.attributes("-toolwindow", True) # This removes the maximize button on Windows self.bold_font = font.Font(weight="bold") self.batch_folder = os.path.join("TG", "TGMAIN") self.ini_file = os.path.join(self.batch_folder, "config_tg-.ini") # Ensure batch folder exists if not os.path.exists(self.batch_folder): messagebox.showerror("Error", f"Batch folder not found: {self.batch_folder}") self.master.destroy() return # Read the config file self.config = configparser.ConfigParser() if not os.path.exists(self.ini_file): messagebox.showerror("Error", f"Configuration file not found: {self.ini_file}") self.master.destroy() return self.config.read(self.ini_file) self.entries = {} self.create_offset_fields() # Add the "Run Offset 1 Time" checkbox and input fields for offsets self.run_offset_var = tk.BooleanVar() self.offset_x_var = tk.StringVar() self.offset_y_var = tk.StringVar() # Load initial states self.run_offset_var.set(self.config.getboolean('Settings', 'run_offset_once', fallback=False)) self.offset_x_var.set(self.config.get('Settings', 'offset_x_value', fallback='')) self.offset_y_var.set(self.config.get('Settings', 'offset_y_value', fallback='')) offset_frame = tk.Frame(self.master) offset_frame.grid(row=self.last_row + 1, column=0, columnspan=6, pady=10) run_once_checkbox = tk.Checkbutton( offset_frame, text="Run Offset 1 Time", variable=self.run_offset_var, command=self.save_run_offset_state ) run_once_checkbox.pack(side=tk.LEFT, padx=5) # Explanatory label explanation_label = tk.Label(offset_frame, text="(enable when use multiple models)") explanation_label.pack(side=tk.LEFT, padx=5) # Offset X label and input offset_x_label = tk.Label(offset_frame, text="offset_x:") offset_x_label.pack(side=tk.LEFT, padx=5) offset_x_entry = tk.Entry(offset_frame, textvariable=self.offset_x_var, width=4) offset_x_entry.pack(side=tk.LEFT, padx=5) offset_x_entry.bind("<KeyRelease>", lambda e: self.save_offset_x_value()) # Offset Y label and input offset_y_label = tk.Label(offset_frame, text="offset_y:") offset_y_label.pack(side=tk.LEFT, padx=5) offset_y_entry = tk.Entry(offset_frame, textvariable=self.offset_y_var, width=4) offset_y_entry.pack(side=tk.LEFT, padx=5) offset_y_entry.bind("<KeyRelease>", lambda e: self.save_offset_y_value()) # Add buttons for mode switching at the bottom, centered self.mode_var = tk.StringVar() self.check_current_mode() mode_frame = tk.Frame(self.master) mode_frame.grid(row=1000, column=0, columnspan=6, pady=10) tk.Radiobutton(mode_frame, text="CUDA mode", variable=self.mode_var, value="CUDA", command=self.switch_mode).pack(side=tk.LEFT, padx=5) tk.Radiobutton(mode_frame, text="DirectML mode", variable=self.mode_var, value="DirectML", command=self.switch_mode).pack(side=tk.LEFT, padx=5) def create_offset_fields(self): """Populate offset fields in a multi-column, multi-row grid layout with aligned entry fields.""" row = 0 col = 0 max_columns = 3 for section in self.config.sections(): # Skip 'Settings' section which is not an offset if section == "Settings": continue # Section Label (Bold) label = tk.Label(self.master, text=section, font=self.bold_font) label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_value = self.config.get(section, 'offset_x', fallback='0') offset_y_value = self.config.get(section, 'offset_y', fallback='0') # Label and aligned entry for offset_x offset_x_label = tk.Label(self.master, text="offset_x:") offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry = tk.Entry(self.master, width=10) offset_x_entry.insert(0, offset_x_value) offset_x_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_x_entry: self.update_value(sec, 'offset_x', entry.get())) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Label and aligned entry for offset_y offset_y_label = tk.Label(self.master, text="offset_y:") offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry = tk.Entry(self.master, width=10) offset_y_entry.insert(0, offset_y_value) offset_y_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_y_entry: self.update_value(sec, 'offset_y', entry.get())) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Store entries for later access self.entries[section] = (label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry) col += 1 if col >= max_columns: col = 0 row += 3 # Update last used row for later layout adjustments self.last_row = row + 2 def update_value(self, section, key, value): # Set the value with no spaces around `=` self.config.set(section, key, value) self.write_config() def save_run_offset_state(self): """Save the state of the 'Run Offset 1 Time' checkbox.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'run_offset_once', str(self.run_offset_var.get())) self.write_config() def save_offset_x_value(self): """Save the value of the offset_x input entry.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'offset_x_value', self.offset_x_var.get()) self.write_config() def save_offset_y_value(self): """Save the value of the offset_y input entry.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'offset_y_value', self.offset_y_var.get()) self.write_config() def write_config(self): with open(self.ini_file, 'w') as configfile: for section in self.config.sections(): configfile.write(f'[{section}]\n') for key, val in self.config.items(section): configfile.write(f'{key}={val}\n') # No spaces around `=` configfile.write('\n') def check_current_mode(self): tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") if os.path.exists(tecogan_bat) and os.path.exists(tecogan_dml_bat): self.mode_var.set("CUDA") elif os.path.exists(tecogan_bat) and not os.path.exists(tecogan_dml_bat): self.mode_var.set("DirectML") self.hide_directml_fields() else: messagebox.showerror("Error", "Invalid batch file configuration for determining mode.") self.master.destroy() def switch_mode(self): if self.mode_var.get() == "DirectML": self.hide_directml_fields() else: self.show_all_fields() tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") tecogan_cuda_bat = os.path.join(self.batch_folder, "tecogan_cuda.bat") try: if self.mode_var.get() == "DirectML": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_cuda_bat) if os.path.exists(tecogan_dml_bat): os.rename(tecogan_dml_bat, tecogan_bat) elif self.mode_var.get() == "CUDA": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_dml_bat) if os.path.exists(tecogan_cuda_bat): os.rename(tecogan_cuda_bat, tecogan_bat) except FileNotFoundError as e: messagebox.showerror("Error", f"File not found during mode switching: {e}") except Exception as e: messagebox.showerror("Error", f"Unexpected error during mode switching: {e}") def hide_directml_fields(self): """Hide fields containing 'BVPP' or 'TG8Xa' in their name and reorganize layout.""" row = 0 col = 0 max_columns = 3 for section, widgets in self.entries.items(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets if "BVPP" in section or "TG8Xa" in section: label.grid_remove() offset_x_label.grid_remove() offset_x_entry.grid_remove() offset_y_label.grid_remove() offset_y_entry.grid_remove() else: # Re-grid remaining widgets to fill gaps label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the new layout for buttons self.last_row = row + 2 def show_all_fields(self): """Show all fields and reset layout.""" row = 0 col = 0 max_columns = 3 for widgets in self.entries.values(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the reset layout for buttons self.last_row = row + 2 if __name__ == "__main__": root = tk.Tk() editor = OffsetConfigEditor(root) root.mainloop()Key Changes:
- Removed
input_var: Eliminated the variableinput_varthat was being used to store the value of the input field. - Deleted Associated Widgets and Logic: Removed the
new_input_entrywidget and the functionsave_input_value, as they were related to theinput_valuefield. - Configuration Cleanup: Adjusted the configuration setup to exclude settings related to
input_value.
With these changes, the script focuses solely on the checkbox and
offset_xandoffset_yinput fields without any reference to theinput_valuefield. - Removed
- User
import tkinter as tk from tkinter import font, messagebox import configparser import os
class OffsetConfigEditor: def init(self, master): self.master = master self.master.title("TG - Offsets and GPU Mode Config Editor")
# Remove maximize button self.master.resizable(False, False) # Disable window resizing self.master.attributes("-toolwindow", True) # This removes the maximize button on Windows self.bold_font = font.Font(weight="bold") self.batch_folder = os.path.join("TG", "TGMAIN") self.ini_file = os.path.join(self.batch_folder, "config_tg-.ini") # Ensure batch folder exists if not os.path.exists(self.batch_folder): messagebox.showerror("Error", f"Batch folder not found: {self.batch_folder}") self.master.destroy() return # Read the config file self.config = configparser.ConfigParser() if not os.path.exists(self.ini_file): messagebox.showerror("Error", f"Configuration file not found: {self.ini_file}") self.master.destroy() return self.config.read(self.ini_file) self.entries = {} self.create_offset_fields() # Add the "Run offset correction 1 time" checkbox and input fields for offsets self.run_offset_var = tk.BooleanVar() self.offset_x_var = tk.StringVar() self.offset_y_var = tk.StringVar() # Load initial states self.run_offset_var.set(self.config.getboolean('Settings', 'run_offset_once', fallback=False)) self.offset_x_var.set(self.config.get('Settings', 'offset_x_value', fallback='')) self.offset_y_var.set(self.config.get('Settings', 'offset_y_value', fallback='')) offset_frame = tk.Frame(self.master) offset_frame.grid(row=self.last_row + 1, column=0, columnspan=6, pady=10) run_once_checkbox = tk.Checkbutton( offset_frame, text="Run offset correction only 1 time", variable=self.run_offset_var, command=self.save_run_offset_state ) run_once_checkbox.pack(side=tk.LEFT, padx=5) # Explanatory label explanation_label = tk.Label(offset_frame, text="(enable when use multiple models and input correct offset values)") explanation_label.pack(side=tk.LEFT, padx=5) # Offset X label and input offset_x_label = tk.Label(offset_frame, text="offset_x:") offset_x_label.pack(side=tk.LEFT, padx=5) offset_x_entry = tk.Entry(offset_frame, textvariable=self.offset_x_var, width=4) offset_x_entry.pack(side=tk.LEFT, padx=5) offset_x_entry.bind("<KeyRelease>", lambda e: self.save_offset_x_value()) # Offset Y label and input offset_y_label = tk.Label(offset_frame, text="offset_y:") offset_y_label.pack(side=tk.LEFT, padx=5) offset_y_entry = tk.Entry(offset_frame, textvariable=self.offset_y_var, width=4) offset_y_entry.pack(side=tk.LEFT, padx=5) offset_y_entry.bind("<KeyRelease>", lambda e: self.save_offset_y_value()) # Add buttons for mode switching at the bottom, centered self.mode_var = tk.StringVar() self.check_current_mode() mode_frame = tk.Frame(self.master) mode_frame.grid(row=1000, column=0, columnspan=6, pady=10) tk.Radiobutton(mode_frame, text="CUDA mode", variable=self.mode_var, value="CUDA", command=self.switch_mode).pack(side=tk.LEFT, padx=5) tk.Radiobutton(mode_frame, text="DirectML mode", variable=self.mode_var, value="DirectML", command=self.switch_mode).pack(side=tk.LEFT, padx=5) def create_offset_fields(self): """Populate offset fields in a multi-column, multi-row grid layout with aligned entry fields.""" row = 0 col = 0 max_columns = 3 for section in self.config.sections(): # Skip 'Settings' section which is not an offset if section == "Settings": continue # Section Label (Bold) label = tk.Label(self.master, text=section, font=self.bold_font) label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_value = self.config.get(section, 'offset_x', fallback='0') offset_y_value = self.config.get(section, 'offset_y', fallback='0') # Label and aligned entry for offset_x offset_x_label = tk.Label(self.master, text="offset_x:") offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry = tk.Entry(self.master, width=10) offset_x_entry.insert(0, offset_x_value) offset_x_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_x_entry: self.update_value(sec, 'offset_x', entry.get())) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Label and aligned entry for offset_y offset_y_label = tk.Label(self.master, text="offset_y:") offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry = tk.Entry(self.master, width=10) offset_y_entry.insert(0, offset_y_value) offset_y_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_y_entry: self.update_value(sec, 'offset_y', entry.get())) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Store entries for later access self.entries[section] = (label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry) col += 1 if col >= max_columns: col = 0 row += 3 # Update last used row for later layout adjustments self.last_row = row + 2 def update_value(self, section, key, value): # Set the value with no spaces around `=` self.config.set(section, key, value) self.write_config() def save_run_offset_state(self): """Save the state of the 'Run Offset 1 Time' checkbox.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'run_offset_once', str(self.run_offset_var.get())) self.write_config() def save_offset_x_value(self): """Save the value of the offset_x input entry.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'offset_x_value', self.offset_x_var.get()) self.write_config() def save_offset_y_value(self): """Save the value of the offset_y input entry.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'offset_y_value', self.offset_y_var.get()) self.write_config() def write_config(self): with open(self.ini_file, 'w') as configfile: for section in self.config.sections(): configfile.write(f'[{section}]\n') for key, val in self.config.items(section): configfile.write(f'{key}={val}\n') # No spaces around `=` configfile.write('\n') def check_current_mode(self): tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") if os.path.exists(tecogan_bat) and os.path.exists(tecogan_dml_bat): self.mode_var.set("CUDA") elif os.path.exists(tecogan_bat) and not os.path.exists(tecogan_dml_bat): self.mode_var.set("DirectML") self.hide_directml_fields() else: messagebox.showerror("Error", "Invalid batch file configuration for determining mode.") self.master.destroy() def switch_mode(self): if self.mode_var.get() == "DirectML": self.hide_directml_fields() else: self.show_all_fields() tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") tecogan_cuda_bat = os.path.join(self.batch_folder, "tecogan_cuda.bat") try: if self.mode_var.get() == "DirectML": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_cuda_bat) if os.path.exists(tecogan_dml_bat): os.rename(tecogan_dml_bat, tecogan_bat) elif self.mode_var.get() == "CUDA": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_dml_bat) if os.path.exists(tecogan_cuda_bat): os.rename(tecogan_cuda_bat, tecogan_bat) except FileNotFoundError as e: messagebox.showerror("Error", f"File not found during mode switching: {e}") except Exception as e: messagebox.showerror("Error", f"Unexpected error during mode switching: {e}") def hide_directml_fields(self): """Hide fields containing 'BVPP' or 'TG8Xa' in their name and reorganize layout.""" row = 0 col = 0 max_columns = 3 for section, widgets in self.entries.items(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets if "BVPP" in section or "TG8Xa" in section: label.grid_remove() offset_x_label.grid_remove() offset_x_entry.grid_remove() offset_y_label.grid_remove() offset_y_entry.grid_remove() else: # Re-grid remaining widgets to fill gaps label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the new layout for buttons self.last_row = row + 2 def show_all_fields(self): """Show all fields and reset layout.""" row = 0 col = 0 max_columns = 3 for widgets in self.entries.values(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the reset layout for buttons self.last_row = row + 2if name == "main": root = tk.Tk() editor = OffsetConfigEditor(root) root.mainloop()
- Assistant
The code you've posted is a Tkinter application for editing configurations related to offsets and GPU modes, which includes a checkbox and input fields for
offset_xandoffset_y. It seems you've removed theinput_valuefield as requested in your previous query. Here's a brief rundown of the current functionality, as well as some recommendations and clarifications:Overview:
- Checkbox: The "Run offset correction only 1 time" checkbox lets users toggle whether to run the offset correction once, and its state is saved in the configuration.
- Offset Fields: Two entry fields (
offset_xandoffset_y) allow specifying offset values, which are saved to the configuration file. - Mode Switching: The application allows switching between "CUDA" and "DirectML" modes using radio buttons, which rename corresponding batch files to control the GPU mode.
- Configuration Handling: The application reads from and writes to an
.inifile located in a specified path to maintain state between sessions.
If You Need Clarifications or Further Improvements:
- Validation: Ensure the values entered for
offset_xandoffset_yare valid (e.g., numeric) by adding validation logic. - Error Handling: Extend error handling for file operations or parsing to enhance robustness and user feedback.
- User Feedback: Consider adding more user feedback for operations, such as saving status or validation errors.
Example Additions for Validation:
You can add validation when the entries change:
def validate_offset(self, value): try: float(value) # or int(value) if you expect integer return True except ValueError: return False def on_offset_x_change(self, e): value = self.offset_x_var.get() if not self.validate_offset(value): messagebox.showwarning("Invalid Input", "Offset X must be a number.") self.offset_x_var.set("0") # Reset to a default value or previous valid state def on_offset_y_change(self, e): value = self.offset_y_var.get() if not self.validate_offset(value): messagebox.showwarning("Invalid Input", "Offset Y must be a number.") self.offset_y_var.set("0") # Reset to a default value or previous valid state # And update binding to use these validation functions offset_x_entry.bind("<KeyRelease>", self.on_offset_x_change) offset_y_entry.bind("<KeyRelease>", self.on_offset_y_change)This would handle invalid inputs by warning the user and resetting the field to a default or previous valid value.
Feel free to ask if you want to make any specific improvements or changes!
- User
I need that in those script after label "enable when use multiple models and input correct offset values" input fileds and its label would be below and not to the right
- Assistant
To modify the layout so that the input fields for
offset_xandoffset_yare placed below the explanatory label rather than beside it, you need to adjust the grid placement of the widgets in theTkinterlayout. This involves setting the row and column positions appropriately.Here’s how you can update the code to achieve the desired layout:
import tkinter as tk from tkinter import font, messagebox import configparser import os class OffsetConfigEditor: def __init__(self, master): self.master = master self.master.title("TG - Offsets and GPU Mode Config Editor") # Remove maximize button self.master.resizable(False, False) # Disable window resizing self.master.attributes("-toolwindow", True) # This removes the maximize button on Windows self.bold_font = font.Font(weight="bold") self.batch_folder = os.path.join("TG", "TGMAIN") self.ini_file = os.path.join(self.batch_folder, "config_tg-.ini") # Ensure batch folder exists if not os.path.exists(self.batch_folder): messagebox.showerror("Error", f"Batch folder not found: {self.batch_folder}") self.master.destroy() return # Read the config file self.config = configparser.ConfigParser() if not os.path.exists(self.ini_file): messagebox.showerror("Error", f"Configuration file not found: {self.ini_file}") self.master.destroy() return self.config.read(self.ini_file) self.entries = {} self.create_offset_fields() # Add the "Run offset correction 1 time" checkbox and input fields for offsets self.run_offset_var = tk.BooleanVar() self.offset_x_var = tk.StringVar() self.offset_y_var = tk.StringVar() # Load initial states self.run_offset_var.set(self.config.getboolean('Settings', 'run_offset_once', fallback=False)) self.offset_x_var.set(self.config.get('Settings', 'offset_x_value', fallback='')) self.offset_y_var.set(self.config.get('Settings', 'offset_y_value', fallback='')) offset_frame = tk.Frame(self.master) offset_frame.grid(row=self.last_row + 1, column=0, columnspan=2, pady=10) run_once_checkbox = tk.Checkbutton( offset_frame, text="Run offset correction only 1 time", variable=self.run_offset_var, command=self.save_run_offset_state ) run_once_checkbox.grid(row=0, column=0, sticky='w', padx=5) # Explanatory label explanation_label = tk.Label(offset_frame, text="(enable when use multiple models and input correct offset values)") explanation_label.grid(row=1, column=0, columnspan=2, sticky='w', padx=5) # Layout for offset_x and offset_y input fields offset_x_label = tk.Label(offset_frame, text="offset_x:") offset_x_label.grid(row=2, column=0, sticky='e', padx=5) offset_x_entry = tk.Entry(offset_frame, textvariable=self.offset_x_var, width=8) offset_x_entry.grid(row=2, column=1, sticky='w', padx=5) offset_x_entry.bind("<KeyRelease>", lambda e: self.save_offset_x_value()) offset_y_label = tk.Label(offset_frame, text="offset_y:") offset_y_label.grid(row=3, column=0, sticky='e', padx=5) offset_y_entry = tk.Entry(offset_frame, textvariable=self.offset_y_var, width=8) offset_y_entry.grid(row=3, column=1, sticky='w', padx=5) offset_y_entry.bind("<KeyRelease>", lambda e: self.save_offset_y_value()) # Add buttons for mode switching at the bottom, centered self.mode_var = tk.StringVar() self.check_current_mode() mode_frame = tk.Frame(self.master) mode_frame.grid(row=1000, column=0, columnspan=2, pady=10) tk.Radiobutton(mode_frame, text="CUDA mode", variable=self.mode_var, value="CUDA", command=self.switch_mode).pack(side=tk.LEFT, padx=5) tk.Radiobutton(mode_frame, text="DirectML mode", variable=self.mode_var, value="DirectML", command=self.switch_mode).pack(side=tk.LEFT, padx=5) def create_offset_fields(self): """Populate offset fields in a multi-column, multi-row grid layout with aligned entry fields.""" row = 0 col = 0 max_columns = 3 for section in self.config.sections(): # Skip 'Settings' section which is not an offset if section == "Settings": continue # Section Label (Bold) label = tk.Label(self.master, text=section, font=self.bold_font) label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_value = self.config.get(section, 'offset_x', fallback='0') offset_y_value = self.config.get(section, 'offset_y', fallback='0') # Label and aligned entry for offset_x offset_x_label = tk.Label(self.master, text="offset_x:") offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry = tk.Entry(self.master, width=10) offset_x_entry.insert(0, offset_x_value) offset_x_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_x_entry: self.update_value(sec, 'offset_x', entry.get())) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Label and aligned entry for offset_y offset_y_label = tk.Label(self.master, text="offset_y:") offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry = tk.Entry(self.master, width=10) offset_y_entry.insert(0, offset_y_value) offset_y_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_y_entry: self.update_value(sec, 'offset_y', entry.get())) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Store entries for later access self.entries[section] = (label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry) col += 1 if col >= max_columns: col = 0 row += 3 # Update last used row for later layout adjustments self.last_row = row + 2 def update_value(self, section, key, value): # Set the value with no spaces around `=` self.config.set(section, key, value) self.write_config() def save_run_offset_state(self): """Save the state of the 'Run Offset 1 Time' checkbox.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'run_offset_once', str(self.run_offset_var.get())) self.write_config() def save_offset_x_value(self): """Save the value of the offset_x input entry.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'offset_x_value', self.offset_x_var.get()) self.write_config() def save_offset_y_value(self): """Save the value of the offset_y input entry.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'offset_y_value', self.offset_y_var.get()) self.write_config() def write_config(self): with open(self.ini_file, 'w') as configfile: for section in self.config.sections(): configfile.write(f'[{section}]\n') for key, val in self.config.items(section): configfile.write(f'{key}={val}\n') # No spaces around `=` configfile.write('\n') def check_current_mode(self): tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") if os.path.exists(tecogan_bat) and os.path.exists(tecogan_dml_bat): self.mode_var.set("CUDA") elif os.path.exists(tecogan_bat) and not os.path.exists(tecogan_dml_bat): self.mode_var.set("DirectML") self.hide_directml_fields() else: messagebox.showerror("Error", "Invalid batch file configuration for determining mode.") self.master.destroy() def switch_mode(self): if self.mode_var.get() == "DirectML": self.hide_directml_fields() else: self.show_all_fields() tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") tecogan_cuda_bat = os.path.join(self.batch_folder, "tecogan_cuda.bat") try: if self.mode_var.get() == "DirectML": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_cuda_bat) if os.path.exists(tecogan_dml_bat): os.rename(tecogan_dml_bat, tecogan_bat) elif self.mode_var.get() == "CUDA": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_dml_bat) if os.path.exists(tecogan_cuda_bat): os.rename(tecogan_cuda_bat, tecogan_bat) except FileNotFoundError as e: messagebox.showerror("Error", f"File not found during mode switching: {e}") except Exception as e: messagebox.showerror("Error", f"Unexpected error during mode switching: {e}") def hide_directml_fields(self): """Hide fields containing 'BVPP' or 'TG8Xa' in their name and reorganize layout.""" row = 0 col = 0 max_columns = 3 for section, widgets in self.entries.items(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets if "BVPP" in section or "TG8Xa" in section: label.grid_remove() offset_x_label.grid_remove() offset_x_entry.grid_remove() offset_y_label.grid_remove() offset_y_entry.grid_remove() else: # Re-grid remaining widgets to fill gaps label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the new layout for buttons self.last_row = row + 2 def show_all_fields(self): """Show all fields and reset layout.""" row = 0 col = 0 max_columns = 3 for widgets in self.entries.values(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the reset layout for buttons self.last_row = row + 2 if __name__ == "__main__": root = tk.Tk() editor = OffsetConfigEditor(root) root.mainloop()Key Layout Changes:
- The checkbox and the explanatory label are now on the first row of
offset_frame. - The
offset_xlabel and entry are placed on the second row ofoffset_frame. - The
offset_ylabel and entry are placed on the third row ofoffset_frame.
This layout adjustment organizes the components vertically, which keeps the interface cleaner and aligned when you want fields directly below a related label.
- The checkbox and the explanatory label are now on the first row of
- User
no, label "enable when use multiple models and input correct offset values" shold be same line (to the right) as "Run offset correction only 1 time"
- Assistant
To arrange the widgets so that the explanatory label is on the same line as the "Run offset correction only 1 time" checkbox, while moving the
offset_xandoffset_ylabels and their input fields below, you only need to adjust their grid positioning.Here's how you can achieve the desired layout:
import tkinter as tk from tkinter import font, messagebox import configparser import os class OffsetConfigEditor: def __init__(self, master): self.master = master self.master.title("TG - Offsets and GPU Mode Config Editor") # Remove maximize button self.master.resizable(False, False) # Disable window resizing self.master.attributes("-toolwindow", True) # This removes the maximize button on Windows self.bold_font = font.Font(weight="bold") self.batch_folder = os.path.join("TG", "TGMAIN") self.ini_file = os.path.join(self.batch_folder, "config_tg-.ini") # Ensure batch folder exists if not os.path.exists(self.batch_folder): messagebox.showerror("Error", f"Batch folder not found: {self.batch_folder}") self.master.destroy() return # Read the config file self.config = configparser.ConfigParser() if not os.path.exists(self.ini_file): messagebox.showerror("Error", f"Configuration file not found: {self.ini_file}") self.master.destroy() return self.config.read(self.ini_file) self.entries = {} self.create_offset_fields() # Add the "Run offset correction 1 time" checkbox and input fields for offsets self.run_offset_var = tk.BooleanVar() self.offset_x_var = tk.StringVar() self.offset_y_var = tk.StringVar() # Load initial states self.run_offset_var.set(self.config.getboolean('Settings', 'run_offset_once', fallback=False)) self.offset_x_var.set(self.config.get('Settings', 'offset_x_value', fallback='')) self.offset_y_var.set(self.config.get('Settings', 'offset_y_value', fallback='')) offset_frame = tk.Frame(self.master) offset_frame.grid(row=self.last_row + 1, column=0, columnspan=2, pady=10) # Checkbox and explanatory label on the same row run_once_checkbox = tk.Checkbutton( offset_frame, text="Run offset correction only 1 time", variable=self.run_offset_var, command=self.save_run_offset_state ) run_once_checkbox.grid(row=0, column=0, sticky='w', padx=5) explanation_label = tk.Label(offset_frame, text="(enable when use multiple models and input correct offset values)") explanation_label.grid(row=0, column=1, sticky='w', padx=5) # Offset X label and input on next row offset_x_label = tk.Label(offset_frame, text="offset_x:") offset_x_label.grid(row=1, column=0, sticky='e', padx=5) offset_x_entry = tk.Entry(offset_frame, textvariable=self.offset_x_var, width=8) offset_x_entry.grid(row=1, column=1, sticky='w', padx=5) offset_x_entry.bind("<KeyRelease>", lambda e: self.save_offset_x_value()) # Offset Y label and input on the following row offset_y_label = tk.Label(offset_frame, text="offset_y:") offset_y_label.grid(row=2, column=0, sticky='e', padx=5) offset_y_entry = tk.Entry(offset_frame, textvariable=self.offset_y_var, width=8) offset_y_entry.grid(row=2, column=1, sticky='w', padx=5) offset_y_entry.bind("<KeyRelease>", lambda e: self.save_offset_y_value()) # Add buttons for mode switching at the bottom, centered self.mode_var = tk.StringVar() self.check_current_mode() mode_frame = tk.Frame(self.master) mode_frame.grid(row=1000, column=0, columnspan=2, pady=10) tk.Radiobutton(mode_frame, text="CUDA mode", variable=self.mode_var, value="CUDA", command=self.switch_mode).pack(side=tk.LEFT, padx=5) tk.Radiobutton(mode_frame, text="DirectML mode", variable=self.mode_var, value="DirectML", command=self.switch_mode).pack(side=tk.LEFT, padx=5) def create_offset_fields(self): """Populate offset fields in a multi-column, multi-row grid layout with aligned entry fields.""" row = 0 col = 0 max_columns = 3 for section in self.config.sections(): # Skip 'Settings' section which is not an offset if section == "Settings": continue # Section Label (Bold) label = tk.Label(self.master, text=section, font=self.bold_font) label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_value = self.config.get(section, 'offset_x', fallback='0') offset_y_value = self.config.get(section, 'offset_y', fallback='0') # Label and aligned entry for offset_x offset_x_label = tk.Label(self.master, text="offset_x:") offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry = tk.Entry(self.master, width=10) offset_x_entry.insert(0, offset_x_value) offset_x_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_x_entry: self.update_value(sec, 'offset_x', entry.get())) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Label and aligned entry for offset_y offset_y_label = tk.Label(self.master, text="offset_y:") offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry = tk.Entry(self.master, width=10) offset_y_entry.insert(0, offset_y_value) offset_y_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_y_entry: self.update_value(sec, 'offset_y', entry.get())) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Store entries for later access self.entries[section] = (label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry) col += 1 if col >= max_columns: col = 0 row += 3 # Update last used row for later layout adjustments self.last_row = row + 2 def update_value(self, section, key, value): # Set the value with no spaces around `=` self.config.set(section, key, value) self.write_config() def save_run_offset_state(self): """Save the state of the 'Run Offset 1 Time' checkbox.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'run_offset_once', str(self.run_offset_var.get())) self.write_config() def save_offset_x_value(self): """Save the value of the offset_x input entry.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'offset_x_value', self.offset_x_var.get()) self.write_config() def save_offset_y_value(self): """Save the value of the offset_y input entry.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'offset_y_value', self.offset_y_var.get()) self.write_config() def write_config(self): with open(self.ini_file, 'w') as configfile: for section in self.config.sections(): configfile.write(f'[{section}]\n') for key, val in self.config.items(section): configfile.write(f'{key}={val}\n') # No spaces around `=` configfile.write('\n') def check_current_mode(self): tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") if os.path.exists(tecogan_bat) and os.path.exists(tecogan_dml_bat): self.mode_var.set("CUDA") elif os.path.exists(tecogan_bat) and not os.path.exists(tecogan_dml_bat): self.mode_var.set("DirectML") self.hide_directml_fields() else: messagebox.showerror("Error", "Invalid batch file configuration for determining mode.") self.master.destroy() def switch_mode(self): if self.mode_var.get() == "DirectML": self.hide_directml_fields() else: self.show_all_fields() tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") tecogan_cuda_bat = os.path.join(self.batch_folder, "tecogan_cuda.bat") try: if self.mode_var.get() == "DirectML": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_cuda_bat) if os.path.exists(tecogan_dml_bat): os.rename(tecogan_dml_bat, tecogan_bat) elif self.mode_var.get() == "CUDA": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_dml_bat) if os.path.exists(tecogan_cuda_bat): os.rename(tecogan_cuda_bat, tecogan_bat) except FileNotFoundError as e: messagebox.showerror("Error", f"File not found during mode switching: {e}") except Exception as e: messagebox.showerror("Error", f"Unexpected error during mode switching: {e}") def hide_directml_fields(self): """Hide fields containing 'BVPP' or 'TG8Xa' in their name and reorganize layout.""" row = 0 col = 0 max_columns = 3 for section, widgets in self.entries.items(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets if "BVPP" in section or "TG8Xa" in section: label.grid_remove() offset_x_label.grid_remove() offset_x_entry.grid_remove() offset_y_label.grid_remove() offset_y_entry.grid_remove() else: # Re-grid remaining widgets to fill gaps label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the new layout for buttons self.last_row = row + 2 def show_all_fields(self): """Show all fields and reset layout.""" row = 0 col = 0 max_columns = 3 for widgets in self.entries.values(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the reset layout for buttons self.last_row = row + 2 if __name__ == "__main__": root = tk.Tk() editor = OffsetConfigEditor(root) root.mainloop()Key Layout Adjustments:
- The checkbox and the explanatory label are placed on the same row by setting their grid positions as
row=0and different columns. - The
offset_xandoffset_yfields with labels are on separate subsequent rows, ensuring they appear below the explanatory label.
This arrangement will keep the checkbox and label on one line, with offset fields below them.
- The checkbox and the explanatory label are placed on the same row by setting their grid positions as
- User
make 4 columns instead of 3: import tkinter as tk from tkinter import font, messagebox import configparser import os
class OffsetConfigEditor: def init(self, master): self.master = master self.master.title("TG - Offsets and GPU Mode Config Editor")
# Remove maximize button self.master.resizable(False, False) # Disable window resizing self.master.attributes("-toolwindow", True) # This removes the maximize button on Windows self.bold_font = font.Font(weight="bold") self.batch_folder = os.path.join("TG", "TGMAIN") self.ini_file = os.path.join(self.batch_folder, "config_tg-.ini") # Ensure batch folder exists if not os.path.exists(self.batch_folder): messagebox.showerror("Error", f"Batch folder not found: {self.batch_folder}") self.master.destroy() return # Read the config file self.config = configparser.ConfigParser() if not os.path.exists(self.ini_file): messagebox.showerror("Error", f"Configuration file not found: {self.ini_file}") self.master.destroy() return self.config.read(self.ini_file) self.entries = {} self.create_offset_fields() # Add the "Run offset correction 1 time" checkbox and input fields for offsets self.run_offset_var = tk.BooleanVar() self.offset_x_var = tk.StringVar() self.offset_y_var = tk.StringVar() # Load initial states self.run_offset_var.set(self.config.getboolean('Settings', 'run_offset_once', fallback=False)) self.offset_x_var.set(self.config.get('Settings', 'offset_x_value', fallback='')) self.offset_y_var.set(self.config.get('Settings', 'offset_y_value', fallback='')) offset_frame = tk.Frame(self.master) offset_frame.grid(row=self.last_row + 1, column=0, columnspan=6, pady=10) run_once_checkbox = tk.Checkbutton( offset_frame, text="Run offset correction only 1 time", variable=self.run_offset_var, command=self.save_run_offset_state ) run_once_checkbox.pack(side=tk.LEFT, padx=5) # Explanatory label explanation_label = tk.Label(offset_frame, text="(enable when use multiple models and input correct offset values)") explanation_label.pack(side=tk.LEFT, padx=5) # Offset X label and input offset_x_label = tk.Label(offset_frame, text="offset_x:") offset_x_label.pack(side=tk.LEFT, padx=5) offset_x_entry = tk.Entry(offset_frame, textvariable=self.offset_x_var, width=4) offset_x_entry.pack(side=tk.LEFT, padx=5) offset_x_entry.bind("<KeyRelease>", lambda e: self.save_offset_x_value()) # Offset Y label and input offset_y_label = tk.Label(offset_frame, text="offset_y:") offset_y_label.pack(side=tk.LEFT, padx=5) offset_y_entry = tk.Entry(offset_frame, textvariable=self.offset_y_var, width=4) offset_y_entry.pack(side=tk.LEFT, padx=5) offset_y_entry.bind("<KeyRelease>", lambda e: self.save_offset_y_value()) # Add buttons for mode switching at the bottom, centered self.mode_var = tk.StringVar() self.check_current_mode() mode_frame = tk.Frame(self.master) mode_frame.grid(row=1000, column=0, columnspan=6, pady=10) tk.Radiobutton(mode_frame, text="CUDA mode", variable=self.mode_var, value="CUDA", command=self.switch_mode).pack(side=tk.LEFT, padx=5) tk.Radiobutton(mode_frame, text="DirectML mode", variable=self.mode_var, value="DirectML", command=self.switch_mode).pack(side=tk.LEFT, padx=5) def create_offset_fields(self): """Populate offset fields in a multi-column, multi-row grid layout with aligned entry fields.""" row = 0 col = 0 max_columns = 3 for section in self.config.sections(): # Skip 'Settings' section which is not an offset if section == "Settings": continue # Section Label (Bold) label = tk.Label(self.master, text=section, font=self.bold_font) label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_value = self.config.get(section, 'offset_x', fallback='0') offset_y_value = self.config.get(section, 'offset_y', fallback='0') # Label and aligned entry for offset_x offset_x_label = tk.Label(self.master, text="offset_x:") offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry = tk.Entry(self.master, width=10) offset_x_entry.insert(0, offset_x_value) offset_x_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_x_entry: self.update_value(sec, 'offset_x', entry.get())) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Label and aligned entry for offset_y offset_y_label = tk.Label(self.master, text="offset_y:") offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry = tk.Entry(self.master, width=10) offset_y_entry.insert(0, offset_y_value) offset_y_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_y_entry: self.update_value(sec, 'offset_y', entry.get())) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Store entries for later access self.entries[section] = (label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry) col += 1 if col >= max_columns: col = 0 row += 3 # Update last used row for later layout adjustments self.last_row = row + 2 def update_value(self, section, key, value): # Set the value with no spaces around `=` self.config.set(section, key, value) self.write_config() def save_run_offset_state(self): """Save the state of the 'Run Offset 1 Time' checkbox.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'run_offset_once', str(self.run_offset_var.get())) self.write_config() def save_offset_x_value(self): """Save the value of the offset_x input entry.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'offset_x_value', self.offset_x_var.get()) self.write_config() def save_offset_y_value(self): """Save the value of the offset_y input entry.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'offset_y_value', self.offset_y_var.get()) self.write_config() def write_config(self): with open(self.ini_file, 'w') as configfile: for section in self.config.sections(): configfile.write(f'[{section}]\n') for key, val in self.config.items(section): configfile.write(f'{key}={val}\n') # No spaces around `=` configfile.write('\n') def check_current_mode(self): tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") if os.path.exists(tecogan_bat) and os.path.exists(tecogan_dml_bat): self.mode_var.set("CUDA") elif os.path.exists(tecogan_bat) and not os.path.exists(tecogan_dml_bat): self.mode_var.set("DirectML") self.hide_directml_fields() else: messagebox.showerror("Error", "Invalid batch file configuration for determining mode.") self.master.destroy() def switch_mode(self): if self.mode_var.get() == "DirectML": self.hide_directml_fields() else: self.show_all_fields() tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") tecogan_cuda_bat = os.path.join(self.batch_folder, "tecogan_cuda.bat") try: if self.mode_var.get() == "DirectML": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_cuda_bat) if os.path.exists(tecogan_dml_bat): os.rename(tecogan_dml_bat, tecogan_bat) elif self.mode_var.get() == "CUDA": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_dml_bat) if os.path.exists(tecogan_cuda_bat): os.rename(tecogan_cuda_bat, tecogan_bat) except FileNotFoundError as e: messagebox.showerror("Error", f"File not found during mode switching: {e}") except Exception as e: messagebox.showerror("Error", f"Unexpected error during mode switching: {e}") def hide_directml_fields(self): """Hide fields containing 'BVPP' or 'TG8Xa' in their name and reorganize layout.""" row = 0 col = 0 max_columns = 3 for section, widgets in self.entries.items(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets if "BVPP" in section or "TG8Xa" in section: label.grid_remove() offset_x_label.grid_remove() offset_x_entry.grid_remove() offset_y_label.grid_remove() offset_y_entry.grid_remove() else: # Re-grid remaining widgets to fill gaps label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the new layout for buttons self.last_row = row + 2 def show_all_fields(self): """Show all fields and reset layout.""" row = 0 col = 0 max_columns = 3 for widgets in self.entries.values(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the reset layout for buttons self.last_row = row + 2if name == "main": root = tk.Tk() editor = OffsetConfigEditor(root) root.mainloop()
- Assistant
To modify your script to use four columns instead of three for organizing the input fields in the grid, you'll need to adjust the logic in the
create_offset_fieldsmethod to accommodate four columns per row. This change primarily affects how you increment thecolandrowcounters when placing items.Here's how you can make these changes:
import tkinter as tk from tkinter import font, messagebox import configparser import os class OffsetConfigEditor: def __init__(self, master): self.master = master self.master.title("TG - Offsets and GPU Mode Config Editor") # Remove maximize button self.master.resizable(False, False) # Disable window resizing self.master.attributes("-toolwindow", True) # This removes the maximize button on Windows self.bold_font = font.Font(weight="bold") self.batch_folder = os.path.join("TG", "TGMAIN") self.ini_file = os.path.join(self.batch_folder, "config_tg-.ini") # Ensure batch folder exists if not os.path.exists(self.batch_folder): messagebox.showerror("Error", f"Batch folder not found: {self.batch_folder}") self.master.destroy() return # Read the config file self.config = configparser.ConfigParser() if not os.path.exists(self.ini_file): messagebox.showerror("Error", f"Configuration file not found: {self.ini_file}") self.master.destroy() return self.config.read(self.ini_file) self.entries = {} self.create_offset_fields() # Add the "Run offset correction 1 time" checkbox and input fields for offsets self.run_offset_var = tk.BooleanVar() self.offset_x_var = tk.StringVar() self.offset_y_var = tk.StringVar() # Load initial states self.run_offset_var.set(self.config.getboolean('Settings', 'run_offset_once', fallback=False)) self.offset_x_var.set(self.config.get('Settings', 'offset_x_value', fallback='')) self.offset_y_var.set(self.config.get('Settings', 'offset_y_value', fallback='')) offset_frame = tk.Frame(self.master) offset_frame.grid(row=self.last_row + 1, column=0, columnspan=6, pady=10) run_once_checkbox = tk.Checkbutton( offset_frame, text="Run offset correction only 1 time", variable=self.run_offset_var, command=self.save_run_offset_state ) run_once_checkbox.pack(side=tk.LEFT, padx=5) # Explanatory label explanation_label = tk.Label(offset_frame, text="(enable when use multiple models and input correct offset values)") explanation_label.pack(side=tk.LEFT, padx=5) # Offset X label and input offset_x_label = tk.Label(offset_frame, text="offset_x:") offset_x_label.pack(side=tk.LEFT, padx=5) offset_x_entry = tk.Entry(offset_frame, textvariable=self.offset_x_var, width=4) offset_x_entry.pack(side=tk.LEFT, padx=5) offset_x_entry.bind("<KeyRelease>", lambda e: self.save_offset_x_value()) # Offset Y label and input offset_y_label = tk.Label(offset_frame, text="offset_y:") offset_y_label.pack(side=tk.LEFT, padx=5) offset_y_entry = tk.Entry(offset_frame, textvariable=self.offset_y_var, width=4) offset_y_entry.pack(side=tk.LEFT, padx=5) offset_y_entry.bind("<KeyRelease>", lambda e: self.save_offset_y_value()) # Add buttons for mode switching at the bottom, centered self.mode_var = tk.StringVar() self.check_current_mode() mode_frame = tk.Frame(self.master) mode_frame.grid(row=1000, column=0, columnspan=6, pady=10) tk.Radiobutton(mode_frame, text="CUDA mode", variable=self.mode_var, value="CUDA", command=self.switch_mode).pack(side=tk.LEFT, padx=5) tk.Radiobutton(mode_frame, text="DirectML mode", variable=self.mode_var, value="DirectML", command=self.switch_mode).pack(side=tk.LEFT, padx=5) def create_offset_fields(self): """Populate offset fields in a multi-column, multi-row grid layout with aligned entry fields.""" row = 0 col = 0 max_columns = 4 # Change from 3 to 4 for section in self.config.sections(): # Skip 'Settings' section which is not an offset if section == "Settings": continue # Section Label (Bold) label = tk.Label(self.master, text=section, font=self.bold_font) label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_value = self.config.get(section, 'offset_x', fallback='0') offset_y_value = self.config.get(section, 'offset_y', fallback='0') # Label and aligned entry for offset_x offset_x_label = tk.Label(self.master, text="offset_x:") offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry = tk.Entry(self.master, width=10) offset_x_entry.insert(0, offset_x_value) offset_x_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_x_entry: self.update_value(sec, 'offset_x', entry.get())) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Label and aligned entry for offset_y offset_y_label = tk.Label(self.master, text="offset_y:") offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry = tk.Entry(self.master, width=10) offset_y_entry.insert(0, offset_y_value) offset_y_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_y_entry: self.update_value(sec, 'offset_y', entry.get())) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Store entries for later access self.entries[section] = (label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry) col += 1 if col >= max_columns: col = 0 row += 3 # Update last used row for later layout adjustments self.last_row = row + 2 def update_value(self, section, key, value): self.config.set(section, key, value) self.write_config() def save_run_offset_state(self): """Save the state of the 'Run Offset 1 Time' checkbox.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'run_offset_once', str(self.run_offset_var.get())) self.write_config() def save_offset_x_value(self): """Save the value of the offset_x input entry.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'offset_x_value', self.offset_x_var.get()) self.write_config() def save_offset_y_value(self): """Save the value of the offset_y input entry.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'offset_y_value', self.offset_y_var.get()) self.write_config() def write_config(self): with open(self.ini_file, 'w') as configfile: for section in self.config.sections(): configfile.write(f'[{section}]\n') for key, val in self.config.items(section): configfile.write(f'{key}={val}\n') # No spaces around `=` configfile.write('\n') def check_current_mode(self): tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") if os.path.exists(tecogan_bat) and os.path.exists(tecogan_dml_bat): self.mode_var.set("CUDA") elif os.path.exists(tecogan_bat) and not os.path.exists(tecogan_dml_bat): self.mode_var.set("DirectML") self.hide_directml_fields() else: messagebox.showerror("Error", "Invalid batch file configuration for determining mode.") self.master.destroy() def switch_mode(self): if self.mode_var.get() == "DirectML": self.hide_directml_fields() else: self.show_all_fields() tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") tecogan_cuda_bat = os.path.join(self.batch_folder, "tecogan_cuda.bat") try: if self.mode_var.get() == "DirectML": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_cuda_bat) if os.path.exists(tecogan_dml_bat): os.rename(tecogan_dml_bat, tecogan_bat) elif self.mode_var.get() == "CUDA": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_dml_bat) if os.path.exists(tecogan_cuda_bat): os.rename(tecogan_cuda_bat, tecogan_bat) except FileNotFoundError as e: messagebox.showerror("Error", f"File not found during mode switching: {e}") except Exception as e: messagebox.showerror("Error", f"Unexpected error during mode switching: {e}") def hide_directml_fields(self): """Hide fields containing 'BVPP' or 'TG8Xa' in their name and reorganize layout.""" row = 0 col = 0 max_columns = 4 # Change from 3 to 4 for section, widgets in self.entries.items(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets if "BVPP" in section or "TG8Xa" in section: label.grid_remove() offset_x_label.grid_remove() offset_x_entry.grid_remove() offset_y_label.grid_remove() offset_y_entry.grid_remove() else: # Re-grid remaining widgets to fill gaps label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the new layout for buttons self.last_row = row + 2 def show_all_fields(self): """Show all fields and reset layout.""" row = 0 col = 0 max_columns = 4 # Change from 3 to 4 for widgets in self.entries.values(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the reset layout for buttons self.last_row = row + 2 if __name__ == "__main__": root = tk.Tk() editor = OffsetConfigEditor(root) root.mainloop()Changes Made:
- Increased Columns: Changed
max_columnsfrom 3 to 4 in all relevant methods (create_offset_fields,hide_directml_fields,show_all_fields) to adjust the grid layout to support four columns instead of three. - Column Spacing: The use of
col * 2for label positioning handles a single label/entry pair, but ensure you're observing how columns are consumed since each pair uses two columns.
This adjustment will organize the fields into four columns instead of three, allowing more entries in a single row and making better use of screen space if more sections are present.
- Increased Columns: Changed
- User
make when I enable run offset correction then it would rename at relative path TG\TGMAIN 2 .bat files to tecogan.bat to tecogan_offset.bat, tecogan_no_offset.bat to tecogan.bat and disable CUDA or DML mode changing. when disable offset correction then revert changes and enable mode changing.
- Assistant
To implement your requirements, you need to modify the behavior of the checkbox that toggles the "Run offset correction only 1 time" feature. Specifically, when enabling the checkbox, the script should rename batch files to enable offset correction and disable CUDA or DML mode switching. Conversely, when disabling offset correction, it should rename the files back and re-enable mode switching.
Here is how you can update your script:
import tkinter as tk from tkinter import font, messagebox import configparser import os class OffsetConfigEditor: def __init__(self, master): self.master = master self.master.title("TG - Offsets and GPU Mode Config Editor") # Remove maximize button self.master.resizable(False, False) # Disable window resizing self.master.attributes("-toolwindow", True) # This removes the maximize button on Windows self.bold_font = font.Font(weight="bold") self.batch_folder = os.path.join("TG", "TGMAIN") self.ini_file = os.path.join(self.batch_folder, "config_tg-.ini") # Ensure batch folder exists if not os.path.exists(self.batch_folder): messagebox.showerror("Error", f"Batch folder not found: {self.batch_folder}") self.master.destroy() return # Read the config file self.config = configparser.ConfigParser() if not os.path.exists(self.ini_file): messagebox.showerror("Error", f"Configuration file not found: {self.ini_file}") self.master.destroy() return self.config.read(self.ini_file) self.entries = {} self.create_offset_fields() # Add the "Run offset correction 1 time" checkbox and input fields for offsets self.run_offset_var = tk.BooleanVar() self.offset_x_var = tk.StringVar() self.offset_y_var = tk.StringVar() # Load initial states self.run_offset_var.set(self.config.getboolean('Settings', 'run_offset_once', fallback=False)) self.offset_x_var.set(self.config.get('Settings', 'offset_x_value', fallback='')) self.offset_y_var.set(self.config.get('Settings', 'offset_y_value', fallback='')) offset_frame = tk.Frame(self.master) offset_frame.grid(row=self.last_row + 1, column=0, columnspan=6, pady=10) run_once_checkbox = tk.Checkbutton( offset_frame, text="Run offset correction only 1 time", variable=self.run_offset_var, command=self.toggle_offset_correction ) run_once_checkbox.pack(side=tk.LEFT, padx=5) # Explanatory label explanation_label = tk.Label(offset_frame, text="(enable when use multiple models and input correct offset values)") explanation_label.pack(side=tk.LEFT, padx=5) # Offset X label and input offset_x_label = tk.Label(offset_frame, text="offset_x:") offset_x_label.pack(side=tk.LEFT, padx=5) offset_x_entry = tk.Entry(offset_frame, textvariable=self.offset_x_var, width=4) offset_x_entry.pack(side=tk.LEFT, padx=5) offset_x_entry.bind("<KeyRelease>", lambda e: self.save_offset_x_value()) # Offset Y label and input offset_y_label = tk.Label(offset_frame, text="offset_y:") offset_y_label.pack(side=tk.LEFT, padx=5) offset_y_entry = tk.Entry(offset_frame, textvariable=self.offset_y_var, width=4) offset_y_entry.pack(side=tk.LEFT, padx=5) offset_y_entry.bind("<KeyRelease>", lambda e: self.save_offset_y_value()) # Add buttons for mode switching at the bottom, centered self.mode_var = tk.StringVar() self.check_current_mode() mode_frame = tk.Frame(self.master) mode_frame.grid(row=1000, column=0, columnspan=6, pady=10) self.cuda_radio = tk.Radiobutton(mode_frame, text="CUDA mode", variable=self.mode_var, value="CUDA", command=self.switch_mode) self.cuda_radio.pack(side=tk.LEFT, padx=5) self.dml_radio = tk.Radiobutton(mode_frame, text="DirectML mode", variable=self.mode_var, value="DirectML", command=self.switch_mode) self.dml_radio.pack(side=tk.LEFT, padx=5) def create_offset_fields(self): """Populate offset fields in a multi-column, multi-row grid layout with aligned entry fields.""" row = 0 col = 0 max_columns = 4 for section in self.config.sections(): # Skip 'Settings' section which is not an offset if section == "Settings": continue # Section Label (Bold) label = tk.Label(self.master, text=section, font=self.bold_font) label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_value = self.config.get(section, 'offset_x', fallback='0') offset_y_value = self.config.get(section, 'offset_y', fallback='0') # Label and aligned entry for offset_x offset_x_label = tk.Label(self.master, text="offset_x:") offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry = tk.Entry(self.master, width=10) offset_x_entry.insert(0, offset_x_value) offset_x_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_x_entry: self.update_value(sec, 'offset_x', entry.get())) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Label and aligned entry for offset_y offset_y_label = tk.Label(self.master, text="offset_y:") offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry = tk.Entry(self.master, width=10) offset_y_entry.insert(0, offset_y_value) offset_y_entry.bind("<KeyRelease>", lambda e, sec=section, entry=offset_y_entry: self.update_value(sec, 'offset_y', entry.get())) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) # Store entries for later access self.entries[section] = (label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry) col += 1 if col >= max_columns: col = 0 row += 3 # Update last used row for later layout adjustments self.last_row = row + 2 def update_value(self, section, key, value): self.config.set(section, key, value) self.write_config() def toggle_offset_correction(self): """Toggle offset correction and manage related file renaming.""" tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_offset_bat = os.path.join(self.batch_folder, "tecogan_offset.bat") tecogan_no_offset_bat = os.path.join(self.batch_folder, "tecogan_no_offset.bat") try: if self.run_offset_var.get(): if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_offset_bat) if os.path.exists(tecogan_no_offset_bat): os.rename(tecogan_no_offset_bat, tecogan_bat) # Disable mode switching self.cuda_radio.config(state=tk.DISABLED) self.dml_radio.config(state=tk.DISABLED) else: if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_no_offset_bat) if os.path.exists(tecogan_offset_bat): os.rename(tecogan_offset_bat, tecogan_bat) # Enable mode switching self.cuda_radio.config(state=tk.NORMAL) self.dml_radio.config(state=tk.NORMAL) # Save state to config self.save_run_offset_state() except FileNotFoundError as e: messagebox.showerror("Error", f"File not found during toggling offset correction: {e}") except Exception as e: messagebox.showerror("Error", f"Unexpected error during toggling offset correction: {e}") def save_run_offset_state(self): """Save the state of the 'Run Offset 1 Time' checkbox.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'run_offset_once', str(self.run_offset_var.get())) self.write_config() def save_offset_x_value(self): """Save the value of the offset_x input entry.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'offset_x_value', self.offset_x_var.get()) self.write_config() def save_offset_y_value(self): """Save the value of the offset_y input entry.""" if not self.config.has_section('Settings'): self.config.add_section('Settings') self.config.set('Settings', 'offset_y_value', self.offset_y_var.get()) self.write_config() def write_config(self): with open(self.ini_file, 'w') as configfile: for section in self.config.sections(): configfile.write(f'[{section}]\n') for key, val in self.config.items(section): configfile.write(f'{key}={val}\n') # No spaces around `=` configfile.write('\n') def check_current_mode(self): tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") if os.path.exists(tecogan_bat) and os.path.exists(tecogan_dml_bat): self.mode_var.set("CUDA") elif os.path.exists(tecogan_bat) and not os.path.exists(tecogan_dml_bat): self.mode_var.set("DirectML") self.hide_directml_fields() else: messagebox.showerror("Error", "Invalid batch file configuration for determining mode.") self.master.destroy() def switch_mode(self): if self.mode_var.get() == "DirectML": self.hide_directml_fields() else: self.show_all_fields() tecogan_bat = os.path.join(self.batch_folder, "tecogan.bat") tecogan_dml_bat = os.path.join(self.batch_folder, "tecogan_dml.bat") tecogan_cuda_bat = os.path.join(self.batch_folder, "tecogan_cuda.bat") try: if self.mode_var.get() == "DirectML": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_cuda_bat) if os.path.exists(tecogan_dml_bat): os.rename(tecogan_dml_bat, tecogan_bat) elif self.mode_var.get() == "CUDA": if os.path.exists(tecogan_bat): os.rename(tecogan_bat, tecogan_dml_bat) if os.path.exists(tecogan_cuda_bat): os.rename(tecogan_cuda_bat, tecogan_bat) except FileNotFoundError as e: messagebox.showerror("Error", f"File not found during mode switching: {e}") except Exception as e: messagebox.showerror("Error", f"Unexpected error during mode switching: {e}") def hide_directml_fields(self): """Hide fields containing 'BVPP' or 'TG8Xa' in their name and reorganize layout.""" row = 0 col = 0 max_columns = 4 for section, widgets in self.entries.items(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets if "BVPP" in section or "TG8Xa" in section: label.grid_remove() offset_x_label.grid_remove() offset_x_entry.grid_remove() offset_y_label.grid_remove() offset_y_entry.grid_remove() else: # Re-grid remaining widgets to fill gaps label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the new layout for buttons self.last_row = row + 2 def show_all_fields(self): """Show all fields and reset layout.""" row = 0 col = 0 max_columns = 4 for widgets in self.entries.values(): label, offset_x_label, offset_x_entry, offset_y_label, offset_y_entry = widgets label.grid(row=row, column=col * 2, columnspan=2, sticky="w", padx=(5, 15)) offset_x_label.grid(row=row + 1, column=col * 2, sticky="e", padx=5) offset_x_entry.grid(row=row + 1, column=col * 2 + 1, sticky="w", padx=(0, 10)) offset_y_label.grid(row=row + 2, column=col * 2, sticky="e", padx=5) offset_y_entry.grid(row=row + 2, column=col * 2 + 1, sticky="w", padx=(0, 10)) col += 1 if col >= max_columns: col = 0 row += 3 # Update last row based on the reset layout for buttons self.last_row = row + 2 if __name__ == "__main__": root = tk.Tk() editor = OffsetConfigEditor(root) root.mainloop()Key Modifications:
-
toggle_offset_correctionMethod:- Added logic to rename
.batfiles based on the checkbox state. - Disabled mode switching buttons when offset correction is enabled (
run_offset_varisTrue). - Reverted the file names and enabled mode switching when offset correction is disabled.
- Added logic to rename
-
Renaming Logic:
- When enabling offset correction:
tecogan.batis renamed totecogan_offset.bat.tecogan_no_offset.batis renamed totecogan.bat.
- When disabling offset correction:
- Reverts the above renaming logic.
- When enabling offset correction:
-
Disabling/Enabling Mode Switching:
- Used
config(state=tk.DISABLED)andconfig(state=tk.NORMAL)to manage the state of the mode switching radio buttons.
- Used
With these changes, the script behaves as you specified: renaming files appropriately and managing the enable/disable state of the mode buttons depending on whether the offset correction is active.
-