简体   繁体   中英

Python3 show image in a sequence

Hello I'm need to show a sequnce of images whe the user make click in a button, I wrote the next code but only show me the las image... any idea what is wrong?

#!/usr/bin/python3
import os
import time
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk


class GridWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="NEOTECH")
        self.grid = Gtk.Grid()
        self.add(self.grid)

        self.btnStartTest=Gtk.Button("Iniciar Prueba")
        self.btnStartTest.connect("clicked",self.StartTest)

        self.image = Gtk.Image()
        self.image.set_from_file("logo-NeoTech.png")

        self.grid.add(self.btnStartTest)
        self.grid.attach(self.image,0,1,1,1)


    def StartTest(self,widget):
        self.image.set_from_file("gato3.jpg")
        time.sleep(2)
        self.image.set_from_file("gato4.jpg")
        print("fin")


win = GridWindow()
win.set_position(Gtk.WindowPosition.CENTER)
win.set_default_size(1000,480)
win.set_type_hint(Gdk.WindowTypeHint.MENU)
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

Don't use time.sleep() in a GUI program; it blocks the GUI mainloop and makes it unresponsive.

Use GLib.timeout_add instead:

from gi.repository import Gtk, Gdk, GLib

class GridWindow(Gtk.Window):
    def StartTest(self,widget):
        self.image.set_from_file("gato3.jpg")
        GLib.timeout_add(2000, self.show_last_image)

    def show_last_image(self):
        self.image.set_from_file("gato4.jpg")
        print("fin")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM