omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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.


    New to Python....

    Pythonista
    4
    10
    2281
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • OkieWolf
      OkieWolf last edited by ccc

      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.

      1 Reply Last reply Reply Quote 0
      • ccc
        ccc last edited by

        Post your code here?

        OkieWolf 1 Reply Last reply Reply Quote 0
        • 7upser
          7upser last edited by

          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 1 Reply Last reply Reply Quote 0
          • OkieWolf
            OkieWolf @ccc last edited by ccc

            @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
            
            1 Reply Last reply Reply Quote 0
            • OkieWolf
              OkieWolf @7upser last edited by 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 1 Reply Last reply Reply Quote 0
              • mikael
                mikael @OkieWolf last edited by 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
                
                1 Reply Last reply Reply Quote 0
                • 7upser
                  7upser last edited by

                  @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 1 Reply Last reply Reply Quote 0
                  • OkieWolf
                    OkieWolf @7upser last edited by ccc

                    @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')
                    
                    1 Reply Last reply Reply Quote 0
                    • 7upser
                      7upser last edited by

                      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 1 Reply Last reply Reply Quote 1
                      • OkieWolf
                        OkieWolf @7upser last edited by

                        @7upser I really appreciate your help!!

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post
                        Powered by NodeBB Forums | Contributors