Re: [Tutor] Tk -- which label clicked

2005-07-15 Thread Michael Lange
On Fri, 15 Jul 2005 10:20:48 +1200 (NZST)
[EMAIL PROTECTED] wrote:


> def clicked(w):
> print 'Widget %s clicked! Text:' % (str(w), w.cget('text'))
> 
> def makeCallback(w):
> def callback(e):
> clicked(w)
> return callback
> 
> # create labels
> for text in ['foo', 'bar', 'baz']:
> lb = Label(master, text=text)
> lb.bind('', makeCallback(lb))
> 

I don't think it will work this way, because you don't catch the event bind() 
passes to the callback
(you also use a variable "e" in makeCallback() that isn't defined anywhere).

You probably better use a lambda in this case:

def callback(text):
print text

for text in ('foo', 'bar', 'baz'):
lb = Label(master, text=text)
lb.pack()
lb.bind('<1>', lambda event, t=text: callback(t))

or use event.widget to determine which label was clicked:

def callback(event):
print event.widget['text']

for text in ('foo', 'bar', 'baz'):
lb = Label(master, text=text)
lb.pack()
lb.bind('<1>', callback)

I hope this helps

Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] HTML links from Tkinter

2005-07-15 Thread Michael Lange
On Fri, 15 Jul 2005 10:55:37 +1200 (NZST)
[EMAIL PROTECTED] wrote:


> There's no table widget in standard Tkinter.  Search in the Python Cookbook 
> (on
> ActiveState) for a MultiListbox; it might do what you need.
> 
> -- 

On the Tkinter wiki there are some links to Tkinter table widgets (I have not 
tried any of these
though):



Regards

Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tk -- which label clicked

2005-07-15 Thread jfouhy
Quoting Michael Lange <[EMAIL PROTECTED]>:

> I don't think it will work this way, because you don't catch the event
> bind() passes to the callback
> (you also use a variable "e" in makeCallback() that isn't defined
> anywhere).

That's what the variable 'e' is doing!

Here is some code I just wrote and tested:

>>> def clicked(w):
...  print 'Widget %s clicked! Text: %s' % (str(w), w.cget('text'))
...
>>> def makeCallback(w):
...  def callback(e):
...   clicked(w)
...  return callback
...
>>> from Tkinter import *
>>> tk = Tk()
>>> for text in ['foo', 'bar', 'baz']:
...  lb = Label(tk, text=text)
...  lb.pack()
...  lb.bind('', makeCallback(lb))
...
'11427352callback'
'12096856callback'
'12097016callback'
>>> # Now to click on the labels.
Widget .11444992 clicked! Text: foo
Widget .12097096 clicked! Text: bar
Widget .12097336 clicked! Text: baz
Widget .12097096 clicked! Text: bar
Widget .11444992 clicked! Text: foo

-

