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.
User Input to String Variable
-
Hello,
I've created a simple program to convert coordinate systems and am stuck on how to save user data entered into 3 different textfields to their respective string variables(basemap,zone,block)
I've seen the delegate solution but have trouble comprehending what is occurring. Is there a simple solution to this by creating a function?
SOLVED-> Also, I've tried to set the default keyboard to only show the phone pad but it is still showing the default keyboard.
Here is my code :-------
import sys import os import time import utm import simplekml import console #run the GUI import ui tf = ui.TextField() tf.keyboard_type = ui.KEYBOARD_NUMBER_PAD #def text_field_action(sender): # v = ui.load_view('UTM2KML.pyui') # tf = v['basemaptext'] # tf.action = text_field_action def getInput(view): textfield = view["textfield name"] input = textfield.text return input def mainscreen(): BASEMAP = getInput("basemaptext") BLOCK = getInput("blocktext") ZONE = getInput("zonetext") def createbutton(sender): #basemap #block #zone BASEMAP = getInput("basemaptext") BLOCK = getInput("blocktext") ZONE = getInput("zonetext") #The syntax is utm.to_latlon(EASTING, NORTHING, ZONE NUMBER, ZONE LETTER). BLOCK = "00" #converts the strings to ints #BASEMAP = int(BASEMAP) ZONE = int(ZONE) if BLOCK == "": EASTING = BASEMAP[0:-3] + BLOCK[0:-1] + "0000" NORTHING = BASEMAP[2:] + BLOCK[1:] + "0000" EASTING = int(EASTING) NORTHING = int(NORTHING) else: EASTING = BASEMAP[0:-3] + BLOCK[0:-1] + "000" NORTHING = BASEMAP[2:] + BLOCK[1:] + "000" EASTING = int(EASTING) NORTHING = int(NORTHING) #print(EASTING) #print(NORTHING) int(EASTING) int(NORTHING) int(ZONE) #Return (LAT,LONG) #print("Latitutde, Longitutde in Decimal Degrees") #print(utm.to_latlon(EASTING,NORTHING,ZONE,'N')) #split and reverse for the kml conversion location = str(utm.to_latlon(EASTING,NORTHING,ZONE,'N')) lati = location.split(',')[0] longi = location.split(',')[1] kmllocation =(longi.strip("()"),lati.strip("()")) #Create the kml file kml = simplekml.Kml() kml.newpoint(name="WAYPOINT", coords=[(kmllocation)]) #save kml location kml.save("Waypoints.kml") console.open_in("Waypoints.kml") v = ui.load_view('UTM2KML').present('fullscreen') #v.present('fullscreen')
-
can you add triple backquotes before/after your code? makes the formatting work out:
``` Your code ```
Does your pyui have textfields that you want to enter into? or do you want to use dialog inputs? your code seems to try to use dialogs instead textfields.
ui programming works a little differently than pure sequential programming. your main logic needs to come within the actions or delegates. A simple textfield_action would check all three textfields to see if there are valid values, then would use the data and update some other part of the ui to show the result. or maybe you have an "enter" button on the ui. you can either have your textfield_action refer to your root view directly (global), or else get there via sender.superview, at which point you can access the other subviews
zone=int(sender.superview['zonetextfield'].text)
etc
-
your main problem seems to be
getInput(view):
is not doing what you think it is -- it should be taking in a textfieldname argument, instead of hard coding "textfield name". you also are not actually passing it the view, so you have to decide whether to use globals, or not
getInput(textfieldname): return v[textfieldname].text
or
getInput(v,textfieldname): return v[textfieldname].text
but then of course you need to call it with the first argument being the root view.
-
your other problems: you dont actually define textfield_action anywhere. and, you load the view at the start, then load it again before you present it. load it once. set your actions, then present the same object.
import ui def text_field_action(sender): print('textfield data changed') v = ui.load_view('UTM2KML.pyui') tf = v['basemaptext'] tf.keyboard_type = ui.KEYBOARD_NUMBER_PAD tf.action = text_field_action [...] v.present()
note that here you would load the view Once, set attributes on the actual textfields in the view, not new instances that you then dont use.
note that the numeric pad only works for iphone, not ipad. see a recent thread by cvp for a workaround,
-
Hello Jon, I do have a create button when pushed converts the entered text and creates a KML.
How can I tie the two together?
-
your create button looks almost okay, if you fix the other problems mentioned.
you have a choice to either directly reference the global
v
, and access your textviews from there, or else usesender.superview
(sender is the button, so supeview is the view that contains it, so depending on how nested your views are you may need to go "up" more until you can traverse "down". Either approach works, though with the global approach you have to be careful whn presening aspanel
since you might later run another script that clears globals. -
Thank you beautiful people! Your suggestions worked! I'm working on limiting the characters entered and auto hiding the keyboard when trying to the character limit!
-