self.root = root
self.root.geometry("800x600") # Initial window size
self.root.title("IMG Viewer")
self.root.configure(bg=self.bgcolor) # Set window background color
self.root.attributes("-topmost", False) # Make the window stay on top
self.image_folder = ""
self.selected_folder = "none"
self.image_files = []
self.current_image_index = 0
self.history = []
self.history_index = -1
self.total_images = 0
self.togsess_counter = 0
self.togsess_mode = tk.BooleanVar(value=False)
self.session_active = False
self.session_index = 0
self.session_image_count = 0
self.main_frame = tk.Frame(self.root, bg=self.bgcolor)
self.main_frame.pack()
self.root.lift()
self.select_folder_button = tk.Button(self.main_frame, text="Select Folder", command=self.select_folder)
self.select_folder_button.configure(**self.button_red)
self.select_folder_button.pack(side=tk.LEFT, padx=5, pady=10)
ToolTip(self.select_folder_button, msg="Pick a folder", follow=True,
**self.tooltip_color)
self.set_timer_button = tk.Button(self.main_frame, text="Timer", command=self.set_timer_interval)
self.set_timer_button.configure(**self.button_style)
self.set_timer_button.pack(side=tk.LEFT, padx=5, pady=10)
ToolTip(self.set_timer_button, msg="Set timer interval", follow=True,
**self.tooltip_color)
self.start_button = tk.Button(self.main_frame, text="Start", command=self.start_pause_slideshow)
self.start_button.configure(**self.button_style_disabled)
self.start_button.pack(side=tk.LEFT, padx=5, pady=10)
ToolTip(self.start_button, msg="Start or pause timer", follow=True,
**self.tooltip_color)
#image canvas
self.canvas = tk.Canvas(self.root)
self.is_mirrored = False
self.is_greyscale = False
self.is_paused = True
self.rotated = 0
self.vol = True
self.beep1 = self.resource_path("beep1.wav")
self.beep2 = self.resource_path("beep2.wav")
self.ico = self.resource_path("icon.ico")
self.root.iconbitmap(self.ico)
self.grid_type="d"
self.timer_label = tk.Label(self.root, text="15", anchor="ne")
self.timer_label.configure(**self.timer_style)
self.timer_label.place(relx=1, anchor="ne", x=-0, y=0)
self.timer_interval = 15000 # Default time interval in milliseconds (15 seconds)
self.set_timer_interval = 15000 # Store the set timer interval
self.timer = None # Timer ID
self.window_size = (self.root.winfo_width(), self.root.winfo_height())
self.resize_timer = None
self.root.bind("<Right>", self.next_image)
self.root.bind("<Left>", self.previous_image)
self.root.bind("<space>", self.start_pause_slideshow)
self.root.bind("<Configure>", self.update_image_size)
def select_folder(self):
self.selected_folder = filedialog.askdirectory(initialdir=self.image_folder)
if not self.selected_folder or self.selected_folder == self.image_folder:
return
self.image_folder = self.selected_folder
if self.image_folder:
image_files = os.listdir(self.image_folder) # Get all files in the selected folder
self.image_files = [file for file in image_files if file.endswith(self.SUPPORTED_EXTENSIONS)]
self.history = []
self.history_index = -1
self.rotated = 0
self.togsess_counter = 0
self.total_images = len(self.image_files)
if self.image_files:
self.add_image_to_history()
self.current_image_index = 0
self.canvas.pack(fill=tk.BOTH, expand=True)
self.select_folder_button.pack(side=tk.LEFT, padx=5, pady=5)
self.root.title(f"IMGing in {self.image_folder} found {self.total_images} images")
self.canvas.config(bg=self.bgcolor, highlightthickness=0)
self.display_image()
self.start_button.config(**self.button_red)
self.openimgdir.configure(**self.button_style)
self.mirror_button.configure(**self.button_style)
self.greyscale_button.configure(**self.button_style)
self.rotate_button.configure(**self.button_style)
self.grid_button.configure(**self.button_style)
self.select_folder_button.configure(**self.button_style)
else:
messagebox.showinfo("No Image Files", "The selected folder does not contain any image files.")
self.image_folder = ""
self.root.title("IMG")
def add_image_to_history(self):
#code
def set_timer_interval(self):
dialog = CustomTimeDialog(self.root)
if dialog.result is not None:
self.session_active = dialog.togsess_mode.get() # Ensure you're accessing the toggled state from the dialog
if self.session_active:
# Session mode is active, dialog.result contains the list of session strings
self.process_session(dialog.result)
# Set the interval to the first session's time
self.timer_interval = self.current_session_list[0][1] * 1000
else:
# Session mode is inactive, dialog.result should be a numerical value
interval = dialog.result
if interval < 1: # Check if interval is less than 1 second
interval = 1
self.timer_interval = interval * 1000 # Convert to milliseconds
self.root.lift() # Bring the main window to the top
self.root.focus_force() # Give focus to the main window
minutes = self.timer_interval // 60000
seconds = (self.timer_interval % 60000) // 1000
if self.timer_interval <=59000:
self.timer_label.config(text=f"{seconds}")
else :
self.timer_label.config(text=f"{minutes:d}:{seconds:02d}")
def process_session(self, session_list):
self.current_session_list = []
for session_str in session_list:
try:
num_pics, time_str = session_str.split(' pics for ')
minutes, seconds = self.parse_time(time_str)
self.current_session_list.append((int(num_pics), minutes * 60 + seconds))
except ValueError as e:
# Handle the error appropriately, e.g., by logging or messaging the user
pass # Or whatever error handling you choose
def parse_time(self, time_str):
splits = time_str.split('m')
if len(splits) > 1:
minutes = int(splits[0])
seconds = int(splits[1].replace('s', ''))
else:
minutes = 0
seconds = int(time_str.replace('s', ''))
return minutes, seconds
def start_pause_slideshow(self, event=None):
if self.image_folder == "":
messagebox.showinfo("You haven't picked a folder", "Please select an image folder first.")
else:
if self.timer is None: # Check if timer is not running
self.display_image()
self.start_button.config(text="Pause", **self.button_style)
self.timer_label.configure(**self.timer_style)
self.is_paused = False # Set paused flag to False
self.start_timer()
else:
if self.is_paused: # Check if the slideshow is paused
self.start_button.config(text="Pause", **self.button_style)
self.timer_label.configure(**self.timer_style)
self.is_paused = False # Set paused flag to False
self.start_timer()
else:
self.pause_timer()
self.start_button.config(text="Start", **self.button_red)
self.timer_label.configure(bg="green")
self.is_paused = True # Set paused flag to True
def pause_timer(self):
if self.timer is not None:
self.root.after_cancel(self.timer)
self.timer = None
if self.session_active:
self.session_index = 0
self.session_image_count = self.current_session_list[0][0]
self.timer_interval = self.current_session_list[0][1] * 1000
def start_timer(self):
if self.image_folder != "" and not self.is_paused:
self.update_timer()
self.timer = self.root.after(1000, self.start_timer)
def update_timer(self):
# Calculate minutes and seconds left
minutes = self.timer_interval // 60000
seconds = (self.timer_interval % 60000) // 1000
if self.timer_interval <=59000:
self.timer_label.config(text=f"{seconds}")
else :
self.timer_label.config(text=f"{minutes:d}:{seconds:02d}")
if self.vol:
if self.timer_interval == 3000:
wave_obj = sa.WaveObject.from_wave_file(self.beep1)
play_obj = wave_obj.play()
play_obj.wait_done()
if self.timer_interval == 2000:
wave_obj = sa.WaveObject.from_wave_file(self.beep1)
play_obj = wave_obj.play()
play_obj.wait_done()
if self.timer_interval == 1000:
wave_obj = sa.WaveObject.from_wave_file(self.beep1)
play_obj = wave_obj.play()
play_obj.wait_done()
if self.timer_interval == 0:
wave_obj = sa.WaveObject.from_wave_file(self.beep2)
play_obj = wave_obj.play()
play_obj.wait_done()
self.timer_interval -= 1000
if self.timer_interval < 0:
self.timer_interval = self.set_timer_interval # Use the stored set timer interval
self.next_image()
def load_image_delayed(self):
# This method will be called after the 500ms delay if no keypress happens
if self.timer is not None:
self.root.after_cancel(self.timer) # Cancel the existing timer
self.timer = None # Clear the timer ID
# Load the current image from history without changing to a new random image
image_path = os.path.join(self.image_folder, self.history[self.history_index])
threading.Thread(target=self.load_image, args=(image_path,)).start()
if not self.is_paused:
self.timer = self.set_timer_interval
self.root.after(500, self.start_timer)
def next_image(self, event=None):
if self.image_folder != "":
if len(self.image_files) == len(self.history):
self.history_index = (self.history_index + 1) % len(self.image_files)
else:
self.add_image_to_history()
self.display_image()
# Reset the timer interval to the stored set timer interval
self.timer_interval = self.set_timer_interval
if self.session_active:
self.session_image_count -= 1
if self.session_image_count <= 0:
self.session_index = (self.session_index + 1) % len(self.current_session_list)
self.session_image_count, interval = self.current_session_list[self.session_index]
self.timer_interval = interval * 1000 # Setting the new interval from session
else:
self.timer_interval = self.set_timer_interval
else:
self.timer_interval = self.set_timer_interval
def previous_image(self, event=None):
if self.image_folder != "" and self.history_index > 0:
self.history_index -= 1
self.display_image()
# When session is active, control the flow based on session settings
if self.session_active:
current_count, _ = self.current_session_list[self.session_index]
# Move to the previous session step, if it's the first image in the current session step
if self.session_image_count == current_count:
if self.session_index > 0:
self.session_index -= 1
else:
self.session_index = len(self.current_session_list) - 1
_, interval = self.current_session_list[self.session_index]
self.session_image_count = self.current_session_list[self.session_index][0]
self.timer_interval = interval * 1000
else:
self.session_image_count += 1
_, interval = self.current_session_list[self.session_index]
self.timer_interval = interval * 1000
else:
# For regular (non-session) mode, simply reset the timer interval
self.timer_interval = self.set_timer_interval
i want when i activate session mode and set the timer to use the session, and i open the timer again, i want it to save or acknowledge the previous setting in the session list and session checkbox instead of resetting the timer on opening the timer window.
when there's something to be changed, give the new codeblock and show me precisely where i can put the codeblock. if something needs to be added, tell me concisely.
To remember the session settings across multiple openings of the timer dialog, you'll need to save the state of the current session settings and pass them back to the CustomTimeDialog whenever it is opened.
Here's what you need to do:
In your ImageViewer class, you need to have variables to store the current session settings. Something like this should be added to the __init__ of ImageViewer:
Next, modify the set_timer_interval method in ImageViewer to store the current settings when the dialog is confirmed:
python
def set_timer_interval(self):
if not hasattr(self, 'current_session_timings'): # Make sure the properties exist
self.current_session_timings = []
self.is_session_mode = False
# Pass the current session settings to the dialog
dialog = CustomTimeDialog(self.root, self.current_session_timings, self.is_session_mode)
if dialog.result is not None:
self.is_session_mode = dialog.togsess_mode.get() # Store the new session mode
if self.is_session_mode:
self.current_session_timings = dialog.result # Store the session settings
# ... Rest of the if block ...
# ... Rest of the method ...
Modify the CustomTimeDialog class to accept the current session settings as arguments and to set the widget values:
python
class CustomTimeDialog(simpledialog.Dialog):
def __init__(self, parent, current_session_timings=None, is_session_mode=False, **kwargs):
self.current_session_timings = current_session_timings if current_session_timings else []
self.is_session_mode = is_session_mode
super().__init__(parent, **kwargs)
# Inside the body method:
def body(self, master):
# ... Existing code ...
self.togsess_mode.set(self.is_session_mode)
if self.is_session_mode:
self.toggle_togsess() # Initialize the state of session elements based on current setting
# Update the Listbox content with the current session timings:
self.sessionString.set(self.current_session_timings)
self.sessionlist = list(self.current_session_timings)
return self.spin_seconds # initial focus
# ... Rest of the CustomTimeDialog class ...
Where to put the code blocks:
The modifications in steps 1 and 2 should be added to the ImageViewer class.
The modifications in step 3 should be added to the CustomTimeDialog class - update the constructor to accept the new parameters for session timings and mode, and update the body method to use these parameters.
These steps should make your ImageViewer remember the selected session mode and timings between the openings of CustomTimeDialog. Remember, you may need to further test and tweak the code to make sure it integrates well with your existing logic.