Getting value from Tkinter entry widget: variable scope difficulty

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kevin Walzer

    #1

    Getting value from Tkinter entry widget: variable scope difficulty

    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

  • jim-on-linux

    #2
    Re: Getting value from Tkinter entry widget: variable scope difficulty


    Kevin,
    if the present value of searchterm is what you
    want, I suggest you make a class


    class Kgui:
    def __init__(self) : # init self
    self.makeGUI()


    def makeGUI(self):

    ###lots of code to set up toplevel windows

    searchbutton=Bu tton(topframe,
    image=searchgla ss, command=self.do Something)
    searchbutton.im age=searchglass
    searchbutton.pa ck(side=LEFT, expand = NO)

    # and all the resrt of the code


    def doSomething(sel f):

    file = os.popen('syste mcommand %s' %
    searchterm, 'r')

    self.searchterm = searchterm

    print self.searchterm #used inside the class

    Kgui.searchterm = searchterm

    print Kgui.searchterm #used outside the class




    you can also do;
    Kgui.searchterm = self.searchterm = searchterm

    I think this is what you are asking for.

    jim-on-linux

    http://www.inqvista.com








    On Sunday 19 November 2006 21:18, Kevin Walzer
    wrote:
    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
    http://www.codebykevin.com

    Comment

    • Hendrik van Rooyen

      #3
      Re: Getting value from Tkinter entry widget: variable scope difficulty

      "Kevin Walzer" <kw@kevin-walzer.com>
      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()
      this will make a new local variable - if you want to assign to the global one,
      you need to add a global statement before doing this, in this makeGUI function.
      - you can read the global here if you have defined searchterm in an outer scope,
      but the assign will make a new local one.
      >
      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')
      here searchterm seems to be a command line variable.
      look in the help for sys.argv how to access command line variables

      - not that you need to, but its good to know... :-)

      had makeGUI been a class instead of a function, you could have
      written makeGUI.searcht erm here, without mucking around with globals....
      >
      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.
      >
      True
      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.
      global searchterm # - in outer scope

      global searchterm # - in inner scope before assign


      HTH - Hendrik


      Comment

      Working...