Tkinter 图像应用程序在运行后保持冻结系统
Tkinter image application keeps freezing system after it runs
我正在使用以下代码测试应用程序:
#!/usr/bin/env python3
import os
from tkinter import *
from tkinter import filedialog
from PIL import Image, ImageTk
root = Tk()
root.title("Image Viewer App")
root.withdraw()
location_path = filedialog.askdirectory()
root.resizable(0, 0)
#Load files in directory path
im=[]
def load_images(loc_path):
for path,dirs,filenames in os.walk(loc_path):
for filename in filenames:
im.append(ImageTk.PhotoImage(Image.open(os.path.join(path, filename))))
load_images(location_path)
root.geometry("700x700")
#Display test image with Label
label=Label(root, image=im[0])
label.pack()
root.mainloop()
问题是当我 运行 它时,我的系统会冻结并且 Linux 发行版会崩溃。我不知道我做错了什么,除非我不确定将整个图像存储在列表变量中与仅存储位置本身是否是个好主意。现在,它只是测试打开一张图片的能力 img=[0].
加载图像可能需要时间并导致卡顿。最好在子线程中 运行 load_images()
代替:
import os
import threading
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
root = tk.Tk()
root.geometry("700x700")
root.title("Image Viewer App")
root.resizable(0, 0)
root.withdraw()
#Display test image with Label
label = tk.Label(root)
label.pack()
location_path = filedialog.askdirectory()
root.deiconify() # show the root window
#Load files in directory path
im = []
def load_images(loc_path):
for path, dirs, filenames in os.walk(loc_path):
for filename in filenames:
im.append(ImageTk.PhotoImage(file=os.path.join(path, filename)))
print(f'Total {len(im)} images loaded')
if location_path:
# run load_images() in a child thread
threading.Thread(target=load_images, args=[location_path]).start()
# show first image
def show_first_image():
label.config(image=im[0]) if len(im) > 0 else label.after(50, show_first_image)
show_first_image()
root.mainloop()
请注意,我已将 from tkinter import *
更改为 import tkinter as tk
,因为不建议使用通配符导入。
我正在使用以下代码测试应用程序:
#!/usr/bin/env python3
import os
from tkinter import *
from tkinter import filedialog
from PIL import Image, ImageTk
root = Tk()
root.title("Image Viewer App")
root.withdraw()
location_path = filedialog.askdirectory()
root.resizable(0, 0)
#Load files in directory path
im=[]
def load_images(loc_path):
for path,dirs,filenames in os.walk(loc_path):
for filename in filenames:
im.append(ImageTk.PhotoImage(Image.open(os.path.join(path, filename))))
load_images(location_path)
root.geometry("700x700")
#Display test image with Label
label=Label(root, image=im[0])
label.pack()
root.mainloop()
问题是当我 运行 它时,我的系统会冻结并且 Linux 发行版会崩溃。我不知道我做错了什么,除非我不确定将整个图像存储在列表变量中与仅存储位置本身是否是个好主意。现在,它只是测试打开一张图片的能力 img=[0].
加载图像可能需要时间并导致卡顿。最好在子线程中 运行 load_images()
代替:
import os
import threading
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
root = tk.Tk()
root.geometry("700x700")
root.title("Image Viewer App")
root.resizable(0, 0)
root.withdraw()
#Display test image with Label
label = tk.Label(root)
label.pack()
location_path = filedialog.askdirectory()
root.deiconify() # show the root window
#Load files in directory path
im = []
def load_images(loc_path):
for path, dirs, filenames in os.walk(loc_path):
for filename in filenames:
im.append(ImageTk.PhotoImage(file=os.path.join(path, filename)))
print(f'Total {len(im)} images loaded')
if location_path:
# run load_images() in a child thread
threading.Thread(target=load_images, args=[location_path]).start()
# show first image
def show_first_image():
label.config(image=im[0]) if len(im) > 0 else label.after(50, show_first_image)
show_first_image()
root.mainloop()
请注意,我已将 from tkinter import *
更改为 import tkinter as tk
,因为不建议使用通配符导入。