Lambdas are (probably) going away in Py3000, so I am trying to get away from
using them (it's hard, though :-) ).  Anyway, makeCallback() defines a function
which takes one argument (that's the event), and then calls clicked() on the
widget argument to makeCallback().  That function is returned and bound to
.

Here's a simpler example of the same thing:

>>> def add(x):
...  def addx(y):
...   return x + y
...  return addx
...
>>> f = add(5)
>>> f(3), f(5), f(9)
(8, 10, 14)

(of course, I could have done the same thing by writing: f = (5).__add__ ...)

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] HTML links from Tkinter

2005-07-15 Thread Alberto Troiano
Thanks for the links
I will try both, the tktable and multilistbox

About the HTML llinks I will have to make some functions using webbrowser. I 
want to show some reports (tktable or multilistbox) but I want she to be 
able to check the same query on the web
So I think that with the GET method I can send paramaters to the page I want 
her to open

Best Regards

Alberto

>From: Michael Lange <[EMAIL PROTECTED]>
>To: tutor@python.org
>Subject: Re: [Tutor] HTML links from Tkinter
>Date: Fri, 15 Jul 2005 10:45:29 +0200
>
>On Fri, 15 Jul 2005 10:55:37 +1200 (NZST)
>[EMAIL PROTECTED] wrote:
>
>
> > There's no table widget in standard Tkinter.  Search in the Python 
>Cookbook (on
> > ActiveState) for a MultiListbox; it might do what you need.
> >
> > --
>
>On the Tkinter wiki there are some links to Tkinter table widgets (I have 
>not tried any of these
>though):
>
>
>
>Regards
>
>Michael
>___
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor


Gaucho


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Catching OLE error

2005-07-15 Thread Bernard Lebel
Thanks Danny.


Bernard


On 7/14/05, Danny Yoo <[EMAIL PROTECTED]> wrote:
> 
> > > So the errors are getting raised before Python even knows there is a
> > > problem, so it cannot catch them in an except block. From my very
> > > limited expoerience of COM programming I'd guess that there is a type
> > > mismatch somewhere either in the data you are passing in or in the
> > > data you are getting back.
> > >
> > > But thats just a guess based on the fact that at lreast 80% of my COM
> > > programming errors have been data type inconsistencies!
> 
> > I'll check it out.
> 
> Hi Bernard,
> 
> There was some previous discussion about this on the main Python newsgroup
> around 2002:
> 
> http://mail.python.org/pipermail/python-list/2002-May/106238.html
> http://mail.python.org/pipermail/python-list/2002-May/106257.html
> 
> I'm not sure if this situation has improved much since then; you may want
> to check with the python-win32 list and see what they say:
> 
> http://mail.python.org/mailman/listinfo/python-win32
> 
> Good luck to you!
> 
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Tk -- which label clicked

2005-07-15 Thread Michael Lange
On Fri, 15 Jul 2005 23:29:22 +1200 (NZST)
[EMAIL PROTECTED] wrote:

> Quoting Michael Lange <[EMAIL PROTECTED]>:
> 
> > I don't think it will work this way, because you don't catch the event
> > bind() passes to the callback
> > (you also use a variable "e" in makeCallback() that isn't defined
> > anywhere).
> 
> That's what the variable 'e' is doing!
> 
> Here is some code I just wrote and tested:
> 
> >>> def clicked(w):
> ...  print 'Widget %s clicked! Text: %s' % (str(w), w.cget('text'))
> ...
> >>> def makeCallback(w):
> ...  def callback(e):
> ...   clicked(w)
> ...  return callback
> ...

Aah, you're right, I guess I got confused a little.
Still I think it's overly complicated in this context, when you can you get the 
same result with:

def makeCallback(event):
print "Widget %s clicked! Text %s" % (str(event.widget), 
event.widget['text'])

Regards

Michael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python Lists

2005-07-15 Thread DaSmith




Hi, I am a new Python Programmer, and am encountering some problems with
lists.

I have created a class which has many "Nested list" attributes. When I
create a second instance of the class, the lists are not empty, and already
contain the same values as were held in the previous instantiation. As a
C/C++ programmer, this majes no semns to me at all. Could someone please
explain to me what I must do to avoid this effect.

Kind Regards,

Daniel Smith. ( [EMAIL PROTECTED] )


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] populating a listbox from a list

2005-07-15 Thread Max Russell
Hello-

I have a list in this format:

puckman   puckmana  puckmanf  puckmanh  pacman   
pacmanf   puckmod   pacmod  
newpuc2   newpuc2b  newpuckx  pacheart  hangly   
hangly2   hangly3   piranhah
crush crush2crush3maketrax  maketrxb 
korosuke  mbrushpaintrlr
pacplus   joymanctrpllrp  eyes  eyes2
mrtnt gorkans   eggor   
jumpshot  jumpshtp  shootbul  piranha   piranhao 
nmousenmouseb   mspacman
mspacmnf  mspacmat  woodpek   woodpeka  mspacmab 
pacgalmspacpls  ponpoko 
ponpokov  lizwizalibaba   dremshpr  vanvan   
vanvank   vanvanb   bwcasino

