From b7fc6f3ad9da01c88addedf100ebe0e1d0fac3c9 Mon Sep 17 00:00:00 2001 From: Tom Schimansky Date: Mon, 20 Dec 2021 11:55:07 +0100 Subject: [PATCH] added number_of_steps argument for CTkSlider --- Readme.md | 1 + Readme_pypi.md | 2 +- customtkinter/customtkinter_slider.py | 15 +++++++++++++-- examples/complex_example.py | 5 +++++ examples/complex_example_other_style.py | 1 + 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index 46bd047..83e8cb5 100644 --- a/Readme.md +++ b/Readme.md @@ -345,6 +345,7 @@ width | slider width in px height | slider height in px from_ | lower slider value to | upper slider value +number_of_steps | number of steps in which the slider can be positioned border_width | space around the slider rail in px fg_color | forground color, tuple: (light_color, dark_color) or single color progress_color | tuple: (light_color, dark_color) or single color, colors the slider line before the round button and is set to fg_color by default diff --git a/Readme_pypi.md b/Readme_pypi.md index 182dea3..25d8c6b 100644 --- a/Readme_pypi.md +++ b/Readme_pypi.md @@ -1,6 +1,6 @@ # CustomTkinter -![macOS darkmode](https://raw.githubusercontent.com/TomSchimansky/CustomTkinter/master/documentation_images/customtkinter_mode_switch.gif) +![](https://raw.githubusercontent.com/TomSchimansky/CustomTkinter/master/documentation_images/customtkinter_mode_switch.gif) With CustomTkinter you can create modern looking user interfaces in python with tkinter. CustomTkinter is a diff --git a/customtkinter/customtkinter_slider.py b/customtkinter/customtkinter_slider.py index 163eff3..8396b8f 100644 --- a/customtkinter/customtkinter_slider.py +++ b/customtkinter/customtkinter_slider.py @@ -18,6 +18,7 @@ class CTkSlider(tkinter.Frame): button_hover_color=CTkColorManager.MAIN_HOVER, from_=0, to=1, + number_of_steps=None, width=160, height=16, border_width=5, @@ -43,6 +44,7 @@ class CTkSlider(tkinter.Frame): self.hover_state = False self.from_ = from_ self.to = to + self.number_of_steps = number_of_steps self.output_value = self.from_ + (self.value * (self.to - self.from_)) self.configure(width=self.width, height=self.height) @@ -238,7 +240,8 @@ class CTkSlider(tkinter.Frame): if self.value < 0: self.value = 0 - self.output_value = self.from_ + (self.value * (self.to - self.from_)) + self.output_value = self.round_to_step_size(self.from_ + (self.value * (self.to - self.from_))) + self.value = (self.output_value - self.from_) / (self.to - self.from_) self.draw(no_color_updates=True) @@ -253,11 +256,19 @@ class CTkSlider(tkinter.Frame): self.hover_state = False self.canvas.itemconfig("button_parts", fill=CTkColorManager.single_color(self.button_color, self.appearance_mode)) + def round_to_step_size(self, value): + if self.number_of_steps is not None: + step_size = (self.to - self.from_) / self.number_of_steps + value = self.to - (round((self.to - value) / step_size) * step_size) + return value + else: + return value + def get(self): return self.output_value def set(self, output_value): - self.output_value = output_value + self.output_value = self.round_to_step_size(output_value) self.value = (self.output_value - self.from_) / (self.to - self.from_) self.draw(no_color_updates=True) diff --git a/examples/complex_example.py b/examples/complex_example.py index 5281391..c8afbdd 100644 --- a/examples/complex_example.py +++ b/examples/complex_example.py @@ -94,12 +94,17 @@ class App(tkinter.Tk): border_width=3) self.progressbar.place(relx=0.5, rely=0.85, anchor=tkinter.S) + # from tkintermapview import TkinterMapView + # self.map_widget = TkinterMapView(self.frame_info, width=380, height=200, corner_radius=10) + # self.map_widget.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER) + # ============ frame_right <- ============ self.slider_1 = customtkinter.CTkSlider(master=self.frame_right, width=160, height=16, border_width=5, + number_of_steps=3, command=self.progressbar.set) self.slider_1.place(x=20, rely=0.6, anchor=tkinter.W) self.slider_1.set(0.3) diff --git a/examples/complex_example_other_style.py b/examples/complex_example_other_style.py index 9f8117a..04354e3 100644 --- a/examples/complex_example_other_style.py +++ b/examples/complex_example_other_style.py @@ -119,6 +119,7 @@ class App(tkinter.Tk): button_hover_color=App.MAIN_HOVER, width=160, height=16, + number_of_steps=3, border_width=5, command=self.progressbar.set) self.slider_1.place(x=20, rely=0.6, anchor=tkinter.W)