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.


    Calculator

    Pythonista
    4
    11
    6280
    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.
    • Sebastian
      Sebastian last edited by

      Hey guys! I made this <a href="https://gist.github.com/5382778">calculator</a>, which I think works pretty good.
      It's a simple graphical calculator with the usual like addition, subtraction, multiplication, division, percentage, square root and some other stuff. It should work on iPad and iPhone.
      Let me know what you think, and if there are any bugs!

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

        Very nice! I like how this has less than 100 lines of code. :)

        Perhaps you could add some sort of feedback for invalid input, for example like this (makes the text red and plays an 'error' sound):

        https://gist.github.com/omz/572fe9f33a37988ebc63

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

          Thanks! That's a great idea :) I'm glad I found the Button class, but I haven't found it in the docs. I found it kinda by accident as I created a Button class myself, and then I removed the class but the script still worked XD

          I've fixed one bug though, if you pressed the "%" button, and then typed a number, like for instance 25, it would suddenly change to 0.025. I fixed it by switching from "continue" to "pass".

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

            Oh, I didn't even notice you used the Button class – sneaky! ;)

            It's not documented because it's somewhat incomplete, but if you're curious how it works, you could try this in the console:

            <pre>import inspect
            import scene
            print inspect.getsource(scene.Button)</pre>
            (this can also come in handy for other things)

            You'll notice that it has an <code>action</code> attribute. That's actually a function that gets called when the button is tapped, and you could use it to get rid of all your manual touch handling code. Here's another modified version that takes advantage of this:

            https://gist.github.com/omz/e1ea63513f458578f144

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

              Awesome! That shortened it a lot :)
              Thanks for the great info @omz!
              Also, the inspect module was really handy ;)

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

                @omz and Sebastian, nice code. Thanks.

                Please, @omz: how to change text size in TextLayer class in your undocumented Button class ?

                And how about button pads text changing with context ?

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

                  @jose3f Neither <code>TextLayer</code> nor <code>Button</code> (which uses a <code>TextLayer</code> internally) support changing the text after initialization.

                  When you look at the source code for <code>TextLayer</code> with the <code>inspect</code> module, you'll see that it's a very simple class (under 10 lines of code) that basically just renders the given text as an image (using <code>render_text()</code>) and sets the result as the Layer's <code>image</code> attribute, adjusting the size of the layer accordingly. If you wanted to create a <code>TextLayer</code> subclass that allows changing the text, a simple implementation could look like this:

                  <pre>class MutableTextLayer (TextLayer):
                  def set_text(self, text, font, font_size):
                  img, size = render_text(text, font, font_size)
                  self.image = img
                  self.frame = Rect(self.frame.x, self.frame.y, size.w, size.h)</pre>

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

                    @omz: The problem subclassing TextLayer is that Button class calls at init TextLayer not MutableTextLayer. So imho subclassing Button is also required.
                    Thanks.

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

                      @Sebastion - Very awesome. I never knew about how to use eval.

                      Did some research and I wish I had known this sooner. I had written genetic algorithms and probably should have used eval to calculate their strings. Ended up basically writing a function to do the same thing :(

                      The more you know.

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

                        @eliskan Thanks :)
                        The eval function makes a lot of things much easier :)

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

                          Yeah my function ended up being a 20 line function that created a NEW function at runtime.. it was a solution I found that had worked for my problem, but really a short eval would have done the trick.

                          This program you wrote here was very elegant. I wish I could program that cleanly

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