Python GTK Image, Change Image Constantly gets none 图像多次后
Python GTK Image, Change Image Constantly gets none image after a many times
我在 window 中有一个 Gtk.Image 小部件,它必须在一段时间后更改图像。
它 运行 在下面的函数线程中
def dinChangeBg(self,_Timing):
tm.sleep(int(_Timing))
## Verifica se a janela ainda existe
if not(self.Runing):
thread.exit()
bg = "image/background"+str(self.bgIndex)+".jpg"
if not (os.path.isfile(bg)):
print(bg,"bg inesistente")
self.bgIndex = 1
bg = "image/background"+str(self.bgIndex)+".jpg"
if(os.path.isfile(bg)):
print("new bac", bg)
pbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(bg, self.Window.get_size()[0],self.Window.get_size()[1], preserve_aspect_ratio=False)
self.image.clear()
self.image.set_from_pixbuf(pbuf)
self.bgIndex += 1
self.dinChangeBg(int(_Timing))
线程调用是:
_thread.start_new_thread(dinChangeBg,(60,))
一段时间后图像不再变化,图像消失了,
打印继续显示图像位置(如果存在)。
有人知道错误吗?
您不能从另一个线程调用 GTK+ 函数。您的线程必须告诉 GTK+ 线程 both 创建 pixbuf and 更改图像;你用 GLib.idle_add()
来讲述。或者,如果这是一个足够简单的动画,您可以完全放弃第二个线程,请使用 GLib.timeout_add()
到 运行 超时代码。
我在 window 中有一个 Gtk.Image 小部件,它必须在一段时间后更改图像。 它 运行 在下面的函数线程中
def dinChangeBg(self,_Timing):
tm.sleep(int(_Timing))
## Verifica se a janela ainda existe
if not(self.Runing):
thread.exit()
bg = "image/background"+str(self.bgIndex)+".jpg"
if not (os.path.isfile(bg)):
print(bg,"bg inesistente")
self.bgIndex = 1
bg = "image/background"+str(self.bgIndex)+".jpg"
if(os.path.isfile(bg)):
print("new bac", bg)
pbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(bg, self.Window.get_size()[0],self.Window.get_size()[1], preserve_aspect_ratio=False)
self.image.clear()
self.image.set_from_pixbuf(pbuf)
self.bgIndex += 1
self.dinChangeBg(int(_Timing))
线程调用是:
_thread.start_new_thread(dinChangeBg,(60,))
一段时间后图像不再变化,图像消失了, 打印继续显示图像位置(如果存在)。
有人知道错误吗?
您不能从另一个线程调用 GTK+ 函数。您的线程必须告诉 GTK+ 线程 both 创建 pixbuf and 更改图像;你用 GLib.idle_add()
来讲述。或者,如果这是一个足够简单的动画,您可以完全放弃第二个线程,请使用 GLib.timeout_add()
到 运行 超时代码。