Tkinter __call__

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

    #1

    Tkinter __call__

    from Tkinter import *
    from tkFileDialog import askopenfilename
    from tkColorChooser import askcolor
    from tkMessageBox import askquestion, showerror
    from tkSimpleDialog import askfloat

    demos = {
    'Open': askopenfilename ,
    'Color': askcolor,
    'Query': lambda: askquestion('Wa rning', 'You typed
    "..."\nConfirm? '),
    'Error': lambda: showerror('Erro r!', "He's dead, Jim"),
    'Input': lambda: askfloat('Entry ', 'Enter credit card number')
    }


    class Demo(Frame):
    def __init__(self, parent=None):
    Frame.__init__( self, parent)
    self.pack()
    Label(self, text="Basic demos").pack()
    for (key, value) in demos.items():
    func = (lambda key=key: self.printit(ke y))
    Button(self, text=key, command=func).p ack(side=TOP, fill=BOTH)
    def printit(self, name):
    print name, 'returns =>', demos[name]()


    I have tried but cant get it to work properly.
    I want to instead printit method to put __call__ and call it like that
    Can someone help me, please?
  • John McMonagle

    #2
    Re: Tkinter __call__

    Gigs_ wrote:
    from Tkinter import *
    from tkFileDialog import askopenfilename
    from tkColorChooser import askcolor
    from tkMessageBox import askquestion, showerror
    from tkSimpleDialog import askfloat
    >
    demos = {
    'Open': askopenfilename ,
    'Color': askcolor,
    'Query': lambda: askquestion('Wa rning', 'You typed
    "..."\nConfirm? '),
    'Error': lambda: showerror('Erro r!', "He's dead, Jim"),
    'Input': lambda: askfloat('Entry ', 'Enter credit card number')
    }
    >
    >
    class Demo(Frame):
    def __init__(self, parent=None):
    Frame.__init__( self, parent)
    self.pack()
    Label(self, text="Basic demos").pack()
    for (key, value) in demos.items():
    func = (lambda key=key: self.printit(ke y))
    Button(self, text=key, command=func).p ack(side=TOP, fill=BOTH)
    def printit(self, name):
    print name, 'returns =>', demos[name]()
    >
    >
    I have tried but cant get it to work properly.
    I want to instead printit method to put __call__ and call it like that
    Can someone help me, please?
    Add the following lines to the end of your script and all should work as
    expected:

    root = Tk()
    app = Demo(root)
    root.mainloop()

    Comment

    • Gigs_

      #3
      Re: Tkinter __call__

      John McMonagle wrote:
      Gigs_ wrote:
      >from Tkinter import *
      >from tkFileDialog import askopenfilename
      >from tkColorChooser import askcolor
      >from tkMessageBox import askquestion, showerror
      >from tkSimpleDialog import askfloat
      >>
      >demos = {
      > 'Open': askopenfilename ,
      > 'Color': askcolor,
      > 'Query': lambda: askquestion('Wa rning', 'You typed
      >"..."\nConfirm ?'),
      > 'Error': lambda: showerror('Erro r!', "He's dead, Jim"),
      > 'Input': lambda: askfloat('Entry ', 'Enter credit card number')
      >}
      >>
      >>
      >class Demo(Frame):
      > def __init__(self, parent=None):
      > Frame.__init__( self, parent)
      > self.pack()
      > Label(self, text="Basic demos").pack()
      > for (key, value) in demos.items():
      > func = (lambda: self.printit(ke y))
      > Button(self, text=key, command=func).p ack(side=TOP,
      >fill=BOTH)
      > def printit(self, name):
      > print name, 'returns =>', demos[name]()
      >>
      >>
      >I have tried but cant get it to work properly.
      >I want to instead printit method to put __call__ and call it like that
      >Can someone help me, please?
      >
      Add the following lines to the end of your script and all should work as
      expected:
      >
      root = Tk()
      app = Demo(root)
      root.mainloop()
      >
      I have write root window, but forgot to write here
      I want to use __call__ method instead printit

      Comment

      • Peter Otten

        #4
        Re: Tkinter __call__

        Gigs_ wrote:
        I have tried but cant get it to work properly.
        It does work, but not the way you want it to.
        I want to instead printit method to put __call__ and call it like that
        I don't understand.
        Can someone help me, please?
        Try to explain what you want a bit more explicitly.

        Peter

        Comment

        • James Stroud

          #5
          Re: Tkinter __call__

          Gigs_ wrote:
          from Tkinter import *
          from tkFileDialog import askopenfilename
          from tkColorChooser import askcolor
          from tkMessageBox import askquestion, showerror
          from tkSimpleDialog import askfloat
          >
          demos = {
          'Open': askopenfilename ,
          'Color': askcolor,
          'Query': lambda: askquestion('Wa rning', 'You typed
          "..."\nConfirm? '),
          'Error': lambda: showerror('Erro r!', "He's dead, Jim"),
          'Input': lambda: askfloat('Entry ', 'Enter credit card number')
          }
          >
          >
          class Demo(Frame):
          def __init__(self, parent=None):
          Frame.__init__( self, parent)
          self.pack()
          Label(self, text="Basic demos").pack()
          for (key, value) in demos.items():
          func = (lambda key=key: self.printit(ke y))
          Button(self, text=key, command=func).p ack(side=TOP, fill=BOTH)
          def printit(self, name):
          print name, 'returns =>', demos[name]()
          >
          >
          I have tried but cant get it to work properly.
          I want to instead printit method to put __call__ and call it like that
          Can someone help me, please?
          The code you have should work. My guess is that you don't understand
          lambda and so you want to do it a "different" way, using a "callable"?
          Well, that's what lambda does, it makes a callable object:


          Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
          [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
          Type "help", "copyright" , "credits" or "license" for more information.
          pykey = 1
          pycallable(lamb da key=key: self.printit(ke y))
          True
          py'__call__' in dir(lambda key=key: self.printit(ke y))
          True



          So the code you have already does what you want to do. Maybe understand
          what you are studying before you reinvent the wheel--you will save
          yourself a lot of frustration.

          James

          Comment

          • Gigs_

            #6
            Re: Tkinter __call__

            James Stroud wrote:
            Gigs_ wrote:
            >from Tkinter import *
            >from tkFileDialog import askopenfilename
            >from tkColorChooser import askcolor
            >from tkMessageBox import askquestion, showerror
            >from tkSimpleDialog import askfloat
            >>
            >demos = {
            > 'Open': askopenfilename ,
            > 'Color': askcolor,
            > 'Query': lambda: askquestion('Wa rning', 'You typed
            >"..."\nConfirm ?'),
            > 'Error': lambda: showerror('Erro r!', "He's dead, Jim"),
            > 'Input': lambda: askfloat('Entry ', 'Enter credit card number')
            >}
            >>
            >>
            >class Demo(Frame):
            > def __init__(self, parent=None):
            > Frame.__init__( self, parent)
            > self.pack()
            > Label(self, text="Basic demos").pack()
            > for (key, value) in demos.items():
            > func = (lambda key=key: self.printit(ke y))
            > Button(self, text=key, command=func).p ack(side=TOP,
            >fill=BOTH)
            > def printit(self, name):
            > print name, 'returns =>', demos[name]()
            >>
            >>
            >I have tried but cant get it to work properly.
            >I want to instead printit method to put __call__ and call it like that
            >Can someone help me, please?
            >
            The code you have should work. My guess is that you don't understand
            lambda and so you want to do it a "different" way, using a "callable"?
            Well, that's what lambda does, it makes a callable object:
            >
            >
            Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
            [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
            Type "help", "copyright" , "credits" or "license" for more information.
            pykey = 1
            pycallable(lamb da key=key: self.printit(ke y))
            True
            py'__call__' in dir(lambda key=key: self.printit(ke y))
            True
            >
            >
            >
            So the code you have already does what you want to do. Maybe understand
            what you are studying before you reinvent the wheel--you will save
            yourself a lot of frustration.
            >
            James
            I understand lambda, and I know that code is working. But want to do for
            exercise with __call__. This coed is from programming python 2ed boo

            Comment

            • James Stroud

              #7
              Re: Tkinter __call__

              Gigs_ wrote:
              James Stroud wrote:
              >Gigs_ wrote:
              >> def printit(self, name):
              >> print name, 'returns =>', demos[name]()
              >>>
              >>>
              >>I have tried but cant get it to work properly.
              >>I want to instead printit method to put __call__ and call it like that
              >>Can someone help me, please?
              I understand lambda, and I know that code is working. But want to do for
              exercise with __call__. This coed is from programming python 2ed boo
              You want to use a function factory that itself defines "__call__"? This
              requires creating a class and not a function.


              class printit(object) :
              def __init__(self, name):
              self.name = name
              def __call__(self):
              print self.name, 'returns =>', demos[self.name]()

              class Demo(Frame):
              def __init__(self, parent=None):
              Frame.__init__( self, parent)
              self.pack()
              Label(self, text="Basic demos").pack()
              for (key, value) in demos.items():
              func = printit(key)
              Button(self, text=key, command=func).p ack(side=TOP, fill=BOTH)


              However, the functional way (as with lambda) is the most widely used way
              to do this. Another functional way is with closure:

              def printit(key):
              def _f():
              print key, 'returns =>', demos[key]()
              return _f

              Which behaves identically to the class above. Even more ways to do this
              exist in python, including partial functions--which are also a
              functional approach.

              James

              Comment

              • Gigs_

                #8
                Re: Tkinter __call__

                James Stroud wrote:
                Gigs_ wrote:
                >James Stroud wrote:
                >>Gigs_ wrote:
                >>> def printit(self, name):
                >>> print name, 'returns =>', demos[name]()
                >>>>
                >>>>
                >>>I have tried but cant get it to work properly.
                >>>I want to instead printit method to put __call__ and call it like that
                >>>Can someone help me, please?
                >
                >I understand lambda, and I know that code is working. But want to do
                >for exercise with __call__. This coed is from programming python 2ed boo
                >
                You want to use a function factory that itself defines "__call__"? This
                requires creating a class and not a function.
                >
                >
                class printit(object) :
                def __init__(self, name):
                self.name = name
                def __call__(self):
                print self.name, 'returns =>', demos[self.name]()
                >
                class Demo(Frame):
                def __init__(self, parent=None):
                Frame.__init__( self, parent)
                self.pack()
                Label(self, text="Basic demos").pack()
                for (key, value) in demos.items():
                func = printit(key)
                Button(self, text=key, command=func).p ack(side=TOP, fill=BOTH)
                >
                >
                However, the functional way (as with lambda) is the most widely used way
                to do this. Another functional way is with closure:
                >
                def printit(key):
                def _f():
                print key, 'returns =>', demos[key]()
                return _f
                >
                Which behaves identically to the class above. Even more ways to do this
                exist in python, including partial functions--which are also a
                functional approach.
                >
                James
                thanks man

                Comment

                Working...