参考
代码中的细节方面可以参看:TKinter部件用法总结
代码
from tkinter import *
import os
from tkinter import messagebox, filedialog
import imageio
from PIL import Image, ImageTk
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.place()
self.createWidget()
def createWidget(self):
global photo
photo = None
self.label03 = Label(self, image=photo)
self.label03.grid(column=0, row=0)
# self.label03.place(relx=0.5, rely=0.5, anchor=CENTER)
self.btn01 = Button(self, text='打开', command=self.getfile, bg='white', anchor='s')
self.btn01.grid(column=0, row=1)
# self.btn01.place(relx=0.8, rely=0.5, anchor=CENTER)
def getfile(self):
file_path = filedialog.askopenfilename(title='选择文件', filetypes=[(('JPG', '*.jpg')), ('All Files', '*')])
img = Image.open(file_path)
width, height = img.size
img = img.resize((700, int(700/width*height)))
global photo
photo = ImageTk.PhotoImage(img) # 实际上是把这个变成全局变量
self.label03.configure(image = photo)
self.label03.image = photo
root = Tk()
root.geometry('700x680')
app = Application(master=root)
root.mainloop()
示例
运行示例: