我正在尝试在每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()
您需要删除无限循环-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] 删除。
我来说两句