带有 python 图像幻灯片的空白 GUI
我在幻灯片放映期间显示图像时遇到问题。当 update_image 的前四行被注释掉时,将显示第一张图像 (1.png)。但是当它没有注释时,我得到一个空白的 GUI。有人有什么建议吗?
import Tkinter
import Image, ImageTk
class App():
def __init__(self):
self.root = Tkinter.Tk()
image1=Image.open('C:\Users\Jason\Desktop\ScreenShots\\1.png')
self.root.geometry('%dx%d' % (image1.size[0],image1.size[1]))
tkpi=ImageTk.PhotoImage(image1)
label_image=Tkinter.Label(self.root, image=tkpi)
label_image.place(x=0,y=0,width=image1.size[0],height=image1.size[1])
self.update_image()
self.root.mainloop()
def update_image(self):
image1=Image.open('C:\Users\Jason\Desktop\ScreenShots\\2.png')
tkpi=ImageTk.PhotoImage(image1)
label_image=Tkinter.Label(self.root, image=tkpi)
label_image.place(x=0,y=0,width=image1.size[0],height=image1.size[1])
print 'slide'
self.root.after(500, self.update_image)
app=App()
I'm having problems getting images to display during a slideshow. When the first four lines of update_image is commented out, the first image (1.png) is shown. But when its not commented, I get a blank GUI. Anyone have any suggestions?
import Tkinter
import Image, ImageTk
class App():
def __init__(self):
self.root = Tkinter.Tk()
image1=Image.open('C:\Users\Jason\Desktop\ScreenShots\\1.png')
self.root.geometry('%dx%d' % (image1.size[0],image1.size[1]))
tkpi=ImageTk.PhotoImage(image1)
label_image=Tkinter.Label(self.root, image=tkpi)
label_image.place(x=0,y=0,width=image1.size[0],height=image1.size[1])
self.update_image()
self.root.mainloop()
def update_image(self):
image1=Image.open('C:\Users\Jason\Desktop\ScreenShots\\2.png')
tkpi=ImageTk.PhotoImage(image1)
label_image=Tkinter.Label(self.root, image=tkpi)
label_image.place(x=0,y=0,width=image1.size[0],height=image1.size[1])
print 'slide'
self.root.after(500, self.update_image)
app=App()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能发生的情况是您的图像对象正在被垃圾收集,因为它是
update_image
范围的本地对象。尝试将图像对象保存为类的属性(例如:self.tkpi
)顺便说一句 - 如果您正在进行幻灯片放映,一次显示单个图像,则有没有理由每次创建新图像时都创建新标签。创建一个新图像然后将其分配给现有标签图像就足够了。
我还建议您使用
grid
或pack
而不是place
。place
在某些情况下很有用,但通常最好使用其他两个几何管理器。Probably what is happening is that your image object is getting garbage collected since it is local to the scope of
update_image
. Try saving the image object as a property of the class (eg:self.tkpi
)By the way -- if you're doing a slide show where you are showing a single image at a time, there's no reason to create a new label each time you create a new image. It's sufficient to create a new image and then assign it to the existing label image.
I also recommend you use
grid
orpack
rather thanplace
.place
is useful in a few circumstances, but it's generally better to use the other two geometry managers.