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
-
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 -
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.
Thank you
Regards -
@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.
Regards -
@Matteo
Try this but text can't be long because button is little
Do not use passed title as button title but as label textb=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)
And
__s.add_action1(a,'txt',ui.Image.named('iow:arrow_resize_32'))
-
@cvp You are right, it works! The trick is to change
b=ui.Button(title=title,image=image,action=action)
in
b=ui.Button(title=None,image=image,action=action)
Thank you!
Regards -
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:
self.overlay1.flex='t' self.overlay2.flex='t'
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,
Regards -
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. -
Hi @JonB, thank you for explanation, but in general where can I find documentation about flex and other similar advanced things in order to customize Pythonista IDE?
Thanks
Regards -
Flex is somewhat documented in the you docs. The native version is called autoresizing masks, which you can do Google searches for.
The other approach that might work better for you is a custom view class that implements a layout method. Then, you can relayout the entire view when orientation changes, such as doing one long bar in landscape and two bars in portrait...
-
@JonB Thank you for your answer, and yes, it is interesting to know that user could have a view "A" when in portrait and a view "B" when in landscape. I like your idea about having two short bars in portrait and a single long bar in landscape, obviously it makes sense for little idevices, no problem with ipads.
The only problem about having a single long bar in landscape mode is how to show the triangle key at up-right corner in landscape in order to hide/show the bar based on user choice.
My curiosity: in Pythonista how can user write some python code that executes an external script when in portrait and an other external script when in landscape (I mean, immediately after rotating the device in portrait or landscape mode?)
Thank you very much,
Regards -
The original code ensured the right corner was aligned to the right side of the screen, then flex='L' kept it there.
Another option is a custom
layout
method, which would run code as you describe.It is possible to write a notification handler to register for rotation events. in objc that looks like
[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:)name:UIDeviceOrientationDidChangeNotification object:nil];
but in this case, the StatusBarOverlay would already get these events, so a method named layout would get called automatically, so i wouldnt go that complexity (have to create an objclass, objcinstance, and manage its lifetime)