N.Chandimali
N.Chandimali

Reputation: 799

Storing the input from a Text Field in Tkinter

I want to invoke the method delImg when the button delete is called. I used the following code segment for it. But the method parameters are highlighted as wrong. I used Tkinter.How to correct it ?

import Tkinter
import sys
from Tkinter import *
from tkFileDialog   import askopenfilename
root= Tk()

enText =StringVar()

#root.geometry("400*400+500+300")
root.title("Welcome")


def Hello():
 mtext = enText.get()
 mlabel2 = Label(root,text=mtext).pack()
 print(mtext)
 return mtext

def callback():
 name= askopenfilename()
 print name
 return name

def delImg(m1,n1):
 if(m1!=n1):
    print("Error")

text = Entry(root,textvariable =enText).pack()
mbtn = Button(root,text="Enter",command=callback,fg='red').pack()
mbtn = Button(root,text="Ok",command=Hello,fg='red').pack()
mbtn = Button(root,text="Delete", command= lambda:          

delImg(mtext,name),fg='red').pack()


#print(mtext)
root.mainloop()

Upvotes: 3

Views: 311

Answers (1)

Eilyre
Eilyre

Reputation: 466

The mtext and name variables exist only in the scope of Hello and Callback functions, respectively.

This means that even though you do have an mtext variable, it is inside the Hello function, and you cannot access it from outside the function itself.

There's two ways to get it to global scope — one is to use define these variables as global somewhere in the code (global mtext) or just call the function in the outside scope, and assign the return value to a new variable you'll use with the delImg function.

Upvotes: 3

Related Questions