在二级窗口中,图片不显示,这是为什么
import tkinter as tk
from PIL import Image, ImageTk
# 创建主窗口
root = tk.Tk()
root.geometry("400x400")
def a():
# 创建二级窗口
top = tk.Toplevel(root)
top.geometry("200x200")
# 打开图像文件
image = Image.open("123.jpg")
# 将图像转换为Tk格式
photo = ImageTk.PhotoImage(image)
# 创建标签并将图像放置标签上
label = tk.Label(top, image=photo)
label.pack()
b = tk.Button(root,text='a', command=a)
b.pack()
root.mainloop()
参考链接:
https://blog.csdn.net/m0_50000839/article/details/120078530
缺少一个global photo
直接上代码:
import os
def list_dir(text_list,dir_path):
dir_files = os.listdir(dir_path) # 得到该文件夹下所有的文件
for file in dir_files:
file_path = os.path.join(dir_path, file) # 路径拼接成绝对路径
if os.path.isfile(file_path): # 如果是文件,就打印这个文件路径
if file_path.endswith(".txt"):
text_list.append(file_path)
if os.path.isdir(file_path): # 如果目录,就递归子目录
list_dir(text_list,file_path)
return text_list
if __name__ == '__main__':
all_txt = []
thesaurus_path = r"C:\Users\Lenovo\Desktop\AiShu\AiShuWork\腾讯手机采集41人标注\腾讯采集35人-文本"
text_list = list_dir(all_txt,thesaurus_path)
for text in text_list:
print(text)
运行结果:
C:\Users\Lenovo\Desktop\AiShu\AiShuWork\腾讯手机采集41人标注\腾讯采集35人-文本\TX_35ren_cj_CH_1_1105\SPK0014.txt
C:\Users\Lenovo\Desktop\AiShu\AiShuWork\腾讯手机采集41人标注\腾讯采集35人-文本\TX_35ren_cj_CH_1_1105\SPK0015.txt
C:\Users\Lenovo\Desktop\AiShu\AiShuWork\腾讯手机采集41人标注\腾讯采集35人-文本\TX_35ren_cj_CH_1_1105\SPK0016.txt
C:\Users\Lenovo\Desktop\AiShu\AiShuWork\腾讯手机采集41人标注\腾讯采集35人-文本\TX_35ren_cj_CH_1_1105\SPK0017.txt
C:\Users\Lenovo\Desktop\AiShu\AiShuWork\腾讯手机采集41人标注\腾讯采集35人-文本\TX_35ren_cj_CH_1_1105\SPK0018.txt
C:\Users\Lenovo\Desktop\AiShu\AiShuWork\腾讯手机采集41人标注\腾讯采集35人-文本\TX_35ren_cj_CH_1_1105\SPK0019.txt
C:\Users\Lenovo\Desktop\AiShu\AiShuWork\腾讯手机采集41人标注\腾讯采集35人-文本\TX_35ren_cj_CH_1_1105\SPK0020.txt
对于图片不显示的问题,可以根据以下步骤进行排查和解决:
1.检查图片路径是否正确。可以通过直接打开图片,确认图片路径是否正确。
2.确认代码中是否正确使用了Pillow库进行图片的加载。可以参考参考资料中的代码,尤其是 show_image() 和 init() 中涉及的图片加载部分。
3.如果图片大小超出窗口大小,需要对图片进行缩放。可以使用Pillow库中提供的resize()函数对图片进行缩放,如参考资料中所示。
4.使用try except来捕获异常,以便及时发现和排查图片加载失败的问题。可以参考参考资料中的代码,在show_image()中增加try except语句块。
对于窗口居中显示的需求,可以参考以下步骤:
1.获取屏幕大小,可以使用tkinter库中的winfo_screenwidth()和winfo_screenheight()函数获得屏幕的宽度和高度。
2.设置窗口大小,可以使用canvas来设置窗口的大小,设置canvas的宽和高即可。
3.将窗口放置到屏幕正中央,可以使用geometry()函数和place()函数,其中geometry()函数可以设置窗口的大小和位置,place()函数可以将窗口放置到任意位置中心。
4.打印完整代码,可以使用Python内建的open()函数读取文件并打印文件内容,也可以使用tkinter库中的text框架显示代码。
具体代码如下:
import os
import tkinter as tk
from PIL import ImageTk, Image
class App(tk.Tk):
def __init__(self, images):
super().__init__()
assert tk.TkVersion >= 8.6, "PhotoImage not supported with this version of Tkinter"
self.images = images
self.current_image = 0
# 获取屏幕大小
screen_width = self.winfo_screenwidth()
screen_height = self.winfo_screenheight()
self.width = 800
self.height = 600
# 设置canvas大小
self.canvas = tk.Canvas(self, width=self.width, height=self.height)
self.canvas.pack()
# 将窗口放置到屏幕正中央
x = (screen_width - self.width) // 2
y = (screen_height - self.height) // 2
self.geometry('{}x{}+{}+{}'.format(self.width, self.height, x, y))
self.show_image()
# Prev和Next按钮
prev_button = tk.Button(self, text='Prev', command=self.prev_image)
prev_button.pack(side=tk.LEFT)
next_button = tk.Button(self, text='Next', command=self.next_image)
next_button.pack(side=tk.RIGHT)
#显示代码
with open(__file__, 'r') as f:
code = f.read()
code_text = tk.Text(self, width=30, height=10)
code_text.pack()
code_text.insert(tk.END, code)
def show_image(self):
try:
self.image_obj = Image.open(self.images[self.current_image])
image = self.image_obj.resize((self.width, self.height), Image.LANCZOS)
self.photo = ImageTk.PhotoImage(image)
self.canvas.create_image(0, 0, anchor=tk.NW, image=self.photo)
except Exception as e:
print(f"Error: {e}")
print(f"Image file path: {self.images[self.current_image]}")
def prev_image(self):
self.current_image -= 1
if self.current_image < 0:
self.current_image = len(self.images) - 1
self.canvas.delete('all')
self.show_image()
def next_image(self):
self.current_image += 1
if self.current_image >= len(self.images):
self.current_image = 0
self.canvas.delete('all')
self.show_image()
if __name__ == '__main__':
# 指定目录
directory = "imgs/"
# 获取目录下的所有文件的绝对路径
file_paths = []
for filename in os.listdir(directory):
path = os.path.join(directory, filename)
if any(ext in filename.lower() for ext in ('jpg', 'jpeg', 'png', 'gif')):
file_paths.append(path)
print(path)
app = App(file_paths)
app.mainloop()