Forum Archive

New to Python....

OkieWolf

I have experience with Lisp, VB .NET, VBA but this is my first time with Python so please excuse my lack of Python knowledge. I feel really stupid that I can’t figure out this simple little script.

I’m writing a program to calculate arc length given a radius and angle. The formula is simple.... arc = radius * angle * pi / 180.
I have a dialog with textfield1 which is radius, textfield2 which is angle, and button1 which is the calculate button. There is also a label1 to hold the answer. I can type in both the radius and the angle but can’t get anything to happen when I click calculate.

ccc

Post your code here?

7upser

Did you set button.action?
Here is a quick + dirty Example in Pythonista


import ui
import math

class cUIView(ui.View):
    def __init__(self):
        self.name = 'Title'
        self.width = 300
        self.height = 200

        self.lbl01 = self.makeLabels('angle')
        self.lbl02 = self.makeLabels('radius')
        self.lbl03 = self.makeLabels('arc length')
        self.txtView01 = self.makeTextview()
        self.txtView02 = self.makeTextview()
        self.txtView03 = self.makeTextview()
        self.btn01 = self.makeButtons('btn01')

        self.style()

    def layout(self):
        # layout of the ui.view, will be called when a view is resized
        self.lbl01.frame = (10, 10, 100, 40)
        self.lbl02.frame = (150, 10, 100, 40)
        self.lbl03.frame = (10, 100, 100, 40)
        self.txtView01.frame = (10, 50, 100, 40)
        self.txtView02.frame = (150, 50, 100, 40)
        self.txtView03.frame = (10, 140, 100, 40)
        self.btn01.frame = (150, 100, 100, 40)

    def style(self):
        # the view style
        self.background_color = 'lightgrey'
        self.txtView01.text = str(1.5)
        self.txtView02.text = str(10)

    def makeButtons(self, vName):
        vButton = ui.Button()
        vButton.name = vName
        vButton.title = vName
        vButton.border_width = 1
        vButton.action = self.btnAction
        self.add_subview(vButton)
        return vButton

    def makeLabels(self, vName):
        vLabel = ui.Label()
        vLabel.name = vName
        vLabel.text = vName
        vLabel.border_width = 1
        vLabel.alignment = ui.ALIGN_CENTER
        self.add_subview(vLabel)
        return vLabel

    def makeTextview(self):
        vTxtView = ui.TextView()
        vTxtView.border_width = 1
        self.add_subview(vTxtView)
        return vTxtView

    def btnAction(self, vSender):
        if vSender.name == self.btn01.name:
            pi = math.pi
            angle = float(self.txtView01.text)
            radius = float(self.txtView02.text)
            arc = radius * angle * pi / 180
            self.txtView03.text = str(arc)

if __name__ == '__main__':
    vView = cUIView()
    vView.present('sheet')

OkieWolf

@ccc at this point I’m just trying to get rad to show up in a label so I know the variable got set

import ui
import math

v = ui.load_view()
v.present('sheet')


def button1_tapped(sender):
    rad = sender.superview['textfield1'].text
    ang = sender.superview['textfield2'].text
    pi = math.pi
    sender.superview['label1'].text = rad
OkieWolf

@7upser That works but I don’t understand why. If you use Pythonista, why can’t you set the dialog up in the pyui screen instead of. coding everything?

mikael

@OkieWolf, you can do UIs both ways.

I think your code just needs to connect the action method to the button, something close to:

v['button1'].action = button1_tapped
7upser

@OkieWolf, I think @mikael is write. You can do it with the pyui Editor too.

tag

You need also change the order of your Pythoncode (def needs to be before the main Program).


import ui
import math

def button1_tapped(sender):
    rad = sender.superview['textfield1'].text
    ang = sender.superview['textfield2'].text
    pi = math.pi
    sender.superview['label1'].text = rad


v = ui.load_view()
v.present('sheet')

OkieWolf

@7upser

That worked with the following code....

# ArcLength

import ui
import math


def button1_tapped(sender):
    v = sender.superview
    rad = v['textfield1'].text
    ang = v['textfield2'].text
    pi = math.pi
    v['label2'].text = 'radius = ' + rad
    v['label3'].text = 'angle = ' + ang
    arc = rad * ang * pi / 180
    v['label1'].text = 'Arc Length = ' + arc

v = ui.load_view('ArcLength')
v.present('sheet')

But now it errors on the arc = rad * ang * pi / 180 line.
It says “can’t multiply sequence by non-int of type ‘str’ “

I tried putting str everywhere I could but still errors on that line.

....never mind.....
fixed with following code....

import ui
import math


def button1_tapped(sender):
    v = sender.superview
    rad = float(v['textfield1'].text)
    ang = float(v['textfield2'].text)
    pi = math.pi
    arc = rad * ang * pi / 180
    v['label1'].text = 'Arc Length = ' + str(arc)

v = ui.load_view('ArcLength')
v.present('sheet')
7upser

Hi, @OkieWolf

You can post your Code with the top right button in the forum Editor.

This result in better formatting Code.

To debug your Code:
Close your UI View and click the right Button where you see the Errormessage.

tag

Then click on Variables

tag

There you can see that ang and rad are strings.
You can also set Breakpoints by tap and Hold within the Editor.
Or you can use a print command: print(type(rad))

You only need to convert them to float. And the Result back to string

arc = str(float(rad) * float(ang) * pi / 180)

Edit: See you solve your Problem

OkieWolf

@7upser I really appreciate your help!!