import tkinter as tk
import tkinter.filedialog
import cv2
import numpy as np
from matplotlib import pyplot as plt
##制作窗口
window = tk.Tk()
window.title('My Window')
window.geometry('500x300')
label = tk.Label(window,text = '图像处理', bg = 'green',font = ('Arial',12),width = 30)
label.pack()
## 解决中文路径函数问题
def cv_imread(file_path):
cv_img = cv2.imdecode(np.fromfile(file_path, dtype = np.uint8),-1)
return cv_img
## 按键功能函数
def selectpath():
file_path = tkinter.filedialog.askopenfilename()
img = cv_imread(file_path)
cv2.namedWindow('image',0)
## cv2.resizeWindow('image',700,600)
cv2.imshow('image',img)
k = cv2.waitKey(0)
cv2.destroyAllWindows()
## 制作按键
button = tk.Button(window,text = '打开图片',font =('Arial',12),width = 10,height = 1,command = selectpath)
button.pack()
window.mainloop()
参考文章: