让用户进入 select 并在 Tkinter 中成像并显示该图像
Getting user to select and image and display that said image in Tkinter
我目前正在开发一个基本的 Tkinter 应用程序,但我似乎无法正常显示图像。我的class低于
class App:
def __init__(self):
self.root = tk.Tk()
self.root.title("Diabetic Retinopathy Detection")
self.image = ""
window_width = 700
window_height = 550
# get the screen dimension
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
# find the center point
center_x = int(screen_width/2 - window_width / 2)
center_y = int(screen_height/2 - window_height / 2)
# set the position of the window to the center of the screen
self.root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
file_upload = ttk.Button(self.root, text="Upload 2D Fundus Image",
command=lambda: self.upload_file())
file_upload.pack()
self.root.resizable(False, False)
# when loading a file in, root.lower()
def run(self):
self.root.mainloop()
def upload_file(self):
file_path = askopenfile(mode='r', filetypes=[('Image Files', '*jpeg')])
if file_path is not None:
pass
self.image = file_path.name
img = ImageTk.PhotoImage(Image.open(file_path.name))
output = tk.Label(self.root, text=file_path.name, image=img)
output.pack()
new_app = App()
new_app.run()
当我没有设置图像时,'output' 标签只显示文本,我不明白为什么图像不显示。我知道图像存在是因为 Image.open(file_path.name).show()
有效。谁能解决这个问题?
我想通了,如果其他人卡住了。只需确保 img 在程序的整个运行时范围内。我所做的是设置 self.image = img
而不是文件名,然后将 self.image
作为参数传递给 tk.Label()
。
我目前正在开发一个基本的 Tkinter 应用程序,但我似乎无法正常显示图像。我的class低于
class App:
def __init__(self):
self.root = tk.Tk()
self.root.title("Diabetic Retinopathy Detection")
self.image = ""
window_width = 700
window_height = 550
# get the screen dimension
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
# find the center point
center_x = int(screen_width/2 - window_width / 2)
center_y = int(screen_height/2 - window_height / 2)
# set the position of the window to the center of the screen
self.root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
file_upload = ttk.Button(self.root, text="Upload 2D Fundus Image",
command=lambda: self.upload_file())
file_upload.pack()
self.root.resizable(False, False)
# when loading a file in, root.lower()
def run(self):
self.root.mainloop()
def upload_file(self):
file_path = askopenfile(mode='r', filetypes=[('Image Files', '*jpeg')])
if file_path is not None:
pass
self.image = file_path.name
img = ImageTk.PhotoImage(Image.open(file_path.name))
output = tk.Label(self.root, text=file_path.name, image=img)
output.pack()
new_app = App()
new_app.run()
当我没有设置图像时,'output' 标签只显示文本,我不明白为什么图像不显示。我知道图像存在是因为 Image.open(file_path.name).show()
有效。谁能解决这个问题?
我想通了,如果其他人卡住了。只需确保 img 在程序的整个运行时范围内。我所做的是设置 self.image = img
而不是文件名,然后将 self.image
作为参数传递给 tk.Label()
。