Forum Archive

More than 1 button won’t show up

NewbieCoder

I have problem in which i cant make 2 buttons show up on the screen at once with this code

import ui

v = ui.View()
v.frame = (0,0,400,400)
v.name = 'test'

b = ui.Button()
b.title = 'use'
b.background_color = 'white'
b.border_color = 'blue'
b.border_width = 1
b.corner_radius = 5
b.frame = (10,10,100,32)

ba = ui.Button()
b.title = 'buy'
b.background_color = 'white'
b.border_color = 'blue'
b.border_width = 1
b.corner_radius = 5
b.frame = (110,10,100,32)

a = ''

def tap(sender):
a = sender.title
print(a)
b.action = tap
v.add_subview(ba)
v.add_subview(b)
v.present('sheet')

When i open the ui it only shows the button for the variable ba and doesnt show the variable b for the first button i made
Please tell me if i am doing something wrong or how to fix it

NewbieCoder

The a = ‘’ isnt “ it is two of ‘ by the way

JonB

call me crazy, but you create two buttons names b. yet you are trying to add ba to the view...

btw, use the little icon at the top befor pasting code, or else use three backticks

type this ->```
   #your code here
   ``` <---
cvp

@NewbieCoder Before studying classes, use functions (def) to execute same code
Try to understand this code

import ui

v = ui.View()
v.frame = (0,0,400,400)
v.name = 'test'

a = '' # 2 x ' = single quote, not " = double quote
def tap(sender):
    a = sender.title
    v.name = 'last tapped = '+a

def set_attrib_in_button(btn,title):
    btn.title = title
    btn.background_color = 'white'
    btn.border_color = 'blue'
    btn.border_width = 1
    btn.corner_radius = 5
    btn.action = tap

button1 = ui.Button()
button1.frame = (10,10,100,32)
set_attrib_in_button(button1,'use')
v.add_subview(button1)

button2 = ui.Button()
button2.frame = (110,10,100,32)
set_attrib_in_button(button2,'buy')
v.add_subview(button2)

v.present('sheet')
ccc
import ui


def tap(sender):
    print(sender.title)


use_button = ui.Button(
    title='use',
    action=tap,
    background_color='white',
    border_color='blue',
    border_width=1,
    corner_radius=5,
    frame=(10, 10, 100, 32))

buy_button = ui.Button(
    title='buy',
    action=tap,
    background_color='white',
    border_color='blue',
    border_width=1,
    corner_radius=5,
    frame=(110, 10, 100, 32))

view = ui.View(name='test', frame=(0, 0, 400, 400))
view.add_subview(use_button)
view.add_subview(buy_button)
view.present('sheet')

A few rules that I use:
1. Create things in order: tap() first because it is needed by both buttons. I create the buttons next because they are required to complete the view. I create view last because it needs to encapsulate everything else.
2. Avoid single letter variables. Use descriptive names like buy_button instead.
3. Put all attributes into a single call to ui.Button() to simply the logic flow.
4. Avoid creating variables that are only used once on the following line. The only exception that I make to this is when doing so avoids long lines.
5. Run Reformat Code under the wrench menu to cleanup my code before posting.
6. Use that three backticks trick that @JonB suggests.

cvp

@ccc Poor guy, it is not easy to be a newbie 😅 in Python. there are so much ways to do the same

ccc

One more approach just for completeness:

import ui


def tap(sender):
    print(sender.title)


def make_button(title, frame):
    return ui.Button(
        title=title,
        frame=frame,
        action=tap,
        background_color='white',
        border_color='blue',
        border_width=1,
        corner_radius=5)


view = ui.View(name='test', frame=(0, 0, 400, 400))
view.add_subview(make_button('use', (10, 10, 100, 32)))
view.add_subview(make_button('buy', (110, 10, 100, 32)))
view.present('sheet')
NewbieCoder

Holy БЛЯДЬ i see now.

I am such a тупица ТоТ

cvp

@NewbieCoder Thanks to Google translate 👍

NewbieCoder

@cvp What do you mean

ccc

He pasted your text into https://translate.google.com to translate it into something that he could understand.

cvp

Exactly but I use the app

mikael

@NewbieCoder, lots of good answers here, but for completeness sake I would like to claim that there was nothing technically wrong with your original code, beyond the copy-paste error where you created b and ba, but set the attributes of b twice.

cvp

@mikael You're right, it was the first answer of @JonB

mikael

@cvp, I read his answer three times, and could not see it saying the same thing.

cvp

@mikael Ok, not exactly but +- 🙄