使用Tkinter在一个窗口中随机显示图像

乔纳森·萨伯里(Jonathan Saboury)

我正在尝试在每3秒更改一次的单个窗口中随机显示目录中的图像。我也希望它可以跨平台运行,就像我在Windows中开发时一样,但是它将在Linux上运行。

当前,我有此工作代码,只需单击鼠标即可遍历目录的所有图像文件(下面的代码)

import os, sys, Tkinter, Image, ImageTk

def button_click_exit_mainloop (event):
    event.widget.quit()

root = Tkinter.Tk()
root.bind("<Button>", button_click_exit_mainloop)
root.geometry('+%d+%d' % (-5,-5)) #controls where the window is

#gets list of file names in certain directory. In this case, the directory it is in
dirlist = os.listdir('.') 

for f in dirlist:
    try:
        image1 = Image.open(f)
        root.geometry('%dx%d' % (image1.size[0],image1.size[1]))
        tkpi = ImageTk.PhotoImage(image1)
        label_image = Tkinter.Label(root, image=tkpi)
        label_image.place(x=0,y=0,width=image1.size[0],height=image1.size[1])
        root.mainloop() # wait until user clicks the window

    except Exception, e:
        pass

但是,这样做的方式是当鼠标单击窗口时,它会调用一个函数来关闭小部件。

我遇到的问题是如何调用此函数,或在没有事件的情况下关闭小部件有什么建议?

这就是我目前所拥有的。这显然行不通,因为它卡在了root.mainloop()中,但是它显示了我通常的想法(下面的代码)

import os, sys, Tkinter, Image, ImageTk, random

root = Tkinter.Tk()
root.geometry('+%d+%d' % (-5,-5)) #controls where the window is

#gets list of file names in certain directory. In this case, the directory it is in
dirlist = os.listdir('.') #might not be in order, CHECK!!!

while True:
    randInt = random.randint(0, 1)
    image = Image.open(dirlist[randInt])
    root.geometry('%dx%d' % (image.size[0],image.size[1]))
    tkpi = ImageTk.PhotoImage(image)
    label_image = Tkinter.Label(root, image=tkpi)
    label_image.place(x=0,y=0,width=image.size[0],height=image.size[1])
    root.mainloop()
    time.sleep(3)

谢谢!

-乔纳森

编辑:回应布莱恩·奥克利(Bryan Oakley):我尝试了您的建议,这看起来像解决方案。

每3秒调用一次该函数,并创建一个窗口,但是图像未放置在窗口中。

是我无权访问根目录吗?如何获得访问权限?

这是我目前所拥有的:

import os, sys, Tkinter, Image, ImageTk, random

def changeImage():
    #gets list of file names in certain directory. In this case, the directory it is in
    dirlist = os.listdir('.') #might not be in order, CHECK!!!

    #get random image
    randInt = random.randint(0, 1)
    image = Image.open(dirlist[randInt])

    #set size to show, in this case the whole picture
    root.geometry('%dx%d' % (image.size[0],image.size[1]))

    #Creates a Tkinter compatible photo image
    tkpi = ImageTk.PhotoImage(image)

    #Put image in a label and place it
    label_image = Tkinter.Label(root, image=tkpi)
    label_image.place(x=0,y=0,width=image.size[0],height=image.size[1])

    # call this function again in three seconds
    root.after(3000, changeImage)


root = Tkinter.Tk()
root.geometry('+%d+%d' % (-5,-5)) #controls where the window is

changeImage()

root.mainloop()

谢谢!!

解决方案:我没有更改代码,因此标签仅创建一次,因此每次调用都会创建标签。我之所以没有这样做,是因为它可以应用于许多其他变量(例如,dirlist = os.listdir('。')),但是会使代码更难阅读。除了可能使用更多的周期外,我没有看到任何其他缺点?我没有看到内存随时间增加,这对我来说很重要。

这是代码,谢谢布莱恩·奥克利(Bryan Oakley)对我的帮助!

import os, Tkinter, Image, ImageTk, random

def changeImage():
    global tkpi #need global so that the image does not get derefrenced out of function

    #gets list of file names in certain directory. In this case, the directory it is in
    dirlist = os.listdir('.')

    #get random image
    randInt = random.randint(0, 1)
    image = Image.open(dirlist[randInt])

    #set size to show, in this case the whole picture
    root.geometry('%dx%d' % (image.size[0],image.size[1]))

    #Creates a Tkinter compatible photo image
    tkpi = ImageTk.PhotoImage(image)

    #Put image in a label and place it
    label_image = Tkinter.Label(root, image=tkpi)
    label_image.place(x=0,y=0,width=image.size[0],height=image.size[1])

    # call this function again in 1/2 a second
    root.after(500, changeImage)

tkpi = None #create this global variable so that the image is not derefrenced

root = Tkinter.Tk()
root.geometry('+%d+%d' % (-5,-5)) #controls where the window is
changeImage()
root.mainloop()
布莱恩·奥克利(Bryan Oakley)

您需要删除无限循环-tkinter已经内置了一个。而是使用after定期调用函数:

def changeImage():
    <do whatever you want, such as swapping out images>

    # call this function again in three seconds
    root.after(3000, changeImage)

然后,在您的主程序中,您需要先调用函数,然后再调用mainloop

root = Tkinter.Tk()
...
changeImage()
root.mainloop()

另外,您不需要changeImage每次都创建一个新的标签小部件,只需在函数外部创建标签即可,每次调用时只需更改图像即可。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

仅在一个窗口中显示更新的图像

在一个窗口中显示基于输出的图像 (Python)

如何使用OpenCV在一个窗口中显示一幅图像和一个视频

cv2-在同一窗口中显示图像,一个接一个

如何使tkinter框显示在另一个tkinter窗口中?

python-tkinter:在另一个窗口中打开另一个图像?

如何使用PhotoImage在特定的tkinter窗口中显示图像?

fancybox在同一窗口中显示两个图像

使用类将标签添加到 tkinter 的另一个窗口中

如何在C ++和QT的另一个窗口中显示主窗口的结果图像?

在另一个窗口中显示QProcess输出

Swift:从另一个弹出窗口中显示一个弹出窗口

使用另一个窗口在一个窗口中填充文本框

使用库在弹出窗口中显示图像

Python Tkinter。仅显示一个窗口副本

使用全屏显示我的联系人的视频并在一个小窗口中恢复我的视频的Skype视图

仅在一个窗口中绘图

使用Tkinter窗口运行另一个显示摄像机预览的python文件

在具有重力形式的 Magnific 弹出窗口中的同一个弹出窗口中显示“谢谢消息”

如何在一个 tkinter 窗口中组合两个子图?

无法在tkinter python中的另一个窗口中访问列表

在tkinter中,如何访问在另一个窗口中创建的变量?

如何将此 tkinter 标签和字符串组合到一个窗口中?

单击使用Javascript的选择按钮时,在另一个窗口上显示图像

在一个窗口中并排显示两个全屏视频背景

如何从一个类到另一个Tkinter窗口中获取变量

在另一个窗口中打开Google文档的图像

如何使用第二个Tkinter窗口在第一个中更改图像?

Flink流:在一个窗口中,在另一个窗口中查找状态