Forum Archive

Function call on startup

donnieh

How come the first function info() runs on startup?

import ui


def info():
    vInfo = ui.View()                               
    vInfo.name = 'Info'                 
    vInfo.background_color = 'white'
    vInfo.present()             


def dBW_to_Watt():
    pass 

def Watt_to_dBW():
    pass

def dBm_to_mW():
    pass 

def mW_to_dBm():
    pass

v = ui.View()                               
v.name = 'RF Calc'                  
v.background_color = 'gray' 

sv = ui.ScrollView()
w, h = ui.get_screen_size()
sv.content_size = (w,h*2)
sv.background_color = ('white')
sv.frame = (0,0,w,h)
v.add_subview(sv)

#button specs
bw = w+2
bh = h*0.1
br = 1
bb = .5
bbc = 'gray'

b1 = ui.Button(title='Info')       
b1.font = ('<system>',20)
b1.background_color = 'white'
b1.width = (bw)
b1.height = (bh)
b1.corner_radius = (br)
b1.flex = 'LRTB'        
b1.center = (sv.width * 0.5, sv.height * 0.1)
b1.border_color = bbc
b1.border_width = bb
b1.action = info()      
sv.add_subview(b1)

b2 = ui.Button(title='DBW to Watt')    
b2.font = ('<system>',20)
b2.background_color = 'white'
b2.width = (bw)
b2.height = (bh)
b2.corner_radius = (br)
b2.flex = 'LRTB'    
b2.x = b1.x
b2.y = b1.y + b1.height - 1
b2.border_color = bbc
b2.border_width = bb
b2.action = dBW_to_Watt()
sv.add_subview(b2)

b3 = ui.Button(title='Watt to DBW')    
b3.font = ('<system>',20)
b3.background_color = 'white'
b3.width = (bw)
b3.height = (bh)
b3.corner_radius = (br)
b3.flex = 'LRTB'    
b3.x = b1.x
b3.y = b2.y + b2.height - 1
b3.border_color = bbc
b3.border_width = bb
b3.action = Watt_to_dBW()
sv.add_subview(b3)

b4 = ui.Button(title='dBm to mW')      
b4.font = ('<system>',20)
b4.background_color = 'white'
b4.width = (bw)
b4.height = (bh)
b4.corner_radius = (br)
b4.flex = 'LRTB'    
b4.x = b1.x
b4.y = b3.y + b3.height - 1
b4.border_color = bbc
b4.border_width = bb
b4.action = dBm_to_mW()
sv.add_subview(b4)

b5 = ui.Button(title='mW to dBm')      
b5.font = ('<system>',20)
b5.background_color = 'white'
b5.width = (bw)
b5.height = (bh)
b5.corner_radius = (br)
b5.flex = 'LRTB'    
b5.x = b1.x
b5.y = b4.y + b4.height - 1
b5.border_color = bbc
b5.border_width = bb
b5.action = mW_to_dBm()
sv.add_subview(b5)





v.present(hide_title_bar=True,style='fullscreen' )              
omz

Because of this line:

b1.action = info()

You're calling the function instead of using a reference to assign the action, it should be:

b1.action = info
donnieh

Oh shoot! I've been burned by auto complete! Thank you omz!

ccc

b1.action = info() # call

Should be:

b1.action = info # assignment

donnieh

Thank you. Sorry for such a silly question.

ccc

To simplify your code above, consider making a function:

def make_ui_button(title, action, x, y):
    [ ...  ]

Calling it five times would be easier than repeating the code.

donnieh

Of course yes, thank you. The code started out with one button but grew and became inefficient.

Thanks again.