chnaged complex_example.py and fixed command-variable execution order in CTkSlider

This commit is contained in:
Tom Schimansky 2022-06-15 02:07:51 +02:00
parent d8b5104028
commit fc952294f0
3 changed files with 82 additions and 63 deletions

View File

@ -30,6 +30,7 @@ class CTkSlider(CTkBaseClass):
command=None, command=None,
variable=None, variable=None,
orient="horizontal", orient="horizontal",
state="normal",
**kwargs): **kwargs):
# set default dimensions according to orientation # set default dimensions according to orientation
@ -75,6 +76,7 @@ class CTkSlider(CTkBaseClass):
self.variable: tkinter.Variable = variable self.variable: tkinter.Variable = variable
self.variable_callback_blocked = False self.variable_callback_blocked = False
self.variable_callback_name = None self.variable_callback_name = None
self.state = state
self.grid_rowconfigure(0, weight=1) self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1) self.grid_columnconfigure(0, weight=1)
@ -124,12 +126,18 @@ class CTkSlider(CTkBaseClass):
super().destroy() super().destroy()
def set_cursor(self): def set_cursor(self):
if Settings.cursor_manipulation_enabled: if self.state == "normal" and Settings.cursor_manipulation_enabled:
if sys.platform == "darwin": if sys.platform == "darwin":
self.configure(cursor="pointinghand") self.configure(cursor="pointinghand")
elif sys.platform.startswith("win"): elif sys.platform.startswith("win"):
self.configure(cursor="hand2") self.configure(cursor="hand2")
elif self.state == "disabled" and Settings.cursor_manipulation_enabled:
if sys.platform == "darwin":
self.configure(cursor="arrow")
elif sys.platform.startswith("win"):
self.configure(cursor="arrow")
def draw(self, no_color_updates=False): def draw(self, no_color_updates=False):
if self.orient.lower() == "horizontal": if self.orient.lower() == "horizontal":
orientation = "w" orientation = "w"
@ -166,10 +174,17 @@ class CTkSlider(CTkBaseClass):
self.canvas.itemconfig("progress_parts", fill=ThemeManager.single_color(self.progress_color, self._appearance_mode), self.canvas.itemconfig("progress_parts", fill=ThemeManager.single_color(self.progress_color, self._appearance_mode),
outline=ThemeManager.single_color(self.progress_color, self._appearance_mode)) outline=ThemeManager.single_color(self.progress_color, self._appearance_mode))
self.canvas.itemconfig("slider_parts", fill=ThemeManager.single_color(self.button_color, self._appearance_mode), if self.hover_state is True:
self.canvas.itemconfig("slider_parts",
fill=ThemeManager.single_color(self.button_hover_color, self._appearance_mode),
outline=ThemeManager.single_color(self.button_hover_color, self._appearance_mode))
else:
self.canvas.itemconfig("slider_parts",
fill=ThemeManager.single_color(self.button_color, self._appearance_mode),
outline=ThemeManager.single_color(self.button_color, self._appearance_mode)) outline=ThemeManager.single_color(self.button_color, self._appearance_mode))
def clicked(self, event=None): def clicked(self, event=None):
if self.state == "normal":
if self.orient.lower() == "horizontal": if self.orient.lower() == "horizontal":
self.value = (event.x / self._current_width) / self._widget_scaling self.value = (event.x / self._current_width) / self._widget_scaling
else: else:
@ -185,22 +200,25 @@ class CTkSlider(CTkBaseClass):
self.draw(no_color_updates=False) self.draw(no_color_updates=False)
if self.callback_function is not None:
self.callback_function(self.output_value)
if self.variable is not None: if self.variable is not None:
self.variable_callback_blocked = True self.variable_callback_blocked = True
self.variable.set(round(self.output_value) if isinstance(self.variable, tkinter.IntVar) else self.output_value) self.variable.set(round(self.output_value) if isinstance(self.variable, tkinter.IntVar) else self.output_value)
self.variable_callback_blocked = False self.variable_callback_blocked = False
if self.callback_function is not None:
self.callback_function(self.output_value)
def on_enter(self, event=0): def on_enter(self, event=0):
if self.state == "normal":
self.hover_state = True self.hover_state = True
self.canvas.itemconfig("slider_parts", fill=ThemeManager.single_color(self.button_hover_color, self._appearance_mode), self.canvas.itemconfig("slider_parts",
fill=ThemeManager.single_color(self.button_hover_color, self._appearance_mode),
outline=ThemeManager.single_color(self.button_hover_color, self._appearance_mode)) outline=ThemeManager.single_color(self.button_hover_color, self._appearance_mode))
def on_leave(self, event=0): def on_leave(self, event=0):
self.hover_state = False self.hover_state = False
self.canvas.itemconfig("slider_parts", fill=ThemeManager.single_color(self.button_color, self._appearance_mode), self.canvas.itemconfig("slider_parts",
fill=ThemeManager.single_color(self.button_color, self._appearance_mode),
outline=ThemeManager.single_color(self.button_color, self._appearance_mode)) outline=ThemeManager.single_color(self.button_color, self._appearance_mode))
def round_to_step_size(self, value): def round_to_step_size(self, value):
@ -231,8 +249,8 @@ class CTkSlider(CTkBaseClass):
self.draw(no_color_updates=False) self.draw(no_color_updates=False)
if self.callback_function is not None: # if self.callback_function is not None and not from_variable_callback:
self.callback_function(self.output_value) # self.callback_function(self.output_value)
if self.variable is not None and not from_variable_callback: if self.variable is not None and not from_variable_callback:
self.variable_callback_blocked = True self.variable_callback_blocked = True
@ -246,6 +264,12 @@ class CTkSlider(CTkBaseClass):
def configure(self, *args, **kwargs): def configure(self, *args, **kwargs):
require_redraw = False # some attribute changes require a call of self.draw() at the end require_redraw = False # some attribute changes require a call of self.draw() at the end
if "state" in kwargs:
self.state = kwargs["state"]
self.set_cursor()
require_redraw = True
del kwargs["state"]
if "fg_color" in kwargs: if "fg_color" in kwargs:
self.fg_color = kwargs["fg_color"] self.fg_color = kwargs["fg_color"]
require_redraw = True require_redraw = True

