Search code examples
pythonpysimplegui

how to update multiple gifs in PySimpleGUI


i'm not english so sorry for typing problems

I want to show multiple gifs on my window and i don't know how to do it

here's the code: (sorry if code is messy)

import PySimpleGUI as sg

sg.theme('darkgray12')
colors = (sg.theme_background_color(), sg.theme_background_color())


counter = 1
firstTime = True


def post():
    post = [sg.Frame('',[
        [sg.Image('1.gif',size=(600 ,600),key='-post-')],
    ],key=f'-POST-{counter}')]
    return post


layout = [
    [sg.Frame(title='', layout=[
        [sg.pin(sg.Button('LOAD', key='-LOAD-POST-')),sg.Push(),sg.pin(sg.Text(f'#10', key='-POST-COUNTER-TEXT-'))],
        [sg.pin(sg.Column([], size=(700, 600), key='-COL1-', scrollable=True, vertical_scroll_only=True,expand_y=True, expand_x=True,element_justification='c'))],
    ], key='-MAIN-FRAME-')],
]

window = sg.Window('Problem - updating gif animation for all posts', layout, size=(800, 600), grab_anywhere=True,font=('Montserrat', 9, 'bold'), element_justification='c')
while True:
    event, values = window.read(100)
    if event == sg.WIN_CLOSED:
        break

    if firstTime:
        for i in range(10):
            window.extend_layout(window['-COL1-'], [post()])
            window.refresh()
            window['-COL1-'].contents_changed()
            counter += 1
        firstTime = False

    if event == '-LOAD-POST-':
        if not counter >= 40:
            for i in range(10):
                window.extend_layout(window['-COL1-'], [post()])
                window.refresh()
                window['-COL1-'].contents_changed()
                counter += 1
                window['-POST-COUNTER-TEXT-'].update(f'#{counter - 1}')
        else:
            # print('Enough for today!')
            window['-POST-COUNTER-TEXT-'].update(f'#{counter - 1}(MAX)')
    window.refresh()
    window['-post-'].UpdateAnimation('1.gif',10)
    print(event)

window.close()

in start there're 10 post, when LOAD button is clicked, 10 more post will be added

i want all of them to be animated but only the first one is.

can i do this or it's not possible?

and because this is a demo the gif is same

thanks in advance.


Solution

  • It seems you are only updating one post. Should you add the following before window.refresh()?:

    for i in range(1, counter):
        window[f'-post-{i}-'].UpdateAnimation(f'{i}.gif', 10)