masoud anaraki
masoud anaraki

Reputation: 67

passing variable in TKinter

I'm new in Tkinter and trying to create a " brows button" when you click on, you choose an image, and the path of the image should be saved and then pass the path of the image to another function I try this but I got NameError: name 'MI' is not defined

I want to pass MI(which is the path of the chosen photo ) to the blur() function could you help me?

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()

Upvotes: 1

Views: 126

Answers (2)

Delrius Euphoria
Delrius Euphoria

Reputation: 15088

While python is executing the code and reaches the line

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

It checks for MI but MI doesn't exist, during the execution of this line and hence the error. MI is not defined as long as open_image() is read by python. You dont have to pass MI as an argument, making it global at open_image() should make it available to use at 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()

Also its safe to remove lambda if your not passing in any arguments. So that would make your buttons:

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)

So basically your error is related to misunderstanding with the direction of flow of code.

Not sure if this gonna fix all your errors. Do let me know if this works.

Upvotes: 1

Timur U
Timur U

Reputation: 445

If MI is global, try to use:

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

and:

def blur():
    ....

with out MI

Upvotes: 0

Related Questions