@JonB Wonderful, it works! I used the code:
tb.superview().superview().addSubview_(btn_obj)
(Thanks @cvp)
Really, @JonB how can you know these things? However, now I have a code that creates, every time I launch Pythonista, two buttons, at left and at right of upper bar. The left one, if touched, shows six little squares buttons (one button for one action). The right one, if touched, hides the six buttons.
Maybe you will find this a little easy, but for me it is a great thing ;-)
Two questions:
1. How can I program the left button in order to use it both for showing and hiding of the six user buttons? I mean, when the six buttons are showed, touching the left button should hide them; when the six buttons are hidden, touching the left button should show them.
2. Can Pythonista execute automatically a script every time user changes iphone screen from portrait to landscape and viceversa? Inside 'pythonista_startup.py'?
@cvp thank you for info about buttons size. But the last two values in:
btn=ui.Button( frame=((index)*40,22,40,40))
are a bit strange for me: what do they mean (40 and 40)?
And I can't show any text inside my buttons. Can you kindly show me what and where I must add some code for button text? Let's imagine the user button must show the text "Copy": what should I do in the following code (JonB's apphack.py) to add text inside the button?
# coding: utf-8
''' 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 __ '''
from objc_util import *
import ui,console
import weakref
from functools import partial
w=ObjCClass('UIApplication').sharedApplication().keyWindow()
main_view=w.rootViewController().view()
def get_toolbar(view):
#get main editor toolbar, by recursively walking the view
sv=view.subviews()
for v in sv:
if v._get_objc_classname().startswith(b'OMTabViewToolbar'):
return v
tb= get_toolbar(v)
if tb:
return tb
def create_toolbar_button(action,image,index=0,tag=''):
'''create a button on main toolbar, with action, imagename, index location, and string tagname. button and action are stored in __persistent_views[index]. tag allows finding view using tb.viewFromTag_(hash(tag)) (old idea)'''
assert(callable(action))
tb=get_toolbar(main_view)
global __persistent_views
try:
__persistent_views
except NameError:
__persistent_views={}
#check for existing button in this index and delete if needed
remove_toolbar_button(index)
#add new button to the left of the rightbuttons. index 0 is next to left buttons, index 1 is further left, etc
#store so it is not cleared.
btn=ui.Button( frame=((index)*40,22,40,40))
btn.flex='L'
btn.image=ui.Image.named(image)
btn.action=action
btn_obj=ObjCInstance(btn)
btn_obj.tag=hash(tag)
__persistent_views[index]=(btn,action,tag)
tb.addSubview_(btn_obj)
#JonB suggestion:
tb.superview().superview().addSubview_(btn_obj)
return btn
def remove_toolbar_button(index):
global __persistent_views
try:
btn,action,tag=__persistent_views.pop(index)
btn.action= None
ObjCInstance(btn).removeFromSuperview()
except KeyError:
pass
if __name__=='__main__':
def run_script(sender):
'''run a script without clearing glbals'''
import editor
exec(editor.get_text(),globals())
remove_toolbar_button(0)
create_toolbar_button(run_script,'iow:stop_256',0,'execfile')
Thank you so much!
Regards