[share code] SliderWithLabel class for ui.Slider() featuring editable label
Added ability to set frame as a keyword argument. Thanks @Webmaster4o
@filippocld thanks... Also changed that bit.
At first I had just a UI.Label and had everything changeable.. But after switching to the textfield, I couldn't change much so I had taken out all of the kwargs - border color, background color etc.
It's a bit unfortunate that you can't change the background color (easily). It seems it can be done by changing the image of the background with objc. It's almost just too much hassle to both... Especially if we want different colors. Would have to make an image, then do some objc magic. I don't get paid enough by @Webmaster4o to attempt that sort of stuff.
Now also has a delegate
To make this as generic as possible, You might consider, rather than just a max_value, to have a
limitstuple, that lets you specify the min and max values.
value=limits + slider.value*(limits-limits)
Another idea: perhaps, a user specified number to string translation function, or format string. Basically let the user specify the conversion from a string repr to the value, and vice versa. That would enable hexidecimal entry, floats, rounded ints, rounded to the nearest tenth, hundredth, etc...
@jonb thanks- will try to add in a min value. Makes sense.
Yes the different rounded would be good too, I realize that some would use sliders for crazy numbers.
Also- I think that it may be necessary to make a way for the delegate to distinguish values from different sliders (if they are using the same delegate, for example). I don't know if that's a good way or just creating different delegates per slider. Could easily add a name for the delegate (passing a tuple:
What's the best approach?
Let's say I have a class with three sliders and set the delegate to
selffor each one and provide the method... If I can distinguish
name(or something) then it's probably easier than creating 3 separate objects just for delegate use.
ccc last edited by
self.label.text_color = kwargs.get('text_color', 0.7)
@cook , thanks for sharing again. I really like the effect. It strikes me that you should maybe you should have a name associated with the slider and also pass that back via the delegate. As it would be normal to use one delegate for a number of sliders as you demonstrate in your example.
I am not trying to be critical, but it does look a little messy. I hope you don't mind me saying so. I say it with the best of intentions. Below is a skeleton I normally follow. Not sure it's great, probably better ways to do it.
But it does make it a bit more clearer to what's happening by splitting it up,a little. You also seem to be use a lot of literal values.
Anyway, I really like what you have done.
import ui class MyClass(ui.View): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # def instance vars self.slider = None #etc... self.make_view(**kwargs) def make_view(self, **kwargs): slider = ui.Slider() slider.bg_color = 'green' #etc self.add_subview(slider) self.slider = slider def layout(self): r = ui.Rect(*self.bounds.inset(5, 5)) self.slider.width = r.width self.slider.x = r.x self.slider.y = r.y def draw(self): # only should overide draw if you acually need to use it # here only illustration purposes. pass if __name__ == '__main__': v = MyClass(frame = (0, 0, 600, 800), bg_color = 'white') v.present('sheet')
filippocld last edited by
This is a nice quick way to pull out kwargs that are not part of the ui.View for example. I have seen this done before. I think @Webmaster4o , did something like this before. But you can imagine versions of it. The nice thing is if you pull out kwargs of interest, then you can apply the attrs in a loop rating than having to reference them directly.
Of course does not need to be in its own function.
# returns any kwargs that are not attrs in ui.View def get_non_uiview_kwargs(self, **kwargs): return set(kwargs) - set(ui.View.__dict__)
grnerally with ui delegates or actions, the first argument is the ui component, so that you can have the same callback serve multiple components. To be consistent with the way ui works, you should just pass the slider object itself, (a.k.a self)
For a slider, you should simply have one argument: slider. The user can then query the value.
if you had an action which gets called before the value is i
set ( such as slider_should_change), in that case you'd also send the value, and expect a return)
Webmaster4o last edited by
@jonb thank you. I've made that change so the delegate receives
self. Also saw your note re: setting an action rather than a delegate. I think this is a much better way than a delegate (as you've said). Anyway.. How is this done? Do you have a simplified example so I could see what it's like? I know that you can code it in on this script no problem...- for me it's a learning opportunity as well. So- seeing a simplified example with some explanation can help. Some of these things go over my head simply because I have never learned about them yet.
Thanks for your suggestions and help!
an action would be similar to how you use the delegate, except you are checking for if self.action and callable(self.action). just set self.action=None in the constructor. you could even support both delegates and actions, which is how several ui components work.
@jonb thank you- will try later. It's really late for me now so I will just sleep!
Thanks @jonb. I've actually removed the delegate bit in favour of setting an action.
Also updated with a bit more functionality: ability to set style (and other) attributes of the label.
Next up: min/max value setting and different style of numbers.
@cook , I am not sure I agree with @jonb totally. However, he has whipped me many times. Everytime I contradict him, I am sure I will be wrong.
But regardless.what I would say, some times actions get buried in layers of code. Sometime it's hard to make sense where you should define all this. On the other hand a delegate object can be very flexible in its implementation/design usage. Sorry, maybe some wrong language here. But a delegate object is very flexible in my mind vrs a action. Maybe the word I am looking for is scope.
As usual, I am not sure. It could be a lack of ability that makes actions less attractive/usable to me. Sure a button click only needs a action. But even on bigger projects, not just some test code, this can get difficult.
Anyway, my 2 cents worth of info. I don't say it to try to be smarter, I am just not smarter than these other guys. I still have to say what I think
@phuket2 I think just having an action is more in-line with how the ui.Slider() works so it seems more appropriate to me. I don't imagine there's much more that one would want to do and effectively this action does what the delegate was doing.
(Also... A little less code to implement an action!!)
@cook , I will capitulate. But, I still believe in what I say. But it will not be proven with some test code. You need to implement it in a multi view app before you will start to see the limitations or restrictions. Sample code is just so different. You can make everything work in sample code somehow. But as you start to build a app, things change. Scope/reachablilty starts becoming big issues. I mean to not have to cludge connections between objects. Again this goes back to MCV (MODEL, CONTROLLER,VIEW) or close to that.
But I will stand corrected if need be
I say, try to match the interface if the thing you are replicating. ui.Slider has
actionso an action will be what someone expects.
There is also nothing wrong with using both a delegate and an action -- such as
ui.TextView, which have an action which is called for the most common delegate item, along with delegate items. Both can be set.
IMHO a delegate makes sense when there are more than one type of callback, or when a callback needs other info instead of just
sender. But for single action delegates, an action is easier to work with, since it requires less code, and is more easily discoverable (how many times have you had to pull up the docs to remember whether it is