Using bound variables in Tkinter events

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Derek Fountain

    Using bound variables in Tkinter events

    I'm coming to Tkinter from Tcl/Tk. In Tcl I can get a variable in an event
    using the %<X> substitution mechanism. For example, I can set up a command
    like:

    entry .e -validate 1 -vcmd "checkkey %d"

    knowing that the '%d' will be replaced by something useful - whether the
    entry widget has recieved an insert or deletion in this case. The checkkey
    procedure will recieve "insert", "delete" or whatever as its first
    parameter.

    What is the Tkinter way of getting that %d value?
  • wes weston

    #2
    Re: Using bound variables in Tkinter events

    Derek Fountain wrote:[color=blue]
    > I'm coming to Tkinter from Tcl/Tk. In Tcl I can get a variable in an event
    > using the %<X> substitution mechanism. For example, I can set up a command
    > like:
    >
    > entry .e -validate 1 -vcmd "checkkey %d"
    >
    > knowing that the '%d' will be replaced by something useful - whether the
    > entry widget has recieved an insert or deletion in this case. The checkkey
    > procedure will recieve "insert", "delete" or whatever as its first
    > parameter.
    >
    > What is the Tkinter way of getting that %d value?[/color]

    Derek,
    f = Entry()
    ....
    v = StringVar()
    v.set("test")
    f["textvariab le"] = v

    print v.get()

    Comment

    • Peter Otten

      #3
      Re: Using bound variables in Tkinter events

      Derek Fountain wrote:
      [color=blue]
      > I'm coming to Tkinter from Tcl/Tk. In Tcl I can get a variable in an event
      > using the %<X> substitution mechanism. For example, I can set up a command
      > like:
      >
      > entry .e -validate 1 -vcmd "checkkey %d"
      >
      > knowing that the '%d' will be replaced by something useful - whether the
      > entry widget has recieved an insert or deletion in this case. The checkkey
      > procedure will recieve "insert", "delete" or whatever as its first
      > parameter.
      >
      > What is the Tkinter way of getting that %d value?[/color]

      import Tkinter as tk
      root = tk.Tk()

      def check(action):
      print {"1":"inserting ", "0": "deleting"}.get (action, "should never
      happen")
      return True
      checkId = root.register(c heck)

      entry = tk.Entry(root, validate="key", validatecommand =checkId + " %d")
      entry.pack()
      root.mainloop()

      The above was found by trial and error, so no warranties :-)
      You could use check() directly, like

      entry = tk.Entry(root, validate="key", validatecommand =check)

      but then it will be called without parameters.

      Peter


      Comment

      • John Roth

        #4
        Re: Using bound variables in Tkinter events


        "Derek Fountain" <devnull@exampl e.com> wrote in message
        news:4067a227$0 $16577$5a62ac22 @freenews.iinet .net.au...[color=blue]
        > I'm coming to Tkinter from Tcl/Tk. In Tcl I can get a variable in an event
        > using the %<X> substitution mechanism. For example, I can set up a command
        > like:
        >
        > entry .e -validate 1 -vcmd "checkkey %d"
        >
        > knowing that the '%d' will be replaced by something useful - whether the
        > entry widget has recieved an insert or deletion in this case. The checkkey
        > procedure will recieve "insert", "delete" or whatever as its first
        > parameter.
        >
        > What is the Tkinter way of getting that %d value?[/color]

        It's in the event object that's passed as the (only) parameter
        to your event handler. Everything you ever wanted to know
        about the event is an attribute.

        There are a number of very good references to Tkinter on
        the first page of the Python Library Reference section on
        Tkinter. (That's section 16 in the Python 2.3 reference)

        John Roth


        Comment

        • Derek Fountain

          #5
          Re: Using bound variables in Tkinter events

          wes weston wrote:
          [color=blue]
          > Derek Fountain wrote:[color=green]
          >> I'm coming to Tkinter from Tcl/Tk. In Tcl I can get a variable in an
          >> event using the %<X> substitution mechanism. For example, I can set up a
          >> command like:
          >>
          >> entry .e -validate 1 -vcmd "checkkey %d"
          >>
          >> knowing that the '%d' will be replaced by something useful - whether the
          >> entry widget has recieved an insert or deletion in this case. The
          >> checkkey procedure will recieve "insert", "delete" or whatever as its
          >> first parameter.
          >>
          >> What is the Tkinter way of getting that %d value?[/color]
          >
          > Derek,
          > f = Entry()
          > ...
          > v = StringVar()
          > v.set("test")
          > f["textvariab le"] = v
          >
          > print v.get()[/color]

          Thanks, but that's not what I was after! I want to know the type of action
          I've received - the %d binding for the validatecommand .

          Comment

          • Derek Fountain

            #6
            Re: Using bound variables in Tkinter events

            >> I'm coming to Tkinter from Tcl/Tk. In Tcl I can get a variable in an[color=blue][color=green]
            >> event using the %<X> substitution mechanism. For example, I can set up a
            >> command like:
            >>
            >> entry .e -validate 1 -vcmd "checkkey %d"
            >>
            >> knowing that the '%d' will be replaced by something useful - whether the
            >> entry widget has recieved an insert or deletion in this case. The
            >> checkkey procedure will recieve "insert", "delete" or whatever as its
            >> first parameter.
            >>
            >> What is the Tkinter way of getting that %d value?[/color]
            >
            > It's in the event object that's passed as the (only) parameter
            > to your event handler. Everything you ever wanted to know
            > about the event is an attribute.[/color]

            The validate command doesn't get an event.
            [color=blue]
            > There are a number of very good references to Tkinter on
            > the first page of the Python Library Reference section on
            > Tkinter. (That's section 16 in the Python 2.3 reference)[/color]

            Indeed, but I couldn't find anything which answered this question.

            Comment

            • Derek Fountain

              #7
              Re: Using bound variables in Tkinter events

              > import Tkinter as tk[color=blue]
              > root = tk.Tk()
              >
              > def check(action):
              > print {"1":"inserting ", "0": "deleting"}.get (action, "should never
              > happen")
              > return True
              > checkId = root.register(c heck)
              >
              > entry = tk.Entry(root, validate="key", validatecommand =checkId + " %d")
              > entry.pack()
              > root.mainloop()
              >
              > The above was found by trial and error, so no warranties :-)[/color]

              Awesome, thank you. I don't understand what's going on though. Can you
              explain a bit? I don't understand the mechanism which is expanding the %d.
              Is there some magic in the tk.Entry constructor which does it, or is that
              root.register() doing something clever?

              [color=blue]
              > You could use check() directly, like
              >
              > entry = tk.Entry(root, validate="key", validatecommand =check)
              >
              > but then it will be called without parameters.[/color]

              That's as far as I'd got and I was beginning to think it was impossible to
              get the parameters. At least I now know it can be done, even if I don't
              understand how!

              Comment

              • Peter Otten

                #8
                Re: Using bound variables in Tkinter events

                Derek Fountain wrote:
                [color=blue][color=green]
                >> import Tkinter as tk
                >> root = tk.Tk()
                >>
                >> def check(action):
                >> print {"1":"inserting ", "0": "deleting"}.get (action, "should never
                >> happen")
                >> return True
                >> checkId = root.register(c heck)
                >>
                >> entry = tk.Entry(root, validate="key", validatecommand =checkId + " %d")
                >> entry.pack()
                >> root.mainloop()
                >>
                >> The above was found by trial and error, so no warranties :-)[/color]
                >
                > Awesome, thank you. I don't understand what's going on though. Can you
                > explain a bit? I don't understand the mechanism which is expanding the %d.
                > Is there some magic in the tk.Entry constructor which does it, or is that
                > root.register() doing something clever?[/color]

                register() wraps a python function into a tcl function and returns the new
                tcl function's name (can they start with a number?). If you provide a
                string instead of a python function reference as a callback for (e. g.)
                validatecommand , it is passed unaltered to tcl.
                That's all there is to it - at least so my theory goes.

                Peter

                Comment

                • Cameron Laird

                  #9
                  Re: Using bound variables in Tkinter events

                  In article <c4gf64$3df$04$ 1@news.t-online.com>,
                  Peter Otten <__peter__@web. de> wrote:

                  Comment

                  Working...