Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python. Display gif in ImageView #33

Closed
m00n-err0r opened this issue Nov 8, 2022 · 3 comments
Closed

Python. Display gif in ImageView #33

m00n-err0r opened this issue Nov 8, 2022 · 3 comments

Comments

@m00n-err0r
Copy link

m00n-err0r commented Nov 8, 2022

Hi there! I really love this project, thanks for your work!
Can you suggest, please, how can I display gifs in ImageView and handle events?
First, I tried to open gif like in python bindings example, with io.open, but ImageView displays only first (0) frame. Then I tried to open image with PIL image library and loop over frames. It worked, but I cant handle events, till frame loop is not finished, but also I would like the gif to be shown infinitely. I can do this with while loop, but, again, cant handle events.
Now, I have a code like this:

from io import BytesIO
from time import sleep
from PIL import Image

file = 'some.gif'
gif = Image.open(file)

with tg.Connection() as c:
    a = tg.Activity(c, dialog=True)
    lay = tg.LinearLayout(a)
    e = tg.ImageView(a, lay)
    button = tg.Button(a, 'Text', lay)
    for ev in c.events():
        print (ev.type, ev.value)
        for frame in range(gif.n_frames):
            gif.seek(frame)
            buf = BytesIO()
            gif.save(buf, format='PNG')
            e.setlinearlayoutparams(0)
            e.setheight(gif.height*5, px=True)
            e.setwidth(gif.width*5, px=True)
            e.setimage(buf.getvalue())
            sleep(1.5)

It's looped over all frames only one time, and till loop is not finished, events cant be handled

@tareksander
Copy link
Member

If you are busy doing other things in your program, you can check for events with Connection.checkevent(), or you could make a separate thread to handle the events.

@tareksander
Copy link
Member

You can also look at this, which I used for termux-gui-package to display gifs.

@m00n-err0r
Copy link
Author

After creating the issue, I also began to think about threads. But my mistake was that I started the thread inside the event loop. After your answers, I took it out of the loop and everything worked out. Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants