python - win32api.messagebox gets called on program start -
i have simple python program using familiar win32api message calls. put line in program
mywin['button'].onclick = win32api.messagebox(0, 'hello', 'title')
the problem message box gets displayed program starts. , not displayed when button clicked. ideas doing wrong?
here rest of code:
import gui import win32api gui.window(name='mywin', title=u'gui2py minimal app', resizable=true, height='459px', width='400px', image='', ) gui.button(label=u'click me!', name='button', left='8', top='115', default=true, parent='mywin', ) # reference top level window: mywin = gui.get("mywin") mywin['button'].onclick = win32api.messagebox(0, 'hello', 'title') if name == "main": mywin.show() gui.main_loop()
you assigning .onclick
attribute return value of calling win32api.messagebox
. no different doing:
value = win32api.messagebox(0, 'hello', 'title') mywin['button'].onclick = value
to fix problem, can use lambda function:
mywin['button'].onclick = lambda: win32api.messagebox(0, 'hello', 'title')
the above assigns .onclick
attribute lambda function. when button clicked, lambda called , win32api.messagebox(0, 'hello', 'title')
code executed.
Comments
Post a Comment