and I want to use it to populate a TK listbox:

 def create_widgets(self):
""" Create button, text, and entry widgets.
"""
# create instruction label
self.inst_lbl = Label(self, text = "Please
select a game")
self.inst_lbl.grid(row = 0, column = 0,
columnspan = 2, sticky = W)

# create label for password  
self.pw_lbl = Label(self, text = "Click OK
when ready ")
self.pw_lbl.grid(row = 12, column = 0, sticky
= W)

#create the scrollbar for the listbox
self.yScroll = Scrollbar ( self,
orient=VERTICAL )
self.yScroll.grid ( row=1, column=1,
sticky=N+S )

# create listbox to list games
self.lstbx =Listbox(self, height = 20,
yscrollcommand=self.yScroll.set
highlightcolor="yellow")
self.lstbx.grid(row = 1, column = 0,sticky=W)
#lock the vertical scroll to the listbox.
self.yScroll["command"] = self.listbox.yview

# create ok button
self.submit_bttn = Button(self, text = "OK",
command = self.launchmame)
self.submit_bttn.grid(row = 12, column = 1,
sticky = W)

# create quit button
self.submit_bttn = Button(self, text = "Quit",
command = self.quit)
self.submit_bttn.grid(row = 12, column = 2,
sticky = W)

I'm really not sure how to populate the list though- I
can work with lists just as text, but what do I need
to do to read a list into a listbox.

Should I preformat the list I read from to be one
entry per line?

I don't know if I want to pickle my data as i need to
be able to delete defunct list entries from my little
GUI application.

thanks
Max





___ 
Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voicemail 
http://uk.messenger.yahoo.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Lists

2005-07-15 Thread Jonathan Conrad
All of the objects in Python are passed by reference: the address 
and the type of the object.  The immutable types, such as our old 
friend the int, are shared by the function and the application, 
as well.  Assigning to the function variable containing the int 
cannot change the int itself, only the address referenced by the 
variable...



 >>> class ClassName:
... class_variable = ["apples", ["oranges"]]
... def __init__ (self, var = class_variable [:]):
... self.instance_variable = var
...
 >>> oo = ClassName ()
 >>> oo.class_variable.append ("fruit flies")
 >>> oo.instance_variable.append ("pears")
 >>> ov = ClassName ()
 >>> ov.instance_variable.append ("tomato")
 >>> ov.instance_variable [1].append ("mold")
 >>> ow = ClassName ()
 >>> oo.instance_variable
["apples", ["oranges"], "pears"]
 >>> ov.instance_variable
["apples", ["oranges", "mold"], "fruit flies", "tomato"]
 >>> ow.instance_variable
["apples", ["oranges", "mold"], "fruit flies"]
 >>>
 >>> # Here, we have a comparison of the class_variable to the
... # instance_variable and of a shallow_copy to a deep_copy.
...
... # shallow copy
... x = x [:]
 >>> # if x was refering to something else that we do not want
... # changed as we work with it.
 >>>
 >>> def copylist (*o):
... a = []
... for i in o:
... try: a.append (deep_copy (*i))
... except: a.append (i)
... return a
...
 >>> import sys; sys.exit (005)

(For the C/C+ (sic.) types: the dereferentiation operator... *)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Lists

2005-07-15 Thread Danny Yoo


> I have created a class which has many "Nested list" attributes. When I
> create a second instance of the class, the lists are not empty, and
> already contain the same values as were held in the previous
> instantiation. As a C/C++ programmer, this majes no semns to me at all.
> Could someone please explain to me what I must do to avoid this effect.


Hi Daniel,

I'm not positive that this has to do with lists; do you mind showing us an
example of the kind of classes that you've been writing?


As a wild guess, I think you may be doing something like this:

##
class Person:
friends = []
name = None

def __init__(self, name):
self.name = name

def add_friend(self, other):
self.friends.append(other)

def greet_all(self):
for friend in self.friends:
print "hi", friend

def __str__(self):
return self.name
##

This is a Person class that has class-scoped variables 'friends' and
'name', and when we play around with this, we see some funny things going
on:

##
>>> j = Person('john')
>>> p = Person('paul')
>>> r = Person('ringo')
>>> j.add_friend(p); j.add_friend(r)
>>> s = Person('ikari shinji')
>>> j.greet_all()
hi paul
hi ringo
>>> s.greet_all()
hi paul
hi ringo
##

How does 's' have friends?

All instances of Person will share the same 'friends' list here, so it
will seem that all people are in this big band of brothers and sisters.
This is probably nice and communal, but it doesn't reflect the harsh,
dog-eat-dog reality, the separation and angst between people and angst.
The mistake is to have 'friends' be defined in class-scope, rather than in
instance scope.

Let's add that isolation.

##
class Person:
def __init__(self, name):
self.friends = []
self.name = name

def add_friend(self, other):
self.friends.append(other)

def greet_all(self):
for friend in self.friends:
print "hi", friend

def __str__(self):
return self.name
##

Here, each person is __init__()ed in the cold, dark world, alone and
friendless, with no friends, and only a name to distinguish themselves
from their brethren.

##
>>> j = Person('john')
>>> p = Person('paul')
>>> r = Person('ringo')
>>> j.add_friend(p); j.add_friend(r)
>>> s = Person('ikari shinji')
>>> j.greet_all()
hi paul
hi ringo
>>> s.greet_all()
>>>
##

And now Shinji has no friends.  Gosh, this is a depressing example.
*grin*


Since you have some experience in C++, you may find Mark Pilgrim's
"Dive into Python" a useful resource:

http://diveintopython.org/object_oriented_framework/defining_classes.html



I hope this helps clear things up!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Closing BaseHTTPServer...

2005-07-15 Thread lawrence wang
I've been using BaseHTTPServer (and subclassing
BaseHTTPRequestHandler, of course) for a project at work. However, I
can't seem to close my connections completely once I'm done with the
server. I've tried:

server.server_close()
del server

but when I try to use the same port again, it complains that it's
already bound; what's more, the interpreter hangs when I try to exit.
Thanks in advance for any help you can offer!

Lawrence
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [Python-Help] Variable question

2005-07-15 Thread Danny Yoo

[Note: try to avoid crossposting.  The Mailing List Ettiquette FAQ:

http://www.gweep.ca/~edmonds/usenet/ml-etiquette.html

explains why crossposting isn't such a good idea usually.  I'm taking
[EMAIL PROTECTED] out of CC now.]


> I am trying to pass a variable into this ESRI function and it won't let
> me is there a way to do this??  Thanks for your help!

Can you show us what you've tried so far?  Also, can you show us how
Python responds?  Do you get an error message of any sort?

These are things we'll want to know, because otherwise, we can't say
anything concrete and know that we're actually addressing your question.
*grin*



You mentioned ESRI, so I'm assuming you're using ArcGIS.

http://hobu.biz/software/python_guide_esri/

If you think that your question seems less about Python and more about the
integration with ArcGIS, then you may want to try the ESRI forums here:

http://forums.esri.com/forums.asp?c=93&s=1729#1729

where they appear to have a fairly active community of ArcGIS/Python
users.


Good luck to you!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Closing BaseHTTPServer...

2005-07-15 Thread Danny Yoo


> I've been using BaseHTTPServer (and subclassing BaseHTTPRequestHandler,
> of course) for a project at work. However, I can't seem to close my
> connections completely once I'm done with the server. I've tried:
>
> server.server_close()
> del server
>
> but when I try to use the same port again, it complains that it's
> already bound; what's more, the interpreter hangs when I try to exit.
> Thanks in advance for any help you can offer!


