【发布时间】:2020-10-18 14:47:09
【问题描述】:
我是 Tkinter 的新手,当你点击时尝试创建一个“浏览按钮”,你选择一个图像,应该保存图像的路径,然后将图像的路径传递给另一个函数
我试试这个,但我得到了NameError: name 'MI' is not defined
我想将MI(这是所选照片的路径)传递给blur() 函数,你能帮帮我吗?
from tkinter import *
from PIL import ImageTk, Image
from tkinter import filedialog
import cv2 as cv
color = '#20536C'
root = Tk()
root.title('main page')
root.configure(bg=color)
root.geometry('1070x700')
root.resizable(width=False, height=False)
# =================================== Frames ===================================
top = Frame(root, width=1070, height=70, bg='yellow')
top.pack(side=TOP)
# top.grid(row = 0 , column= 1)
left = Frame(root, width=750, height=630, bg='#20536C')
left.pack(side=LEFT)
# left.grid(row = 1 , column= 1)
right = Frame(root, width=320, height=630, bg="red")
right.pack(side=LEFT)
# =================================== Buttons ===================================
btnBrowse = Button(top, width=93, text='select file', font=('Times', 15, 'italic', 'bold')
, command=lambda: open_image())
btnBrowse.pack(side=BOTTOM)
btnMask = Button(right, text='show', width=19, height=6,
command=lambda: blur(MI))
btnMask.pack(side=TOP)
btnMakula = Button(right, text='M', width=19, height=6)
btnMakula.pack(side=TOP)
btnClear = Button(right, text='exit', width=19, height=6, command=root.quit)
btnClear.pack(side=TOP)
# =================================== Text =====================================
textBox = Text(left, width=90, height=10, )
textBox.place(x=20, y=455)
# =================================== Functions ===================================
def open_image():
global mainImage ,MI
''' file dialog way '''
root.filename = filedialog.askopenfilename(initialdir="J://uni//final project//thired phase",
title="select an image",
filetypes=(('jpg files', '*.jpg'), ('all files', '*.*'))
)
MI = root.filename
mainImage = ImageTk.PhotoImage(Image.open(root.filename))
img = Label(left, image=mainImage).place(x=20, y=0)
imageBox = Label(left, text=root.filename, width=65, height=20, bd=3).place(x=20, y=0)
''' actually, it's not opening the file it's bringing the location of the file
then we can open the choosen file via location'''
# label.grid(row=0, column=0, columnspan=2)
return MI
def blur(MI):
I = cv.imread(MI)
I2 = cv.blur(I,3)
cv.imshow('dfdf1',I)
cv.waitKey()
root.mainloop()
【问题讨论】:
-
只要您通过
select file按钮选择了open_image()中的文件,就不会出现上述错误。但是,调用cv.blur()时会出现另一个错误。 -
不胜感激将答案标记为正确的答案