N.Perera N.Perera - 2 years ago 130
Python Question

Storing the input from a Text Field in Tkinter

I want to invoke the method

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


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

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

def delImg(m1,n1):

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:



Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download