Python 在TKinter中传递变量

Python 在TKinter中传递变量,python,tkinter,tk,Python,Tkinter,Tk,我是Tkinter的新手,尝试创建一个“浏览按钮”,当你点击时,你选择一个图像,图像的路径应该被保存,然后将图像的路径传递给另一个函数 我尝试了这个,但是我得到了name错误:没有定义名称“MI” 我想将MI(这是所选照片的路径)传递到blur()函数,您能帮我吗 from tkinter import * from PIL import ImageTk, Image from tkinter import filedialog import cv2 as cv color = '#20536

我是Tkinter的新手,尝试创建一个“浏览按钮”,当你点击时,你选择一个图像,图像的路径应该被保存,然后将图像的路径传递给另一个函数 我尝试了这个,但是我得到了
name错误:没有定义名称“MI”

我想将
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()


当python执行代码并到达行时

btnMask = Button(right, text='show', width=19, height=6,
                 command=lambda: blur(MI))
它检查
MI
,但在执行此行期间,
MI
不存在,因此出现错误<只要python读取
open_image()
,就不会定义code>MI。您不必将
MI
作为参数传递,在
open\u image()
中使其成为全局的,应该可以在
blur()
中使用

如果您没有传入任何参数,则可以安全地删除
lambda
。因此,这将使您的按钮:

btnBrowse = Button(top, width=93, text='select file', font=('Times', 15, 'italic', 'bold'), command=open_image)

btnMask = Button(right, text='show', width=19, height=6,
                 command=blur)
因此,基本上,您的错误与对代码流方向的误解有关


不确定这是否能解决你所有的错误。请务必让我知道这是否有效。

如果
MI
是全局的,请尝试使用:

btnMask = Button(right, text='show', width=19, height=6,
                 command=lambda: blur())
以及:


如果退出
MI

只要您通过
选择文件
按钮在
打开图像()
中选择了一个文件,就不会出现上述错误。但是,在调用
cv.blur()
时,您会遇到另一个错误。请将答案标记为正确答案
btnMask = Button(right, text='show', width=19, height=6,
                 command=lambda: blur())
def blur():
    ....