Ui.textfield drives slider
Is it possible to make a numeric entry field drive a slider position?
I have made a converter that converts feet to metres and also milliseconds (speed of sound) to calculate PA system delay times between speaker arrays. It currently works but I would love to add numeric entry to the slider.
Also at this time you move the slider and can activate feet/metres/milliseconds by turning on switches. How would I make it so the label that displays the output will update with a flock of a switch. (Ie change metres to feet) at present it doesn't update until you put your finger back on the slider
@Vertec, it's a little hard to know exactly what you are asking here. But I think your meaning is can these connections between the ui objects be done in the ui designer to update each other. Basically the answer is no if that was what you are asking.
You can having created your view in the designer write some code that will update the other objects. I know what I am saying is a little vague. But it's not a automatic connection. You can connect it in code. But there are many ways to do it. I did a very lazy one way example below. It's possibly very confusing. Not sure. But after all said and done, it's just worth reading about the ui module in the help.
Hope this gives you a idea anyway
import ui def slider_action(sender): # get the sliders superview a.k.a view v = sender.superview v['mylabel'].text = str(sender.value) if __name__ == '__main__': f = (0, 0, 300, 400) v = ui.View(frame = f, bg_color = 'white') slider = ui.Slider() slider.action = slider_action slider.width= v.width lb = ui.Label(name = 'mylabel') lb.text = '0' lb.y = slider.y + 20 v.add_subview(slider) v.add_subview(lb) v.present('sheet')
Webmaster4o last edited by
@Phuket2 I think he was talking about doing I the other way around.
@Webmaster4o , yes I think so. But rather than not answer him I wanted to give him an idea. Just thought to make the other way, would be very difficult to follow. I think the actual question was about an automatic link done in the ui designer. Just wanted to show something possible in code, but to do it, he is better to read about the ui module first. The other way around you need delegates etc.. It's not hard, but you need to know about them
Ok, here is a dirty update to go the other way also. Look it's rough but you get the idea.
# coding: utf-8 import ui import string class MyTextFieldDelegate (object): def textfield_should_begin_editing(self, textfield): return True def textfield_did_begin_editing(self, textfield): pass def textfield_did_end_editing(self, textfield): pass def textfield_should_return(self, textfield): textfield.end_editing() self.update_slider(textfield) return True def textfield_should_change(self, textfield, range, replacement): if not range: self.update_slider(textfield) return True if replacement not in string.digits : return False return True def textfield_did_change(self, textfield): pass def update_slider(self, textfield): val = float(textfield.text) / 100. slider = textfield.superview['myslider'] slider.value = val def slider_action(sender): # get the sliders superview a.k.a view v = sender.superview v['mylabel'].text = str(sender.value) if __name__ == '__main__': f = (0, 0, 300, 400) v = ui.View(frame = f, bg_color = 'white') slider = ui.Slider() slider.name = 'myslider' slider.action = slider_action slider.width= v.width lb = ui.Label(name = 'mylabel') lb.text = '0' lb.y = slider.y + 20 tf = ui.TextField() tf.height = lb.height tf.delegate = MyTextFieldDelegate() tf.y = lb.y + lb.height + 20 tf.text_color = 0 v.add_subview(slider) v.add_subview(lb) v.add_subview(tf) v.present('sheet')
Cethric last edited by
def textfield_should_change(self, textfield, range, replacement): if not range: self.update_slider(textfield) return True if replacement not in string.digits : return False
def textfield_should_change(self, textfield, range, replacement): if not range and replacement in string.digits: self.update_slider(textfield) return True
As checking if the new entry is a digit after converting the entry to a integer is a bit late? Or have I read that code wrong?
Hi guys thank you for you help. May have gone over my head- my explanation is also lacking. Forgive me - I'm very new to this:
There are two things I'd like to happen in this script.
When I flick either of the three switches I would like the textfield at top left to update to metres, feet or milliseconds (/speed of sound) - at present I have to flick switch and move slider prior to field updating.
I would like to have the slider position update based on text input (numeric only) and relative to which switch is active.
Next two forum posts are my code and ui
# coding: utf-8 import ui import clipboard import decimal from random import random from console import hud_alert def switch_action1(sender): '@type sender: ui.Switch' v = sender.superview sw1 = v['switch1'].value sw2 = v['switch2'] sw3 = v['switch3'] if (sw1): value = 1 sw2.value = False sw3.value = False def switch_action2(sender): '@type sender: ui.Switch' v = sender.superview sw1 = v['switch1'] sw2 = v['switch2'].value sw3 = v['switch3'] if (sw2): value = 1 sw1.value = False sw3.value = False def switch_action3(sender): '@type sender: ui.Switch' v = sender.superview sw1 = v['switch1'] sw2 = v['switch2'] sw3 = v['switch3'].value if (sw3): value = 1 sw1.value = False sw2.value = False def slider_action(sender): v = sender.superview numbers = v['slider1'].value v['numberval'].text = '%.fm' % (numbers*100.00) v['mval'].text = '%.2fm' % (numbers*100.00) v['msval'].text = '%.3fms' % (numbers*2.92*100) v['ftval'].text = '%.2fft' % (numbers*3.37*100) sw1 = v['switch1'].value sw2 = v['switch2'].value sw3 = v['switch3'].value metres = v['mval'].text msecs = v['msval'].text feet = v['ftval'].text if sw1: value = 1 v['mfield'].text = metres v['numberval'].text = metres v['numberval'].text_color = 'd25050' v['slider1'].tint_color = 'd25050' if sw2: value = 1 v['mfield'].text = msecs v['numberval'].text = msecs v['numberval'].text_color = '6bc68b' v['slider1'].tint_color = '6bc68b' if sw3: value = 1 v['mfield'].text = feet v['numberval'].text = feet v['numberval'].text_color = '6a93fb' v['slider1'].tint_color = '6a93fb' v = ui.load_view('VertecAxUi') slider_action(v['slider1']) switch_action1(v['switch1']) switch_action2(v['switch2']) switch_action3(v['switch3']) if ui.get_screen_size() >= 768: v.present('popover') else: v.present()
I can't find if I can send an image here so I'll try to describe ui in text
Text.field (displays slider value)
Slider (updates textfield taking multiplier value from which of three switches is active)
Switch 1(makes text field display slider in metres)
Switch 2(displays in feet)
Switch 3(displays in milliseconds/ speed of sound)
Label(displays value of active switch and changes text colour to match switch Color)
JonB last edited by
you just need to update the labels within the switch action. You might consider a separate update_labels() method which can be called from each switch, slider or textbox action as the final display update.
@Cethric , possibly. It has been a while since using that delegate. I just tried to get something working quickly to show the concept. Difficult some times giving examples. I would not have done it that way, I would have used a custom class etc...
I wonder sometimes about giving advise here. I can sort of help... Sort of 😱 In a way I would prefer not to. Only, because there are so many people better than me. But at the same time, I think I should try and not have only a few trying to support the many, myself included. Maybe I over think it. Oh well sh*t happens 😬
Cethric last edited by
Sometimes our best way of learning is by teaching/supporting/helping others as it forces us to think differently about something that we have learned. Keep teaching/supporting/helping people on here. At the end of the day you might know something that others don't, or can provide a shorted possibly better answer than what others could do.
If the answer you give doesn't go so well, you have the safety net of everyone else around, so long as you learn from it.
Off Topic : my two cents and Freudian babble
Sharing information is everything.
I work in the events industry. Over 20 years as a vision tech, live audio engineer and more recently TD and production manager.
For at least 15 of those years I've fought against the tendency of some professionals to want to hoard knowledge and not impart skills.
All this achieves is bad practice across all sectors (hobby programming included) and eventually when one needs assistance one can't find skilled help as you get peers that don't have the skills to be supportive.
Any argument that imparting knowledge disadvantages the teacher is based on nothing more than fear.
I order to stay at the top of our game we must research and improve eternally. If we all strive to build on our skill-sets then naturally we will always be ahead of those newer to the game and behind those with more years of service than us. This is how it's meant to be.
I shouldn't complain. Three months ago after a life changing event (ruptured aneurysm) I left the events production industry for a permanent job at a University working as a trainer. Now I get to choose how knowledge is imparted every day.
I've got time to breathe and time to learn all those things I'd been putting off. Like Python...