Tkinter __call__

G

Gigs_

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('Warning', 'You typed
"..."\nConfirm?'),
'Error': lambda: showerror('Error!', "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(key))
Button(self, text=key, command=func).pack(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?
 
J

John McMonagle

Gigs_ said:
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('Warning', 'You typed
"..."\nConfirm?'),
'Error': lambda: showerror('Error!', "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(key))
Button(self, text=key, command=func).pack(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()
 
G

Gigs_

John said:
Gigs_ said:
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('Warning', 'You typed
"..."\nConfirm?'),
'Error': lambda: showerror('Error!', "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(key))
Button(self, text=key, command=func).pack(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
 
J

James Stroud

Gigs_ said:
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('Warning', 'You typed
"..."\nConfirm?'),
'Error': lambda: showerror('Error!', "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(key))
Button(self, text=key, command=func).pack(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.
py> key = 1
py> callable(lambda key=key: self.printit(key))
True
py> '__call__' in dir(lambda key=key: self.printit(key))
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
 
G

Gigs_

James said:
Gigs_ said:
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('Warning', 'You typed
"..."\nConfirm?'),
'Error': lambda: showerror('Error!', "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(key))
Button(self, text=key, command=func).pack(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.
py> key = 1
py> callable(lambda key=key: self.printit(key))
True
py> '__call__' in dir(lambda key=key: self.printit(key))
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
 
J

James Stroud

Gigs_ said:
James said:
Gigs_ said:
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).pack(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
 
G

Gigs_

James said:
Gigs_ said:
James said:
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).pack(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top