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.
Programmable keys in Pythonista for quickly wrench actions execution
-
Right, your actions have to stand alone, and cannot rely on globals, or external imports. All imports must happen in the def itself. That's because globals and modules get cleared.
-
@JonB Why did __persistent_views not disappear?
-
-
@Matteo Really, @JonB did all the work, it was described in his comment at begin of script
''' A set of tools to add or delete custom buttons from the toolbar. This may not be super robust, but seems to work ok. Button objects and actions are saved so they survive global clearing, but thid has not been tested extensively. If a function relies on imports that occured outside of the function, these might dissappear -- user must make sure those modules are added to a module that is kept by pythonista, such as anything in site-packages, or name starting with __ '''```
-
@cvp variables with double underscores don't get cleared when running a script. Nor do modules in site-packages, modules starting with dunders, and a few other rules which I can never remember (you can look at pythonista_preflight.py)
-
@JonB Ok, understood, thanks. But, in this case, why to set this variable as global?
-
@cvp Yes you are right, I've tried to read JonB string at the beginning, but I don't have great programming knowledge so, when I read, lets say, 100 words about programming, I understand, the first time, about 20-30 %.
Anyway I will try to work with JonB script, that in my opinion gives to Pythonista much power for little screens (I repeat that Wrench action window of Pythonista is a wonderful feature, but in my little screen its window hides all my screen preventing me to see my code in text editor window).Regards
Bye -
-
@JonB Sorry, I feel very very little/bad/novice/zero (delete eventual useless adjectives, if any) but even that I can't 😢: I don't find this Pythonista_preflight?py
-
@cvp It's an internal script used by Pythonista, you can't find it anywhere in Pythonista's file browser. You can open it using the
editor
module though:import editor import os py3kit_dir = os.path.dirname(os.path.dirname(os.__file__)) editor.open_file(os.path.join(py3kit_dir, "pykit_preflight.py"))
-
@dgelessus Thanks, but I searched for pythonista_preflight.py 😢
-
@dgelessus & @JonB Thanks to both to help me to learn each day something's new.
-
It's the same script - in older versions it was called
pythonista_preflight.py
, then it was renamed topykit_preflight.py
. I think @JonB was just using the old name. -
@dgelessus Thanks
-
By the way, here are my notes on ways to keep variables/modules from getting cleared by preflight:
ways to keep modules from clearing:
- for modules, delete
__file__
e.g
import mymodule del mymodule.__file__
now mymodule is not removed from sys.modules
2) create name staring with dunder__
import mymodule sys.modules['__mymodule']=sys.modules['mymodule']
- set file access to nonwritable
chmod(mymodule.__file__,0xo444)
- place module in site-packages
ways to keep global vars from being cleared
- start variable with __
__myglobalvar=4
- add to pythonista_startup
myglobalvar=4 import pythonista_startup pythonista_startup.myglobalvar=myglobalvar
for things like ui.Views, threads, which needs to reference itself, but does not need to be refernced externally, etc it is sufficient to simply add the var to an existing builtin module
try: ui.mysavedviews.append(myview) except NameError: ui.mysavedviews=[myview]
retain_global
is another way. - for modules, delete
-
@JonB Thank you for these advanced info.
About your code, I've tried to understand how to add an extra horizontal bar with extra six buttons under the default bar.
In this the extra bar under the main one will hide the filename , the run button |≥ and some other built-in keys. But this is not a problem because user can hide the two bars with one touch on the half button in high corner in order to use run key or others.
The target is to have about 10/12 user keys that , when visible, hide the entire height of Pythonista bar.
Unfortunately I can't create this extra bar under the one you see in image.As always, I ask you if you have time how I can modify your code to add an extra bar with extra 6 buttons.
Thank you again
Regards -
Hi! The empirical approach always helps (almost always indeed). After some tests with some changing of numeric values and adding some extra code (copying/modifying some JonB's pieces of code of his Great script) the output is the following (images below).
First (file browser):
Second (console):
I wait to post the full code (that is the JonB's code slightly modified) because I have a little problem with screen rotation: from portrait to landscape and from landscape to portrait the two bars can't return to default one (you can see the problem in image below: the bars become very large...):
I will try to solve this bug that I've introduced modifying the JonB's script and if I will find a solution I will post here the full code, that you can personalize with icons and actions (I can't insert text in background under the buttons/icons ... It would be useful for whose like me can't remember the action of each button/icon without any keyword like, for example, 'find in files' or 'on/off' word wrap).
Regards
-
fwiw, in my previous version you can include a title and image, or could include both. Maybe a more useful approach would be either a help icon, which when active replaces all actions with a hud alert with the name, or, long press gestures that do the same. We could also do something where really tiny text is added to the image icon.
How do you want rotation to behave? Resize the bar and reorganize (fit on one line if there is room)? Or still show two row bar?
I would recommend a single bar, set to the size you want, with set to only flex='L' . You would size it manually, to fit portrait. The add action would then not do any resizing, but would need to keep track of how many buttons have been added, and start adding to the second row. or just accept a row parameter. -
Hi @JonB sorry for delay, thank you for your answer! Yes, you are right about text , but in little screens, text near buttons is space consuming: in little screens , as you already told me in your last post, it would be useful to have tiny text inside buttons (black text on blue buttons should be well visible). Maybe text with maximum 5/6 letters, but I don't know if it would be visible.
About my bug, this weekend I will try to have the same effect I have with your code when I change screen from portrait to landscape: I like when, with your code, the bar disappears in landscape mode and appears again when in portrait mode. With the code modified by me in order to add an extra bar for extra 6 buttons, the script works well but after changing screen mode from portrait to landscape and to portrait again, the two bars in my modified code can't be resized as after the first run of the script.
I will try to debug the code to have two working bars in portrait, even if I change accidentally the screen rotation of phone.
Sorry for my poor answer now but next weekend I will have some time to spend with Python!
Thank you
Regards
Bye -
Hi @JonB , I've solved partially my problem.
Now the script resizes correctly the two bars when I return to portrait mode from landscape. I've modifiedself.overlay.flex = 'lwh'
in
self.overlay.flex = 'l'
The problem now is that the two bars are visible also in landscape mode but I can't hide them because the triangle button that shows-hides the bars is invisible (out of screen) when in landscape mode.
My questions:
- how can I modify this code (your code slightly modified with an empirical approach, ie without knowing what I'm doing ;-)) in order to hide the two bars when my phone is in landscape mode? If the two bars are visible in portrait mode and I change in landscape mode, the two bars should disappear and if I return in portrait mode they should be visible again.
- How can I change the shape (geometric polygon) and position (x, y) of the half-button at the top right corner of the screen in order to show it also in landscape mode? In other words, why it disappear when in landscape mode?
Thank you for help
Regards