View File

@ -46,30 +46,27 @@ class App(customtkinter.CTk):
self.label_1.grid(row=1, column=0, pady=10, padx=10) self.label_1.grid(row=1, column=0, pady=10, padx=10)
self.button_1 = customtkinter.CTkButton(master=self.frame_left, self.button_1 = customtkinter.CTkButton(master=self.frame_left,
text="CTkButton 1", text="CTkButton",
fg_color=("gray75", "gray30"), # <- custom tuple-color
command=self.button_event) command=self.button_event)
self.button_1.grid(row=2, column=0, pady=10, padx=20) self.button_1.grid(row=2, column=0, pady=10, padx=20)
self.button_2 = customtkinter.CTkButton(master=self.frame_left, self.button_2 = customtkinter.CTkButton(master=self.frame_left,
text="CTkButton 2", text="CTkButton",
fg_color=("gray75", "gray30"), # <- custom tuple-color
command=self.button_event) command=self.button_event)
self.button_2.grid(row=3, column=0, pady=10, padx=20) self.button_2.grid(row=3, column=0, pady=10, padx=20)
self.button_3 = customtkinter.CTkButton(master=self.frame_left, self.button_3 = customtkinter.CTkButton(master=self.frame_left,
text="CTkButton 3", text="CTkButton",
fg_color=("gray75", "gray30"), # <- custom tuple-color
command=self.button_event) command=self.button_event)
self.button_3.grid(row=4, column=0, pady=10, padx=20) self.button_3.grid(row=4, column=0, pady=10, padx=20)
self.switch_1 = customtkinter.CTkSwitch(master=self.frame_left) self.label_mode = customtkinter.CTkLabel(master=self.frame_left, text="Appearance Mode:")
self.switch_1.grid(row=9, column=0, pady=10, padx=20, sticky="w") self.label_mode.grid(row=9, column=0, pady=0, padx=20, sticky="w")
self.switch_2 = customtkinter.CTkSwitch(master=self.frame_left, self.optionmenu_1 = customtkinter.CTkOptionMenu(master=self.frame_left,
text="Dark Mode", values=["Light", "Dark", "System"],
command=self.change_mode) command=self.change_appearance_mode)
self.switch_2.grid(row=10, column=0, pady=10, padx=20, sticky="w") self.optionmenu_1.grid(row=10, column=0, pady=10, padx=20, sticky="w")
# ============ frame_right ============ # ============ frame_right ============
@ -134,25 +131,17 @@ class App(customtkinter.CTk):
command=self.progressbar.set) command=self.progressbar.set)
self.slider_2.grid(row=5, column=0, columnspan=2, pady=10, padx=20, sticky="we") self.slider_2.grid(row=5, column=0, columnspan=2, pady=10, padx=20, sticky="we")
self.slider_button_1 = customtkinter.CTkButton(master=self.frame_right, self.switch_1 = customtkinter.CTkSwitch(master=self.frame_right,
height=25, text="CTkSwitch")
text="CTkButton", self.switch_1.grid(row=4, column=2, columnspan=1, pady=10, padx=20, sticky="we")
command=self.button_event)
self.slider_button_1.grid(row=4, column=2, columnspan=1, pady=10, padx=20, sticky="we")
self.slider_button_2 = customtkinter.CTkButton(master=self.frame_right, self.switch_2 = customtkinter.CTkSwitch(master=self.frame_right,
height=25, text="CTkSwitch")
text="CTkButton", self.switch_2.grid(row=5, column=2, columnspan=1, pady=10, padx=20, sticky="we")
command=self.button_event)
self.slider_button_2.grid(row=5, column=2, columnspan=1, pady=10, padx=20, sticky="we")
self.checkbox_button_1 = customtkinter.CTkButton(master=self.frame_right, self.combobox_1 = customtkinter.CTkComboBox(master=self.frame_right,
height=25, values=["Value 1", "Value 2"])
text="CTkButton", self.combobox_1.grid(row=6, column=2, columnspan=1, pady=10, padx=20, sticky="we")
border_width=3, # <- custom border_width
fg_color=None, # <- no fg_color
command=self.button_event)
self.checkbox_button_1.grid(row=6, column=2, columnspan=1, pady=10, padx=20, sticky="we")
self.check_box_1 = customtkinter.CTkCheckBox(master=self.frame_right, self.check_box_1 = customtkinter.CTkCheckBox(master=self.frame_right,
text="CTkCheckBox") text="CTkCheckBox")
@ -169,16 +158,20 @@ class App(customtkinter.CTk):
self.button_5 = customtkinter.CTkButton(master=self.frame_right, self.button_5 = customtkinter.CTkButton(master=self.frame_right,
text="CTkButton", text="CTkButton",
border_width=3, # <- custom border_width
fg_color=None, # <- no fg_color
command=self.button_event) command=self.button_event)
self.button_5.grid(row=8, column=2, columnspan=1, pady=20, padx=20, sticky="we") self.button_5.grid(row=8, column=2, columnspan=1, pady=20, padx=20, sticky="we")
# set default values # set default values
self.optionmenu_1.set("Dark")
self.button_3.configure(state="disabled", text="Disabled CTkButton")
self.combobox_1.set("CTkCombobox")
self.radio_button_1.select() self.radio_button_1.select()
self.switch_2.select()
self.slider_1.set(0.2) self.slider_1.set(0.2)
self.slider_2.set(0.7) self.slider_2.set(0.7)
self.progressbar.set(0.5) self.progressbar.set(0.5)
self.slider_button_1.configure(state=tkinter.DISABLED, text="Disabled Button") self.switch_2.select()
self.radio_button_3.configure(state=tkinter.DISABLED) self.radio_button_3.configure(state=tkinter.DISABLED)
self.check_box_1.configure(state=tkinter.DISABLED, text="CheckBox disabled") self.check_box_1.configure(state=tkinter.DISABLED, text="CheckBox disabled")
self.check_box_2.select() self.check_box_2.select()
@ -186,11 +179,8 @@ class App(customtkinter.CTk):
def button_event(self): def button_event(self):
print("Button pressed") print("Button pressed")
def change_mode(self): def change_appearance_mode(self, new_appearance_mode):
if self.switch_2.get() == 1: customtkinter.set_appearance_mode(new_appearance_mode)
customtkinter.set_appearance_mode("dark")
else:
customtkinter.set_appearance_mode("light")
def on_closing(self, event=0): def on_closing(self, event=0):
self.destroy() self.destroy()

View File

@ -3,7 +3,7 @@ import customtkinter
app = customtkinter.CTk() app = customtkinter.CTk()
app.geometry("400x800") app.geometry("400x900")
app.title("CustomTkinter Test") app.title("CustomTkinter Test")
@ -53,5 +53,10 @@ combobox_1.pack(pady=10, padx=10)
button_7 = customtkinter.CTkButton(master=app, text="Disable/Enable combobox_1", command=lambda: change_state(combobox_1)) button_7 = customtkinter.CTkButton(master=app, text="Disable/Enable combobox_1", command=lambda: change_state(combobox_1))
button_7.pack(padx=20, pady=(10, 20)) button_7.pack(padx=20, pady=(10, 20))
slider_1 = customtkinter.CTkSlider(app)
slider_1.pack(pady=10, padx=10)
button_8 = customtkinter.CTkButton(master=app, text="Disable/Enable slider_1", command=lambda: change_state(slider_1))
button_8.pack(padx=20, pady=(10, 20))
app.mainloop() app.mainloop()