added number_of_steps argument for CTkSlider

This commit is contained in:
Tom Schimansky 2021-12-20 11:55:07 +01:00
parent b3d32a419f
commit b7fc6f3ad9
5 changed files with 21 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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)