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.
Twenty buttons, one function
-
I’m building a program that shows the heads of twenty articles in twenty buttons. After pressing a button a window containing the corresponding text of the article is to be shown.
For each button a function is needed. Do I need to make twenty functions or is it possible to use one? I like the idea of the latter, but I have no clue how to find out which button was pressed. The function call accepts no parameters.
How do I know which button?
-
@Peter-S try this, sender is the button. You can use its title to identify it or you can set and use an user attribute, like b.my_attribute = ....
import ui v = ui.View() v.background_color = 'white' def b_action(sender): sender.superview['tv'].text = sender.title w,h = ui.get_screen_size() nb = 5 dx = int(w/nb) - 10 for i in range(nb): b = ui.Button() b.frame = (2+(dx+8)*i,10,dx,32) b.border_width = 1 b.title = 'header '+str(i+1) b.action = b_action v.add_subview(b) tv = ui.TextView(name='tv') y = b.y + b.height + 10 tv.frame = (2,y,w-4,h-y) v.add_subview(tv) v.present('fullscreen')
-
@cvp You hit the nail on the head, thanks.
My idea was to put the buttons in the window myself and fill in the title by program code.
Your idea is to put the buttons in by code and that makes them a lot better to control.Thanks again!
-
@cvp When I put the variable i in b.my_attribute = .... as str(i), it returns the number of the button pressed. That number correspondents with the right article in the list of articles which I can show in a separate window. A very good solution, thanks.