I'm trying to construct a simple Tkinter GUI and I'm having trouble with
getting the value of an entry widget and passing it onto a callback
function. But I'm not grokking variable scope correctly.
I have two functions. Here are the relevant excerpts:
def makeGUI():
###lots of code to set up toplevel windows
searchbutton=Bu tton(topframe, image=searchgla ss, command=doSomet hing)
searchbutton.im age=searchglass
searchbutton.pa ck(side=LEFT, expand = NO)
searchterm=Stri ngVar()
entryterm=Entry (topframe, textvariable=se archterm)
entryterm.pack( side=RIGHT)
entrylabel=Labe l(topframe, text="Search:")
entrylabel.pack (side=RIGHT)
def doSomething():
file = os.popen('syste mcommand %s' % searchterm, 'r')
print searchterm
---
What I want to happen is the value of searchterm to be passed on to the
doSomething() call when the searchbutton is pressed. Right now it
appears the value of "searchterm " is local to each function.
In Tcl this would be handled by simply assigning the variable
"searchterm " to the global namespace in each function, i.e. "global
searchterm." I've looked and can't quite figure out how to do this in
Python. Any advice is appreciated.
--
Kevin Walzer
Code by Kevin
getting the value of an entry widget and passing it onto a callback
function. But I'm not grokking variable scope correctly.
I have two functions. Here are the relevant excerpts:
def makeGUI():
###lots of code to set up toplevel windows
searchbutton=Bu tton(topframe, image=searchgla ss, command=doSomet hing)
searchbutton.im age=searchglass
searchbutton.pa ck(side=LEFT, expand = NO)
searchterm=Stri ngVar()
entryterm=Entry (topframe, textvariable=se archterm)
entryterm.pack( side=RIGHT)
entrylabel=Labe l(topframe, text="Search:")
entrylabel.pack (side=RIGHT)
def doSomething():
file = os.popen('syste mcommand %s' % searchterm, 'r')
print searchterm
---
What I want to happen is the value of searchterm to be passed on to the
doSomething() call when the searchbutton is pressed. Right now it
appears the value of "searchterm " is local to each function.
In Tcl this would be handled by simply assigning the variable
"searchterm " to the global namespace in each function, i.e. "global
searchterm." I've looked and can't quite figure out how to do this in
Python. Any advice is appreciated.
--
Kevin Walzer
Code by Kevin
Comment