Forum Archive

Browse between different button images based on the contents of a list

guran

I'm trying to create a logic where I have two buttons in a view. Using two additional buttons, I want to scroll through the background images of the first two buttons based on a list that contains the names of different images. Then I should be able to click on the respective image to open another view showing something relaterade to the image I clicked.

As a rookie I have struggled with this for a while but do not get the logic to work. Is there anyone who can help me with suggestions on how to do?

cvp

@guran if, and only if, I correctly understood, a quick and dirty script

import ui

l = ['emj:Angry','emj:Astonished','emj:Cold_Sweat_1','emj:Cold_Sweat_2']

v = ui.View()
v.background_color = 'white'

b1 = ui.Button(name='b1')
b1.frame = (10,10,32,32)
b1.idx = 0
b1.image = ui.Image.named(l[b1.idx]).with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)
def b1_action(sender):
    iv = ui.ImageView()
    iv.image = b1.image
    w,h = iv.image.size
    iv.frame = (0,0,w,h)
    iv.present('sheet')
    iv.wait_modal()
b1.action = b1_action
v.add_subview(b1)

b_next = ui.ButtonItem()
b_next.title = '➡️'
def b_next_action(sender):
    b = v['b1']
    b.idx += 1
    if b.idx == len(l):
        b.idx = 0
    b.image = ui.Image.named(l[b.idx]).with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)
b_next.action = b_next_action
v.right_button_items = (b_next,)

v.present() 
guran

Thank you for your response...
Using a view as a list and managing the button as an item is new to me ... like so much else unfortunately. I will try to transfer it to my script where I use multiple views that are displayed using ”bring_to_front ()”, so that's the technique I probably need to use in the function ”b1_action” to display the image in a new view.
Still have trouble with understanding how to handle the different views and at the same time reach them from each other so it might be that I will raise new questions:)

cvp

@guran you're welcome, I wrote this little script only to show how to change the image of a button by touching another one.

cvp

@guran please, try this one

import ui

l = {
    'cat':['emj:Angry','emj:Astonished','emj:Cold_Sweat_1','emj:Cold_Sweat_2'], 'face':['emj:Cat_Face','emj:Cat_Face_Crying','emj:Cat_Face_Grinning']}

v = ui.View()
v.background_color = 'white'

iv1 = ui.ImageView(name='cat')
iv1.background_color = 'white'
iv1.frame = (0,50,v.width,v.height-50)
v.add_subview(iv1)

iv2 = ui.ImageView(name='face')
iv2.background_color = 'white'
iv2.frame = (0,50,v.width,v.height-50)
v.add_subview(iv2)

def b_action(sender):
    iv = v[sender.name[1:]]
    iv.image = sender.image
    iv.content_mode = ui.CONTENT_SCALE_ASPECT_FIT
    iv.hidden = False
    iv.bring_to_front()

b1 = ui.Button(name='bcat')
b1.idx = 0
b1.frame = (10,10,32,32)
b1.image = ui.Image.named(l['cat'][b1.idx]). with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)
b1.action = b_action
v.add_subview(b1)

b2 = ui.Button(name='bface')
b2.idx = 0
b2.frame = (50,10,32,32)
b2.image = ui.Image.named(l['face'][b2.idx]). with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)
b2.action = b_action
v.add_subview(b2)

def b_next_action(sender):
    nam = sender.title[5:]
    b = v['b'+nam]
    b.idx += 1
    if b.idx == len(l[nam]):
        b.idx = 0
    b.image = ui.Image.named(l[nam][b.idx]). with_rendering_mode(ui.RENDERING_MODE_ORIGINAL)

b_next1 = ui.ButtonItem()
b_next1.title = 'next cat'
b_next1.action = b_next_action

b_next2 = ui.ButtonItem()
b_next2.title = 'next face'
b_next2.action = b_next_action

v.right_button_items = (b_next2,b_next1)

v.present()