Returning a value from a Tk dialog

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gordon Airporte

    #1

    Returning a value from a Tk dialog

    The dialogs in tkColorChooser, tkFileDialog, etc. return useful values
    from their creation somehow, so I can do stuff like this:

    filename = tkFileDialog.as kopenfilename( master=self )

    I would like to make a Yes/No/Cancel dialog that can be used the same
    way (returning 1/0/-1), but I just cannot figure out how to do it. At
    best I've been able to get some kind of instance id from the object. How
    does this work?
  • Ron Adam

    #2
    Re: Returning a value from a Tk dialog



    Gordon Airporte wrote:[color=blue]
    > The dialogs in tkColorChooser, tkFileDialog, etc. return useful values
    > from their creation somehow, so I can do stuff like this:
    >
    > filename = tkFileDialog.as kopenfilename( master=self )
    >
    > I would like to make a Yes/No/Cancel dialog that can be used the same
    > way (returning 1/0/-1), but I just cannot figure out how to do it. At
    > best I've been able to get some kind of instance id from the object. How
    > does this work?[/color]

    All of the dialogs you mention use functions as a caller. And then the
    function is returning the result.


    From tkColorChooser. ..

    def askcolor(color = None, **options):
    "Ask for a color"

    if color:
    options = options.copy()
    options["initialcol or"] = color

    return Chooser(**optio ns).show()


    In this case the Chooser(**optio ns) initiates the dialog, and then the
    show() method is called and it returns the value. The return line above
    is the same as...

    cc = Chooser(**optio ns)
    color = cc.show()
    return color

    The other dialogs work in same way. They are all based on
    tkCommonDialog, so look in tkCommonDialog. py to see exactly what's going on.

    Cheers,
    Ron



    Comment

    • Fredrik Lundh

      #3
      Re: Returning a value from a Tk dialog

      Gordon Airporte wrote:
      [color=blue]
      > The dialogs in tkColorChooser, tkFileDialog, etc. return useful values
      > from their creation somehow, so I can do stuff like this:
      >
      > filename = tkFileDialog.as kopenfilename( master=self )
      >
      > I would like to make a Yes/No/Cancel dialog that can be used the same
      > way (returning 1/0/-1), but I just cannot figure out how to do it. At
      > best I've been able to get some kind of instance id from the object. How
      > does this work?[/color]

      if you want a message box, use the tkMessageBox module. there's
      no ready-made wrapper for yes/no/cancel, but you can call the _show
      directly:

      import tkMessageBox
      result = tkMessageBox._s how(
      "title", "message", tkMessageBox.QU ESTION, tkMessageBox.YE SNOCANCEL
      )

      result will be tkMessageBox.YE S, tkMessageBox.NO , or tkMessageBox.CA NCEL.

      if result == tkMessageBox.YE S:
      return 1
      if result == tkMessageNox.No :
      return 0
      return -1 # assume cancel

      </F>



      Comment

      Working...