Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Can not get segmented control to pass variable
-
I am trying to make a voltage drop calculator but it keep getting an error message that a string can’t be converted to a float, and when I look in the vars the cm, and con values show up as ‘ ‘. So I am doing something wrong. Please help!
# importing mods for equation and gui import ui import math # setting up view that the sub views can be inside and nameing the view view = ui.View() view.name = 'Voltage Drop' view.background_color = '#ffffff' # Variables E: voltage, L: length, I: amprage, con: conducter material, cm: circular mills of the chosen conducter. E = str() L = str() I = str() con = str() cm = str() # attempt at place holder for conductor resistence value ConLbl = ui.Label() ConLbl.text = con ConLbl.name = 'Conducter Value' ConLbl.x = 155 ConLbl.y = 380 ConLbl.width = 60 ConLbl.height = 20 ConLbl.border_width = 1 view.add_subview(ConLbl) # variables for copper and aluminium def segment_action(sender): con = ConLbl.text if sender.selected_index == 0: sender.superview['Conducter Value'].text = '12.9' elif sender.selected_index == 1: sender.superview['Conducter Value'].text = '21.2' # label for conductor cLbl = ui.Label() cLbl.text = 'Conductor' cLbl.x = 140 cLbl.y = 5 view.add_subview(cLbl) # segmented control for conductor material cOa = ui.SegmentedControl(con) cOa.segments = ('Copper', 'Aluminium') cOa.action = segment_action cOa.x = 80 cOa.y = 70 cOa.width = 200 view.add_subview(cOa) # label for wire gauge awgLbl = ui.Label() awgLbl.text = 'AWG' awgLbl.x = 165 awgLbl.y = 90 view.add_subview(awgLbl) # attempt at place holder for cm value cmLbl = ui.Label() cmLbl.text = cm cmLbl.name = 'CM Value' cmLbl.x = 155 cmLbl.y = 410 cmLbl.width = 60 cmLbl.height = 20 cmLbl.border_width = 1 view.add_subview(cmLbl) # segmented control action and variables for wire gauge in AWG def segment_action2(sender): if sender.selected_index == 0: sender.superview['CM Value'].text = '4107' elif sender.selected_index == 1: sender.superview['CM Value'].text = '6530' elif sender.selected_index == 2: sender.superview['CM Value'].text = '10383' elif sender.selected_index == 3: sender.superview['CM Value'].text = '16509' elif sender.selected_index == 4: sender.superview['CM Value'].text = '26251' elif sender.selected_index == 5: sender.superview['CM Value'].text = '41740' elif sender.selected_index == 6: sender.superview['CM Value'].text = '66369' elif sender.selected_index == 7: sender.superview['CM Value'].text = '105518' elif sender.selected_index == 8: sender.superview['CM Value'].text = '133056' elif sender.selected_index == 9: sender.superview['CM Value'].text = '167780' elif sender.selected_index == 10: sender.superview['CM Value'].text = '211566' # segmented control for awg selection awg = ui.SegmentedControl(cm) awg.segments = ('14', '12', '10', '8', '6', '4', '2', '1/0', '2/0', '3/0', '4/0') awg.action = segment_action2 awg.x = 15 awg.y = 150 awg.width = 350 view.add_subview(awg) # label for voltage vLbl = ui.Label() vLbl.text = 'Voltage' vLbl.x = 160 vLbl.y = 140 view.add_subview(vLbl) #text feild for voltage input volts= ui.TextField(E) volts.x = 155 volts.y = 200 volts.width = 60 volts.border_width = 1 volts.height = 20 view.add_subview(volts) # label for length of wire run LenLbl = ui.Label() LenLbl.text = 'Length' LenLbl.x = 160 LenLbl.y = 200 view.add_subview(LenLbl) # text feild input for length (L) length = ui.TextField(L) length.x = 155 length.y = 260 length.width = 60 length.border_width = 1 length.height = 20 view.add_subview(length) # label for amprage ampsLbl = ui.Label() ampsLbl.x = 160 ampsLbl.y = 265 ampsLbl.text = 'Amps' view.add_subview(ampsLbl) #text feild input for amprage(I) amps = ui.TextField(I) amps.x = 155 amps.y = 330 amps.width = 60 amps.height = 20 amps.border_width = 1 view.add_subview(amps) # label for result resLbl = ui.Label() resLbl.text = 'Result' resLbl.x = 150 resLbl.y = 420 view.add_subview(resLbl) # label that shows result voltage drop calc Result = ui.Label() Result.text = str() Result.x = 100 Result.y = 500 Result.width = 200 Result.height = 20 Result.border_width = 1 view.add_subview(Result) # function for voltage drop calc performed after inputs and button pushed def button_tapped(sender): I = float(amps.text) L = float(length.text) E = float(volts.text) Result.text = (2*float(con)*float(L)*float(I))/float(cm) # button to run calc after inputs are entered button = ui.Button(frame=[150, 550, 100, 40], title=' Calculate ') button.background_color=('white') button.action = button_tapped button.tint_color = ('black') button.border_width = 2 view.add_subview(button) # establishing parent view for child views to present in view.present('fullscreen') ```
-
This post is deleted! -
@Ahab3030 you fill the label.text but not the con variable which stays empty thus not convertible to float.
-
@Ahab3030 and you don't convert the result of the calculation to a string
Trydef button_tapped(sender): I = float(amps.text) L = float(length.text) E = float(volts.text) con = sender.superview['Conducter Value'].text cm = sender.superview['CM Value'].text Result.text = str((2*float(con)*float(L)*float(I))/float(cm))
-
@cvp that worked great! Thanks a lot, post at night answer by morning I love this forum!
-
@Ahab3030 for the fun
def segment_action2(sender): sender.superview['CM Value'].text = str([4107,6530,10383,16509,26251,41740,66369,1005518,133056,167780,211566] [sender.selected_index])