473,326 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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('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?
Feb 15 '07 #1
7 2161
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('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()

Feb 16 '07 #2
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('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
Feb 16 '07 #3
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
Feb 16 '07 #4
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('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.
pykey = 1
pycallable(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
Feb 16 '07 #5
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('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.
pykey = 1
pycallable(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
Feb 16 '07 #6
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).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
Feb 16 '07 #7
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).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
Feb 17 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michael Spencer | last post by:
Using: Python 2.3.3 Tkinter.TclVersion = 8.4000000000000004 Windows XP Calling edit_modified() on a Tkinter.Text object raises TypeError when it gets a Boolean True response. Exception in...
2
by: Stewart Midwinter | last post by:
I would like to link the contents of three OptionMenu lists. When I select an item from the first list (call it continents), the contents of the 2nd list (call it countries) would update. And in...
1
by: midtoad | last post by:
I'm trying to display a GIF image in a label as the central area to a Tkinter GUI. The image does not appear, though a space is made for it. Why is this so? I notice that I can display a GIF...
0
by: Ron Adam | last post by:
I want to be able to easily create reusable shapes in Tkinter and be able to use them in mid level dialogs. So after some experimenting I've managed to get something to work. The following does...
6
by: JyotiC | last post by:
hi, i am making a GUI using Tkinter, I have a button and a checkbutton. i want the button to be enable when checkbutton is on and disble when the checkbutton is off. thanx
4
by: Kevin Walzer | last post by:
I'm trying to manage user preferences in a Tkinter application by initializing some values that can then be configured from a GUI. The values are set up as a dict, like so: self.prefs= {...
5
by: exhuma.twn | last post by:
As many might know, windows allows to copy an image into the clipboard by pressing the "Print Screen" button on the keyboard. Is it possible to paste such an image from the clipboard into a "Text"...
1
by: C Martin | last post by:
What am I doing wrong in this code? The callback doesn't work from the Entry widget. ##start code import Tkinter tk = Tkinter.Tk() var = Tkinter.StringVar() print var._name def cb(name,...
0
by: lee.walczak | last post by:
I actually post a topic relating to my problem here: (http://groups.google.co.uk/group/comp.lang.python/browse_thread/ thread/a073d532c4481bfe?hl=en# ) But I thought it could be useful to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.