Hi Lawrence,

I'm not exactly sure if this is the issue you're running into; if you can
show us code, that'll help.  Are you sure nothing's running as a daemon
afterwards?  You may want to try the unix utility 'telnet' and just make
double-check that the server port is closed.


If everything is truly closed, then it still usually takes a moment
between restarts before the socket is available for use again. If we want
to force the issue, we can use the socket.SO_REUSEADDR attribute.  For a
general idea of what common problem is, see:

http://www.unixguide.net/network/socketfaq/4.5.shtml


Concretely, before binding the server's socket to a port, we may need to
set a few socket parameters to reuse a port that's still in cooldown.  If
we have a socket object that hasn't been bound yet, then we can do
something like this:

##
socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
socket.bind(server_address)
##

We shouldn't have to do this either if we're using BaseHTTPServer, since
the mechanism for calling SO_REUSEADDR already exists in
SocketServer.TCPServer.server_bind.


The thing that bothers me is that this should already be happening for
you, since by default, allow_reuse_address is set to true for subclasses
of HTTPServer.  At least, this appears to be true in Python 2.3, according
to this code snippet in the BaseHTTPServer code:

### BaseHTTPServer.py ###
class HTTPServer(SocketServer.TCPServer):

allow_reuse_address = 1# Seems to make sense in testing environment

