Segmenting the Slider
Dann239 last edited by
Currently, I have a slider that I segmented into three pieces (as demonstrated below). Does anyone have an alternative method or style of achieving the same or similar result?
In the past I asked about converting a degree into a compass direction and loved all the different ideas. (Helps me learn quite a bit and is inspiring)
def slider(self, sender): myInt = sender.value/3 if myInt < .1: sender.value = 0 if myInt >= .1: sender.value = .5 if myInt >= .2: sender.value = 1
ccc last edited by
Were you looking for 3 partitions of equal size or did you want partitions of different sizes? I will assume equal in the solution below.
Your variable myInt is not an integer but is instead a float.
For all myInt values equal to or larger than .2, the code above sets sender.value to .5 and then resets it to 1. In the end, you get the correct result but it is just slightly inefficient. You should consider use of
elseto ensure that sender.value is only set once in the function for any input value.
To divide a contiguous range into three part, you should only need two compares instead of three if you judiciously use
def slider(self, sender): if sender.value < 1.0/3: # 0.33333... sender.value = 0 elif sender.value >= 2.0/3: # 0.66666... sender.value = 1 else: # 0.33333... <= sender.value < 0.66666... sender.value = .5
JonB last edited by
Here's a one liner, that divides the slider into N equal steps.
def slider(self,sender): from math import floor N=3 #number of steps sender.value=min(floor(sender.value*N)/(N-1),1.0)
(Edit, min added to ensure 1.0 stays in bounds)
Olaf last edited by
Or without math:
sender.value = 1 if sender.value >= 2./3 else .5 if sender.value >= 1./3 else 0