Forum Archive

Read only text field

BGKirkham

I’m new to GUI programming in Pythonista and I’m writing a simple little iPad/iPone app that takes a few inputs, performs a calculation and writes the result to a TextField. I want to make the result TextField read-only. In other words, when the user clicks on it the keyboard isn’t displayed. Seems like such a simple thing, but so far I haven’t had any success finding any help through Google. Other development environments I’ve used give you access to that property in the GUI designer and at runtime. Can anyone help me?

Second, even though it’s read only, I still want to allow the user to copy the value to the clipboard.

cvp

@BGKirkham why a TextField and not a Label?

cvp

@BGKirkham or you can use the attribute enabled, like

import ui
tf = ui.TextField()
tf.text = '123'
tf.enabled = False
tf.present('sheet') 
BGKirkham

@cvp I guess I could have used a label, but the your “.enabled = false” did the trick. Thank you.

Now I just need to implement copy to clipboard. Don’t tell me. I want to try to figure it out first. 😀

cvp

@BGKirkham You're right. The only thing to know is "how to search in the doc"

BGKirkham

@cvp

I ended up “cheating” in a sense. I made the calculation result a button instead of a TextField or label. It made things easier for me because I’m not experienced enough yet with Pythonista. I attached my current code in case you’re interested. I still have to add input validation. Feel free to offer criticism, if you wish. I’m not sure how to provide the pyui file. I wish scipy was included with Pythonista.

import ui
import math
import clipboard

def derivative(f):
    def compute(x, dx):
        return (f(x + dx) - f(x)) / dx
    return compute

def newtons_method(f, x, dx = 0.00000001, tolerance = 0.000000001):
    df = derivative(f)

    while True:
        x1 = x - f(x) / df(x, dx)
        t = abs(x1 - x)

        if t < tolerance:
            break

        x = x1
    return x

def f(x):
    try:
            ret = -2 * math.log10((rr / 3.7) + (2.51 / (re * math.sqrt(x)))) - (1 / math.sqrt(x))
    except(SyntaxError, ZeroDivisionError):
            ret = 2 * math.log10((rr / 3.7) + (2.51 / (re * math.sqrt(.000001)))) - (1 / math.sqrt(.000001))

    return ret


def button_tapped(sender):
    '@type sender: ui.Button'
    # Get the button's title for the following logic:
    t = sender.name

    global re
    global rr

    if t == 'calc':
        # Get the labels:
        re = eval(sender.superview['reyNum'].text)
        e = eval(sender.superview['pipeRough'].text)
        dia = eval(sender.superview['pipeDia'].text) / 12

        rr = e / dia

        if re <= 2300 and re > 0:
            ff = 64 / re
        elif re >= 4000:
            x_approx = 0.00001
            ff = newtons_method(f, x_approx)
        elif re == 0:
            ff = 0
        else:
            ff = -1

        frictFactorObj = sender.superview['frictFactor']

        if ff > 0:
            frictFactorObj.title = '{0:.5f}'.format(ff)
        elif ff == 0:
            frictFactorObj.title = "No Flow"
        else:
            frictFactorObj.title = "Transition Flow"
    elif t == 'frictFactor':
        sender.superview['copyButton'].hidden = False
    elif t == 'copyButton':
        clipboard.set(sender.superview['frictFactor'].title)
        sender.superview['copyButton'].hidden = True



v = ui.load_view("Moody")

v['reyNum'].keyboard_type = ui.KEYBOARD_DECIMAL_PAD
v['pipeRough'].keyboard_type = ui.KEYBOARD_DECIMAL_PAD
v['pipeDia'].keyboard_type = ui.KEYBOARD_DECIMAL_PAD
v['copyButton'].hidden = True

v.present('sheet') 
ccc

@BGKirkham said:

```

The trailing ``` must be on a line all by itself or it will appear at the the end of your script.

cvp

@BGKirkham said:

I’m not sure how to provide the pyui file

An easy and quick way is
1) rename the .pyui into .txt
2) edit it
3) select all
4) copy
5) paste in forum post, like you do for the .py
6) rename it back to .pyui

BGKirkham

@cvp

I tried, but when I tried to post, it was flagged as spam

mikael

@BGKirkham, something wrong with the spam filter recently.

You need to jump through another hoop and turn it into a gist, so that you can just share the link.