@BlockSweeps By default, the views loaded from your UI file aren't available as Python variables. If you want to access your views from Python, you need to store the root view (returned from
ui.load_view) in a variable and use that to access the other views:
import ui def hit_button(sender): # root['textfield1'] means "root's subview with the name textfield1" root['textfield1'].text = 'Hello' root = ui.load_view('My UI') root.present('sheet')
No, Pythonista 3 is a separate app, so you need to purchase it again. For a while after Pythonista 3 was released, there was a bundle with Pythonista 2 and 3 in it, which allowed you to get Pythonista 3 at a lower price if you already owned Pythonista 2. This is no longer possible though. (Pythonista 2 is also no longer available for purchase - Pythonista 3 supports both Python 2 and 3, so there's no advantage to buying version 2 instead of 3. If you already own the app, you can still redownload it of course.)
If you do buy Pythonista 3, there is one upgrade-related feature that you can still use. If you go into Pythonista 3's settings, you can enable the "Pythonista 2 Files" option. This shows your Pythonista 2 Script Library as a special folder inside the Pythonista 3 file list, which you can use to easily transfer your files from Pythonista 2 to 3, using the normal "move" button in the file list. You can disable the option later to hide the Pythonista 2 files again (nothing will be deleted of course).
@technoway Python actually has a built-in limit on how deep the Python call stack can go. You can get the current limit with
sys.getrecursionlimit()and change it with
sys.setrecursionlimit(limit). The default limit is 1000, which is normally enough that you don't overflow the native stack even if you hit the Python recursion limit. In Pythonista that seems to be too much though, if I run an infinitely recursing function with the default limit, Pythonista crashes. With a lower limit (such as 500) I get a Python
RecursionErroras expected. I have no idea what the size of the native stack is on iOS, but it's probably lower than on Mac.
This is a "feature" in Python and is indeed the expected behavior (though it's not exactly helpful). Stopping a script in Pythonista does the same thing as pressing Ctrl+C when you run Python in a terminal: it raises a
KeyboardInterruptexception. Normally that causes the script to cleanly stop, while still running all exception handlers. Of course that doesn't help if you catch the
KeyboardInterruptexception, which you are doing indirectly here with the unrestricted
exceptblock. As you found out, you can still stop your script by terminating the app though.
If you have a real script where you want to catch exceptions but not prevent stopping the script, you should add the exception type you want to catch to the
exceptblock, for example
except ValueError. If you want to catch all "normal" exceptions, use
except Exceptionrather than
except- the latter catches literally everything, whereas the former lets some "special" exceptions through (like
By the way, the code that you posted can actually be stopped, you just need to press the stop button roughly 1000 times. At some point you'll hit Python's default recursion limit, because every time you catch the exception, you call
Pythonista's main "Script Library" folder (where you have your scripts etc.) has a very long name, which is randomly generated by iOS and is different on every device. This makes it impossible to type out the full paths like you would on a normal computer.
There is a way around this problem though: using the
os.path.expanduserfunction, you can get the full path of a file in this randomly named folder. First, put
import osat the top of your script so you can use the
osmodule. Now you can use
os.path.expanduser("~/Documents")to get the path to Pythonista's "Script Library" folder. This means that if you have a file "Assignment1.pdf" in the Script Library, you can get its path using
For example, if your
pathslist currently looks like this:
paths = [ "/Users/Jean/Dropbox/Assignment1.pdf", "/Users/Jean/Dropbox/Assignment2.pdf", ]
you would change it to
paths = [ os.path.expanduser("~/Documents/Assignment1.pdf"), os.path.expanduser("~/Documents/Assignment2.pdf"), ]
The system-wide font size setting (which you can change in the iOS Settings app) also affects the size of some text in Pythonista, including the file list. Many parts of Pythonista aren't affected though (such as the settings screen and some other table views) and the behavior might be different in the App Store version and the beta.
This is not currently possible. The "Run Pythonista Script" action doesn't mean "run this file with Pythonista", it means "run a Python script that works with this file". At the moment the only way to run a Python script from iCloud is to copy and paste the text into Pythonista.
The current beta version of Pythonista has much better integration with iCloud and the Files app, and it allows opening files in Pythonista directly, without copying them. I don't know when the next release of Pythonista will come out though.
MyClassis a subclass of
ui.Viewalready has its own
__init__method, and now we also define an
__init__method in our subclass. By default, our subclass
__init__would completely replace the
ui.View. This would not be good, because then the code in
ui.View.__init__would never run for our
MyClassviews, and they would not be set up properly. To avoid this, we call
super().__init__(*args, **kwargs), which is (in this case) a shortcut for
ui.View.__init__(self, *args, **kwargs). That is, we let
ui.Viewinitialize our custom view before we do our own initialization.
**kwargssyntax is used here to pass all arguments from our own
__init__to the superclass
__init__. In a function/method definition,
*argsstands for "accept any number of positional (unnamed) arguments and put them into
argsas a tuple", and
**kwargsstands for "accept any number of keyword (named) arguments and put them into
kwargsas a dictionary". In a function/method call, they do basically the opposite, and pass the contents of the given tuple/dictionary as arguments.