【问题标题】:passing variable in TKinter在 TKinter 中传递变量
【发布时间】: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() 时会出现另一个错误。
  • 不胜感激将答案标记为正确的答案

标签: python tkinter tk


【解决方案1】:

当python正在执行代码并到达该行时

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

它检查MIMI 不存在,在执行此行期间并因此出现错误。只要 open_image() 被 python 读取,MI 就没有被定义。您不必将MI 作为参数传递,使其在open_image() 成为全局变量应该可以在blur() 使用。

btnMask = Button(right, text='show', width=19, height=6,
                 command=blur)
.... #same code as yours

def blur():
    I = cv.imread(MI)
    I2 = cv.blur(I,3)
    cv.imshow('dfdf1',I)
    cv.waitKey()

如果您没有传递任何参数,删除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)

所以基本上你的错误与对代码流方向的误解有关。

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

【讨论】:

    【解决方案2】:

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

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

    和:

    def blur():
        ....
    

    没有MI

    【讨论】:

      猜你喜欢
      • 2017-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      相关资源
      最近更新 更多