@donnieh. By getting involved here on the forum and trying to help as I have been helped so many times, I learnt something very cool from you today. Maybe a lot of people just know it, but I didn't. It was your method (self, sender = None). I thought that was really crazy. But today while I was coding, I had a small problem about reusing an already defined method in my custom class from a callback function. The defined method didn't take additional args other than self. But if I call that method from a callback it will fail, silently. For the callback to work it requires (self, sender). I guess I could have just passed None to sender when I called it as a direct method (I hadn't thought of that also), but that would be messy and confusing. Best option was to definfe as (self, sender =None). Nice and clean. Yes, I am sure for a lot of people easy. But I was happy to get it.
import ui
class test(ui.View):
def __init__(self):
# create a simple btn in the view
# sets it callback function
self.btn = ui.Button(title = 'Ok')
self.btn.action = self.do_something_stupid
self.add_subview(self.btn)
self.style()
def style(self):
# the view style
self.background_color = 'white'
# the button style
btn = self.btn
btn.border_width = 1
btn.background_color = 'red'
btn.tint_color = 'white'
btn.font = ('<system>', 10)
def layout(self):
btn = self.btn
btn.x = btn.y = 100
btn.width = 200
btn.height = 128
'''
i didnt understand why you were making sender = None on your previous example. but today when i was coding i wanted to call a function in my class from a callback. i was about to write a wrapper when i remembered your syntax. so nice, i would not have thought of it otherwise. if only one function you could say so what, but as the projects get larger is so important.
'''
def do_something_stupid(self, sender= None ):
'''
the stupid action is to increase the size
of the font by 5, each time the button is pressed or is called as a method
but because sender = None, a callback can use it or can be called as a normal method of the class.
of course, not smart to reference sender in this case(unless you test for a sender). just good for calling code with logic that does not care about the sender
'''
self.btn.font = (self.btn.font[0], self.btn.font[1] + 5)
if __name__ == '__main__':
x = test()
x.present('sheet')
for i in range(10):
x.do_something_stupid()
print x