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
-
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):
-
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".
-
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:
-
Awesome! That shortened it a lot :)
Thanks for the great info @omz!
Also, the inspect module was really handy ;) -
-
@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> -
@omz: The problem subclassing TextLayer is that Button class calls at init TextLayer not MutableTextLayer. So imho subclassing Button is also required.
Thanks. -
@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.
-
@eliskan Thanks :)
The eval function makes a lot of things much easier :) -
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