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.
Getting button title to process in function
-
I would like to get the button title of my script to decide with math operation do. But I have problem with parameter, data gotten from sender (str, int).
I check an answer given to newbie (I did not remember exactly the user) an adapt it, but is not working properly. Here is the code.
Is a simply math app for my kids. In developing of course.
Thanks
import ui #import console texto1 = '' mains=ui.View(frame=(10,10,10,10), background_color='#008000', name='Math for Fun') mains.present('view') controls1=ui.View(frame=(0,0,410,200), background_color='#e6ec5f', name='controls1') def button_tapped(sender): #if button_tapped == b_top1.title('SUM'): #itexto1 = 'Hello World' texto1 = sender.title #else: #texto1 = str(300) return texto1 b_top1 = ui.Button() b_top1.frame = (20,80,80,50) b_top1.title = 'SUM' b_top1.font = ('<System-Bold>', 20) b_top1.background_color = ('#07b2cc') b_top1.border_width = 2 b_top1.corner_radius = 5 b_top1.action = button_tapped b_top2 = ui.Button() b_top2.frame = (115,80,80,50) b_top2.title = 'SUBS' b_top2.font = ('<System-Bold>', 20) b_top2.background_color = '#ff8909' b_top2.border_width = 2 b_top2.corner_radius = 5 b_top1.action = button_tapped b_top3 = ui.Button() b_top3.frame = (215,80,80,50) b_top3.title = 'MULT' b_top3.font = ('<System-Bold>', 20) b_top3.background_color = '#07de1a' b_top3.border_width = 2 b_top3.corner_radius = 5 b_top1.action = button_tapped b_top4 = ui.Button() b_top4.frame = (310,80,80,50) b_top4.title = 'DIV' b_top4.font = ('<System-Bold>', 20) b_top4.background_color = '#ffd12b' b_top4.border_width = 2 b_top4.corner_radius = 5 b_top1.action = button_tapped lb1 = ui.Label(name = 'Label1', bg_color = 'yellow', frame =(80,20,250,50)) lb1.border_color = 'black' lb1.border_width = 1 lb1.flex = 'RB' lb1.alignment=1 lb1.text = 'VAMOS A JUGAR MATEMATICAS' txt_field1 = ui.TextField() txt_field1.frame = (150,145,100,50) txt_field1.border = (50,20,50,50) txt_field1.bg_color = (.9, .98, 1.0) txt_field1.text = button_tapped() mains.add_subview(controls1) controls1.add_subview(b_top1) controls1.add_subview(b_top2) controls1.add_subview(b_top3) controls1.add_subview(b_top4) controls1.add_subview(lb1) controls1.add_subview(txt_field1)
-
@sendog3c could you please provide a bit more information about your problem?
But I have problem with parameter, data gotten from sender (str, int).
What exactly do you mean?
sender.title
should always be astr
. If you want to convert astr
to anint
, useint(text_here)
. If you want to convert aint
to astr
, usestr(int_here)
.def button_tapped(sender): #if button_tapped == b_top1.title('SUM'): #itexto1 = 'Hello World' texto1 = sender.title #else: #texto1 = str(300) return texto1
You can not return something from a callback. Instead, do the operation directly in the callback.
An example:def button_tapped(sender): a = ... # value for a here b = ... # value for b here action = sender.title if action == "SUM": result = a + b # .. other operations txt_field1.text = str(result)
Also, the following will not work:
txt_field1.text = button_tapped()
If i understand correclty, you want to change the text when a button was pressed. However,
button_tapped()
will be evaluated right in this line, before the UI is presented. Instead, modify the text in the callback as shown above. -
You might be able to streamline your code with something like:
def make_button(title, frame, background_color): return ui.Button(title=title, frame=frame, background_color=background_color, font=('<System-Bold>', 20), border_width=2, corner_radius=5, action=button_tapped)
-
Thank both. Ben to help understand better the logic and ccc to become pro my code.
-
What I want to know what button was tapped in order to use the correct Math operation (+ - * /) function o whatever that I coding
-
You only set the action function of your first button (4 times).
b_top1.action = button_tapped
In this function you need to test the self.title (SUM, DIV, ...) if you want to use it for all four operations.
-
Not the self.title but the sender.title, exactly like in the @bennr01 example at the bottom of his post
-
I suspect you also need to keep a list of pending operations, and then only execute when certain operations happen.
For instance, if you press 1, then 2, then plus, then 3, then *, then 4 then +....
Digits would be appended to the current value. When you detect an operand, you just check order of operations, before deciding whether to operate on the output value-- for instance when you encounter a plus, you would execute everything before the plus. But times, you would be operating on the input number, if that makes sense...