Programmable keys in Pythonista for quickly wrench actions execution
@dgelessus Thanks, but I searched for pythonista_preflight.py 😢
It's the same script - in older versions it was called
pythonista_preflight.py, then it was renamed to
pykit_preflight.py. I think @JonB was just using the old name.
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
import mymodule del mymodule.__file__
now mymodule is not removed from sys.modules
- create name staring with dunder
import mymodule sys.modules['__mymodule']=sys.modules['mymodule']
- set file access to nonwritable
- place module in site-packages
ways to keep global vars from being cleared
- start variable with __
- 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_globalis 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
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):
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).
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!
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 modified
self.overlay.flex = 'lwh'
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.
- 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
Hi @JonB, I explain also (I had forgotten to do it in last post) the text-on-icons idea (is it what you have in mind?) with the image below:
The image you see is made with a very simple image editor software in pc.
But if you could make it possible to insert text in the foreground on the buttons in your script like in the image, in addition to understanding how to hide the two button bars in landscape mode it would be very useful.
As always, only if you have time to spend on it and if it suits your interest.
@Matteo You could play, instead of btn.image, with btn.background_image and btn.title
@Matteo Or you could design your own icons with eventual text
@Matteo Or, better, I think, you could add a label on the button with
lbl = ui.Label() lbl.frame = (0,0,btn.width,btn.height) lbl.text = 'copy' btn.add_subview(lbl)```
Hi @cvp, thank you for your help! Unfortunately I need to add text like in last image I've posted in the @JonB script, and even if I understand something about your piece of code, I don't know where and how to implement it in JonB script (I have no skills about it). If you kindly could show me some hint on how to add text, like in image, inside one single button in the script at point 1 of post number 51 , it would be more easy for me to do that for all my personal buttons.
Really sorry if I ask a full working script starting by an existing one, but it is the only way I can learn something deep about Pythonista (and sometimes I'm a little lazy).
Anyway I will try again to implement your code in JonB script.
Try this but text can't be long because button is little
Do not use passed title as button title but as label text
b=ui.Button(title=None,image=image,action=action) b.size_to_fit() self.overlay1.add_subview(b) lbl = ui.Label() lbl.frame = (0,0,b.width,b.height) lbl.font = ('<System-Bold>',16) lbl.text = title b.add_subview(lbl)
@cvp You are right, it works! The trick is to change
Hi guys! Only to say that I've understood (by trying several times and by searching in Internet) how to hide the two bars when in landscape:
Sorry if for you all is easy, but it was not very easy for me ;-) For example I ask myself "what does 't' mean instead of 'l' in flex?"
Now I have a full working script (thanks again to @JonB and @cvp for great support about user buttons request) that I think is good enough for iPhone 5s! I'm happy because I've obtained what was my target!
Here the script.
Thank you all,
Flex tells you what happens when the containing view is resized. The way to envision things are springs or bars connecting the top, left, right and bottom of the view to the corrrsponding edge of the parent, and also a bar across (width) and vertical (height) of your view.
By "flexing" one of these, it becomes a spring.
So, for example, 'W' will allo the width to be flexible -- so keeps the same margin to the parent view by adjusting your view's size.
L means that the left side can float (anchors to the right)
LW means it is anchored to the right side, and the width is resized as the parent is resized.