def server_bind(self):
"""Override server_bind to store the server name."""
SocketServer.TCPServer.server_bind(self)
host, port = self.socket.getsockname()[:2]
self.server_name = socket.getfqdn(host)
self.server_port = port
##


So I think we need some more information before we nail down what's really
happening.  Show us some error messages, and some code, and we might be
able to get a better idea of the situation.


Good luck!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] populating a listbox from a list

2005-07-15 Thread jfouhy
Quoting Max Russell <[EMAIL PROTECTED]>:

> I have a list in this format:
> 
> puckman puckmana puckmanf puckmanh pacman 
> pacmanf puckmod pacmod 
> newpuc2 newpuc2b newpuckx pacheart hangly 

Hi,

You can use .split() to turn a string like that into a list of the individual
elements.
(when called with no arguments, .split() splits on all whitespace)

>>> s = """foo bar baz
... zif zaf zof
... yip yap"""
>>> s
'foo bar baz\nzif zaf zof\nyip yap'
>>> s.split()
['foo', 'bar', 'baz', 'zif', 'zaf', 'zof', 'yip', 'yap']

> I'm really not sure how to populate the list though- I
> can work with lists just as text, but what do I need
> to do to read a list into a listbox.

To insert into a listbox, you use self.lstbx.insert(position, *entries)

For example:

for elem in s.split():
 self.lstbx.insert(END, elem)

You can also do this:

self.lstbx.insert(END, *s.split())

(if x is a list and foo() a function, then foo(*x) is equivalent to foo(x[0],
x[1], x[2], ..., x[len(x)-1]))

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Lists

2005-07-15 Thread Hugo González Monteverde
Looks like you may be using default values in the constructor. The 
object is created with, say, an empty list as a default argument, but 
this argument is defined only one, when the function is defined.

look:

 >>> def myfunc(mylist = []):
 mylist.append(1)
 print mylist


 >>> myfunc()
[1]
 >>> myfunc()
[1, 1]
 >>> myfunc()
[1, 1, 1]
 >>>

all the calls use the same value, which originally was set to an empty 
list, but later it is no longer redefined.. this happens with all 
mutable types as default arguments.

It is likely this is our problem, check your constructors...

Hugo


[EMAIL PROTECTED] wrote:
> 
> 
> 
> Hi, I am a new Python Programmer, and am encountering some problems with
> lists.
> 
> I have created a class which has many "Nested list" attributes. When I
> create a second instance of the class, the lists are not empty, and already
> contain the same values as were held in the previous instantiation. As a
> C/C++ programmer, this majes no semns to me at all. Could someone please
> explain to me what I must do to avoid this effect.
> 
> Kind Regards,
> 
> Daniel Smith. ( [EMAIL PROTECTED] )
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor