A few weeks ago, I was asking questions like this. But, it is really not the right approach. As I say, a few weeks ago I thought it was.
I am sure you can do what you want. I tried to code it but failed :( But I was very close (I am still a beginner). But basically opening a view file, get a reference to the object you want ie(ui.Button) then create a new ui.Button() then pass both objects to a function that assigns the attributes from you button you loaded from the pyui file to the ui.button you created dynamically. But still a lot of code for not much. In my code example below, if you look at the function my_std_button(), does what you are asking for as far as I can see. There is more code than needed, I wanted to show reuse clearly rather than put one button on the screen. I hope it helps.
#please dont copy my code, i am still a beginner
# a lot smarter people around.
# i am still on the learning journey!
import ui
_padding = 10
_placement = ['tl', 'tc', 'tr', 'c', 'bl', 'bc', 'br']
# the obj_pos code not about the answer. just used to position the objects on the screen without having to reinvent the wheel.
def obj_pos(obj, pos, pad=_padding):
pos = pos.lower()
f = obj.superview.frame
l,t,w,h = f[0],f[1],f[2],f[3]
if 'c' in pos:
#deal with centering first, otherwise will get side effects. eg tc (top,center) would fail
obj.x = (w/2) - (obj.width/2)
obj.y = (h/2) - (obj.height/2)
for c in pos:
if c == 't':
obj.y = t + pad
elif c == 'l':
obj.x = l + pad
elif c == 'b':
obj.y = h - obj.height - pad
elif c == 'r':
obj.x = w - obj.width - pad
def my_std_button(the_title):
#this btn could be what you want to try and copy from the ui. only one way to do it. other ways using a dict and setattr.
btn = ui.Button(title = the_title)
btn.background_color = 'red'
btn.tint_color = 'white'
btn.border_width = 1
btn.corner_radius = 5
btn.width = 64
btn.height = 32
btn.action = my_button_click
return btn
def my_button_click(sender):
# of course you could send the callback function to the my_std_button function if you wanted each button to have a different click function.
print sender.title
# could dispatch here with if/elif on title or name....
if __name__ == '__main__':
v = ui.View()
v.frame = (0,0,540,576)
v.background_color = 'white'
#create as many btns in the list _placement
#just to demonstrate reuse
for i in _placement:
btn = my_std_button(i) # getting a new copy of ui.button here, acting like a template
v.add_subview(btn)
obj_pos(btn, i)
v.present('sheet')