class CustomTimeDialog(simpledialog.Dialog): def body(self, master):…
- User
class CustomTimeDialog(simpledialog.Dialog): def body(self, master): self.timebutton_style = {"font": ("consolas", 11), "fg": "white", "bg": "#3c3c3c", "relief": "flat"} self.title('Custom Time') self.time_background = "#242424" self.time_fg="white"
master.configure(bg=self.time_background) self.configure(bg=self.time_background) self.ico = self.resource_path_timer("icon.ico") self.iconbitmap(self.ico) #timebox-------------------------------------------------- ctime_frame = tk.Frame(master, bg=self.time_background) ctime_frame.grid(row=1, column=1) self.timebox = tk.Listbox(ctime_frame, height=6, width=15, font=self.timebutton_style["font"], bg=self.timebutton_style["bg"], fg="#dddddd") for time_string in ['15s', '30s', '45s', '1m', '5m', '10m']: self.timebox.insert(tk.END, time_string) self.timebox.grid(row=1, column=1) self.timebox.bind('<<ListboxSelect>>', self.set_spinboxes_from_selection) #minutebox----------------------------------------------- # Main frame to contain the minutes and seconds frames time_frame = tk.Frame(ctime_frame, bg=self.time_background) time_frame.grid(row=2, column=1, pady=5)# side=tk.TOP, fill=tk.X, padx=5, pady=5) # Create a frame for the minutes spinbox and label minutes_frame = tk.Frame(time_frame, bg=self.time_background) minutes_frame.pack(side=tk.LEFT, fill=tk.X, padx=5) tk.Label(minutes_frame, text="Minutes:", bg=self.time_background, fg=self.time_fg, font=self.timebutton_style["font"]).pack(side=tk.TOP) self.spin_minutes = tk.Spinbox(minutes_frame, from_=0, to=59, width=5, font=self.timebutton_style["font"]) self.spin_minutes.pack(side=tk.TOP) # Create a frame for the seconds spinbox and label seconds_frame = tk.Frame(time_frame, bg=self.time_background) seconds_frame.pack(side=tk.LEFT, fill=tk.X, padx=5) tk.Label(seconds_frame, text="Seconds:", bg=self.time_background, fg=self.time_fg, font=self.timebutton_style["font"]).pack(side=tk.TOP) self.spin_seconds = tk.Spinbox(seconds_frame, from_=0, to=59, width=5, font=self.timebutton_style["font"], wrap=True) self.spin_seconds.pack(side=tk.TOP) #togsessbox------------------------------------------------- session = tk.LabelFrame(master, text="Sessions", fg=self.timebutton_style["fg"], bg=self.timebutton_style["bg"], relief=tk.RIDGE) session.grid(row=1, column=2) #togsessbox = tk.Frame(master, bg=self.time_background) togsess_frame = tk.Frame(session, bg=self.time_background) togsess_frame.grid (row=1,column=1, pady=5) self.togsess_mode = tk.BooleanVar(value=False) #self.togsess_frame = tk.Checkbutton(togsess_frame, bg=self.time_background) self.togsess_checkbox = tk.Checkbutton(togsess_frame, text="Session", variable=self.togsess_mode, command=self.toggle_togsess) self.togsess_checkbox.configure(**self.timebutton_style, bd=0) self.togsess_checkbox.grid(row=1, column=1, sticky=tk.E, ipadx=12) self.img_togsess = tk.Spinbox(togsess_frame, width=3, from_=0, to=100, font=self.timebutton_style["font"], wrap=True, x=500) self.img_togsess.grid(row=1, column=2) #sessionsbox----------------------------------------------------------- self.sessionlist = ['5 images for 30s', '5 images for 1m', '5 images for 5m'] self.sessionString = tk.StringVar(value=self.sessionlist) self.sessionbox = tk.Listbox(session, height=6, width=18, font=self.timebutton_style["font"], bg=self.timebutton_style["bg"], fg=self.timebutton_style["fg"], listvariable=self.sessionString, borderwidth=0) self.sessionbox.grid(row=2, column=1) #session buttons frame------------------------------------------------------------------ sessionbuttons = tk.Frame(session, bg=self.time_background) sessionbuttons.grid(row=3, column=1) self.add_button = tk.Button(sessionbuttons, text="+", width=3, command=self.ok, default=tk.ACTIVE) self.add_button.config(**self.timebutton_style) self.add_button.grid(row=1, column=1) self.del_button = tk.Button(sessionbuttons, text="-", width=3, command=self.ok, default=tk.ACTIVE) self.del_button.config(**self.timebutton_style) self.del_button.grid(row=1, column=2) self.up_button = tk.Button(sessionbuttons, text="Ʌ", width=2, command=lambda: self.move(-1), default=tk.ACTIVE) self.up_button.config(**self.timebutton_style) self.up_button.grid(row=1, column=3) self.down_button = tk.Button(sessionbuttons, text="V", command=lambda: self.move(1), default=tk.ACTIVE) self.down_button.config(**self.timebutton_style) self.down_button.grid(row=1, column=4) return self.spin_seconds # initial focus def buttonbox(self): box = tk.Frame(self, bg=self.time_background) self.ok_button = tk.Button(box, text="Apply", width=16, command=self.ok, default=tk.ACTIVE) self.ok_button.config(**self.timebutton_style) self.ok_button.pack(side=tk.TOP, ipadx=5, pady=5) box.pack() def move(self, num): #num is -1 for up and 1 for down self.idxs = self.sessionbox.curselection() if not self.idxs: return for pos in self.idxs: text=self.sessionbox.get(pos) #gets text self.sessionbox.delete(pos) #removes text from list box self.sessionlist.pop(pos) #removes text from list if pos==len(self.sessionlist) and num==1: pos=-1; #sets at beginning if at bottom and going down if pos==0 and num==-1: pos=len(self.sessionlist)+1; #sets at end if at top and going up self.sessionbox.insert(pos+num, text) #inserts into list box self.sessionlist.insert(pos+num, text) #inserts into list self.sessionbox.selection_set(pos+num) def toggle_togsess(self): if self.togsess_mode.get(): self.togsess_checkbox.configure(fg="#2c2c2c", bg="#b8b8b8") else: self.togsess_checkbox.configure(**self.timebutton_style) def set_spinboxes_from_selection(self, event=None): index = self.timebox.curselection() if not index: return time_string = self.timebox.get(index) if 'm' in time_string: minutes = int(time_string.replace('m', '')) seconds = 0 elif 's' in time_string: minutes = 0 seconds = int(time_string.replace('s', '')) else: # Handle a possible unknown format self.bell() # ring the bell to signal an error return self.spin_minutes.delete(0, tk.END) self.spin_minutes.insert(0, minutes) self.spin_seconds.delete(0, tk.END) self.spin_seconds.insert(0, seconds) def apply(self): minutes = int(self.spin_minutes.get()) seconds = int(self.spin_seconds.get()) self.result = minutes * 60 + seconds def validate(self): try: minutes = int(self.spin_minutes.get()) seconds = int(self.spin_seconds.get()) return True except ValueError: self.bell() return Falsei want the plus and minus button add and remove a value to the session box with the format like the examples. the number of image will be taken from the img_togsess spinbox while the time will be from the spin_minutes and spin_seconds spinboxes.
- Assistant
To add and remove values from the session box based on the input from the img_togsess spinbox, spin_minutes, and spin_seconds spinboxes, you can modify the
add_buttonanddel_buttoncommands as follows:def add_session(self): img_togsess_value = int(self.img_togsess.get()) minutes = int(self.spin_minutes.get()) seconds = int(self.spin_seconds.get()) session_str = f"{img_togsess_value} images for {minutes}m {seconds}s" self.sessionlist.append(session_str) self.sessionString.set(self.sessionlist) self.sessionbox.selection_set(len(self.sessionlist) - 1) def remove_session(self): selected_index = self.sessionbox.curselection() if not selected_index: return selected_index = selected_index[0] self.sessionlist.pop(selected_index) self.sessionString.set(self.sessionlist) if selected_index < len(self.sessionlist): self.sessionbox.selection_set(selected_index) elif len(self.sessionlist) > 0: self.sessionbox.selection_set(len(self.sessionlist) - 1)You also need to update the
buttonboxmethod to use the new methods for the add and remove buttons:def buttonbox(self): box = tk.Frame(self, bg=self.time_background) self.add_button = tk.Button(box, text="+", width=3, command=self.add_session, default=tk.ACTIVE) self.add_button.config(**self.timebutton_style) self.add_button.grid(row=1, column=1) self.del_button = tk.Button(box, text="-", width=3, command=self.remove_session, default=tk.ACTIVE) self.del_button.config(**self.timebutton_style) self.del_button.grid(row=1, column=2) box.pack()Note that the
add_sessionmethod gets the values from theimg_togsess,spin_minutes, andspin_secondsspinboxes and creates a session string in the desired format. It then appends the session string to thesessionlistand updates thesessionStringvariable to reflect the changes.Similarly, the
remove_sessionmethod removes the selected session from thesessionlistand updates thesessionString, and it also handles the selection of the next session after removal.Make sure to update the
bodymethod to bind the new methods to the add and remove buttons:self.add_button = tk.Button(sessionbuttons, text="+", width=3, command=self.add_session, default=tk.ACTIVE) self.add_button.config(**self.timebutton_style) self.add_button.grid(row=1, column=1) self.del_button = tk.Button(sessionbuttons, text="-", width=3, command=self.remove_session, default=tk.ACTIVE) self.del_button.config(**self.timebutton_style) self.del_button.grid(row=1, column=2)With these changes, the plus and minus buttons will add and remove session values from the session box in the desired format, taking the image count from the
img_togsessspinbox and the time from thespin_minutesandspin_secondsspinboxes. - User
i want it so that when it's 0m and (seconds), the sessionbox hides the 0m and only show the seconds s
- Assistant
To hide the "0m" when the minutes value is 0, you can modify the
add_sessionmethod as follows:def add_session(self): img_togsess_value = int(self.img_togsess.get()) minutes = int(self.spin_minutes.get()) seconds = int(self.spin_seconds.get()) if minutes == 0: session_str = f"{img_togsess_value} images for {seconds}s" else: session_str = f"{img_togsess_value} images for {minutes}m {seconds}s" self.sessionlist.append(session_str) self.sessionString.set(self.sessionlist) self.sessionbox.selection_set(len(self.sessionlist) - 1)This modification checks if the
minutesvalue is 0. If it is, the session string is formatted without the "0m".By making this change, when the
minutesvalue is 0, the session box will only show the seconds value in the format "X images for Ys", while for other values ofminutes, it will show the minutes and seconds in the format "X images for Ym Zs". - Assistant
Here is the modified code that hides the "0m" when the minutes value is 0:
def add_session(self): img_togsess_value = int(self.img_togsess.get()) minutes = int(self.spin_minutes.get()) seconds = int(self.spin_seconds.get()) if minutes == 0: session_str = f"{img_togsess_value} images for {seconds}s" else: session_str = f"{img_togsess_value} images for {minutes}m {seconds}s" self.sessionlist.append(session_str) self.sessionString.set(self.sessionlist) self.sessionbox.selection_set(len(self.sessionlist) - 1)I've modified the
add_sessionmethod to check if theminutesvalue is 0. If it is, the session string is formatted without the "0m". Otherwise, it includes the minutes and seconds in the format "X images for Ym Zs".With this change, when the minutes value is 0, the session box will only display the seconds value in the format "X images for Ys". For non-zero minutes values, it will display both minutes and seconds in the format "X images for Ym Zs".