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.
Flex for buttons
-
Does .flex not work for buttons?
-
Works for me. See below for a flex demo, using a button.
Keep in mind, if view B is a subview of view A, B's flex settings come into play when A's size changes.from threading import Timer import ui, console root=ui.View() somecontainer=ui.View(frame=(50,5,200,200),bg_color=(1,0,0)) root.add_subview(somecontainer) b=ui.Button(bg_color=(0,1,0),frame=(50,50,100,100)) somecontainer.add_subview(b) b.action=lambda sender:console.hud_alert('yes, i am a button') def demo(flex): #show the flex mode of b by resizing its parent view b.flex=flex b.title="b.flex='{}'".format(b.flex) duration=0.5 #animation speed def small(): somecontainer.frame=(50,50,200,200) def big(): somecontainer.frame=(50,50,400,400) ui.animate(big,duration=duration) Timer(duration,lambda:ui.animate(small,duration=duration)).start() #have to wait until first animation finishes t=Timer(duration*2,lambda:()) # wait for 2nd animation to complete... t.start() t.join() root.present('sheet') for x in ['','lrtb','l','t','w','h','wh','wl','ht']: demo(x) def spin(): from math import pi b.title=('weeeeeeee') b.transform=b.transform.concat(ui.Transform().rotation(2*pi/3)) somecontainer.transform=somecontainer.transform.concat(ui.Transform().rotation(-pi/3)) for t in range(0,18): ui.delay(lambda :ui.animate(spin,duration=0.1),0.1*t)
-
The only reason I ask is because 'TR' gets on the top left instead of bottom left
-
In the above, change T to TR, these are the same anyway.
Works fine, button stays on the bottom left for the animation with TR.
Not again that flex is only helpful if you have added the subview, then the parent subview changes size. It does not help with initial layout. -
This did not do anything, button still appears on the top left.
-
Using
'TR'
that is. -
Again, works for me. Did you run this example, or something else? Also, I have to ask, are you running this in pythonista in app, or some other configuration, like on the simulator, or on a device?
-
I ran this example inside the pythonista app. The example works fine for me, but putting the flex into my program does not.
-
You mean the example doesn't work in Xcode? Or you can't get flex to work within pythonista for another script? You probably will need to post what you have tried, if you want further help....
-
Within Pythonista for another script, can be found here: https://gist.github.com/tjferry14/32a9745296923c36491e
-
Look back at the example, and the description of flex. Flex is NOT a way to place a button within a view... It is a way to keep it in a position, or relative size. To position a button, use frame, or x and y, to do that.
In general, if you want something to be on the left, you set
x=0
.If you want it on the right, you set
x = (width of parent view) -(desired width of this button)
Vertically, it works the same, to have something aligned to the top of a view
y=0,
bottom would require
y=(height of parent view) - (height of this button)
It can be helpful to draw a cartoon showing relationships of the various x, y, width and height params, which let you easily (maybe a tiny bit of algebra) figure out what the values should be to place the button wherever you want (exercise to the reader: what x and y settings are required to center a button within another view?)
Be careful that, if the root view has not yet been presented, the height and width might return 0. It will get resized when it gets presented.