its amazing how people are able to come up with wrapper class by using already-built in tools, but they fail to fully comprehend the bigger picture. In this case, MultiListbox is supposed to represent a table with columns
and rows. This class has bunch of functions and routines which are basically useless. However, one of the basic function you would expect in a table is updating each and every cell for each row. I've been beating my head
against the wall trying to implement this routine. Still, for the life of me, I have not been able to get it working. If anyone out there can let me in on a solution for updating its rows real-time, I would appreciate it. Thanks.
There's a screen implementation in the ImageChops module. Something like ImageChops.screen(im, Image.new(im.mode, im.size, color)) might do what you want (where im is the source image, and color is a RGB tuple or specifier).
Is there a way to do the same to just colorize an image (so that 0% in the B/W image becomes 0% and 100% Black become just the color (without black). This would be the same effect as "Screen" in Adobe Photoshop.
would somebody help to get the height of a Font?
i got the width with measure as seen below but lost in the
woods for the height.
helv12 = tkFont.Font ( family="Helvetica", size=12 )
width = helv12.measure(mytext)
would you please help me
thanks
It seems that a closing paren is indistinguishable from "(end)": test("1)") yields (literal 1). Similarly for brackets, braces, and commas. I'm not sure how to improve error detection in the parser.
BTW, using ideas from this article, I've managed to write a regexp parser with postfix operators that wrap their arguments instead of modifying them.
Beware of circular imports!
I just spent 24 hours of my life debugging this so watch out. Circular imports do some very strange things. In my case we have some singelton instances that suddenly stopped being singeltons (i.e. they re-instantiated on every run) all because someone added a circular import. So now you know.
In the "Tkinter Checkbutton widget" page : I needed how do using this widget. So we must ask "variablename".get() for an answer with an 0 or an 1.
.get() is very great.
Thanks for this work. Good continuation to you.
The (64bit) MSI installer for Python 2.6.2 DOES correctly place the information in the registry. The problem is that the installer packages for extensions are 32bit, and their calls for the registry data are virtualized and are redirected to HKLM/Software/Wow6432Node/Python/... instead of HKLM/Software/Python/...
I was able to use the 32bit installer for PIL by duplicating the Python registry key to the HKLM/Software/Wow6432Node key.
I'm using Windows 7 (x64) and Python 2.6.2.
The Python 2.6 installer does add information to the registry, though the other installers can't seem to find it. This tool returns no error, and also succesfully edits the registry, though it doesn't solve my problem.
Perhaps there's a change in how Python (and the windows installers) behave in the new operating system. Anyone have any ideas?
I'd love to hear from you at effbot-public{at}piratesofpacifica{dto}.com
The element type also provides a text attribute, which can be used to hold additional data associated with the element. As the name implies, this attribute is usually used to hold a text string, but it can be used for other, application-specific purposes.
from elementtree.ElementTree import Element
elem = Element("tag")
elem.text = "this element also contains text"
If there is no additional data, this attribute is set to an empty string, or None.
The element type actually provides two attributes that can be used in this way; in addition to text, there’s a similar attribute called tail. It too can contain a text string, an application-specific object, or None. The tail attribute is used to store trailing text nodes when reading mixed-content XML files; text that follows directly after an element are stored in the tail attribute for that element:
<tag><elem>this goes into elem's
text attribute</elem>this goes into
elem's tail attribute</tag>
One tends to correlate the above xml brief with the snippet due to "tag" and "elem" objects. Only by careful observation one can realize that they are unrelated.
Something that is obviously useful is to be able to find the attributes of the font that is currently being used by a widget. It is not obvious from the description here how one does that. The answer, discovered by painful experimentation and reading source code, is:
tkFont.Font(font=widget.cget('font')).actual()
It would be helpful to include that as one of your "patterns".
Very useful, clear and concise. I particularly like the examples provided.
One issue I have with the section on "datetime" is that the "tzinfo" argument is not discussed/presented as promised in the overview at the top of the section.
I am very interested in learning more about it. Can it be added?
class PropDict(dict):
"""Simple dict wrapper that allows element access as an attribute"""
def __getattr__(self, name):
return self[name]
def dictify(elem, collections=[]):
"""Convert from ElementTree.Element to a PropDict hierarchy. (Easily modifyable to return regular dicts.)
If you specify tag names in collections, all sub-elements will be sewn up into the parent collection:
<obj><lics><lic><id>1</id></lic><lic><id>2</id></lic><notlic>blah</notlic></lics></obj> becomes {lics:[{id:1},{id:2},'blah']}
"""
rval = PropDict()
for i in elem.getchildren():
if i.tag in collections:
rval[i.tag] = []
for j in i.getchildren():
rval[i.tag].append(dictify(j, collections))
elif rval.has_key(i.tag):
if not isinstance(rval[i.tag], list):
rval[i.tag] = [rval[i.tag]]
rval[i.tag].append(dictify(i, collections))
else:
children = i.getchildren()
if len(children):
rval[i.tag] = dictify(i, collections)
else:
rval[i.tag] = i.text
if not rval.has_key('text') and elem.text and elem.text.strip():
rval['text'] = elem.text.strip()
return rval
The result is well suited to be printed on a dumb ASCII console or on a web page.
I would agree that having to use this sort of hack is most likely an indicator of bad coding practice, and using this in a web app where most characters are non-ascii would probably be totally insane. But nobody is perfect!
What MultiListbox is this? And why the confrontational style? Maybe the original author didn't have the same use case in mind as you have?
its amazing how people are able to come up with wrapper class by using already-built in tools, but they fail to fully comprehend the bigger picture. In this case, MultiListbox is supposed to represent a table with columns
and rows. This class has bunch of functions and routines which are basically useless. However, one of the basic function you would expect in a table is updating each and every cell for each row. I've been beating my head
against the wall trying to implement this routine. Still, for the life of me, I have not been able to get it working. If anyone out there can let me in on a solution for updating its rows real-time, I would appreciate it. Thanks.
There's a screen implementation in the ImageChops module. Something like ImageChops.screen(im, Image.new(im.mode, im.size, color)) might do what you want (where im is the source image, and color is a RGB tuple or specifier).
Is there a way to do the same to just colorize an image (so that 0% in the B/W image becomes 0% and 100% Black become just the color (without black). This would be the same effect as "Screen" in Adobe Photoshop.
would somebody help to get the height of a Font?
i got the width with measure as seen below but lost in the
woods for the height.
helv12 = tkFont.Font ( family="Helvetica", size=12 )
width = helv12.measure(mytext)
would you please help me
thanks
Had the same problem and was just about to lose hope. Tried this and it worked. You are my hero! Thank you so much! :)
As to Elementree, I am interested in a release that will work with Python 3. What is the status?
Thanks.
The Photoimage class discusses about inserting a jpg image. In that, there is one line that shows as
label.image = image # keep a reference!
This does not work. This line should be changed to
label.image = photo # keep a reference!
Kindly check this and change the article if needed. For me, if I place photo in that line, it displayed the image properly.
Thanks for your work.
A quick googling brought up this page:
http://wiki.forum.nokia.com/index.php/C++_Python_Extensions
Hope this helps!
Hi,
Please is there an implementation for cElement Tree for PYS60
Is there a way of being able to paste text into an entry widget? Yes, Ctrl + V. I should have tried that first.
I can't get the last() predicate to work.
Sample:
from elementtree import ElementTree as ET
text = """<term>
<month id="september"/>
<month id="october"/>
<month id="novemeber"/>
</term>"""
tree= ET.fromstring(text)
print tree.find('month[last()]')
Raises an Invalid Predicate syntax error.
The error class seems to have a bug as when an error is found, the error class only gets four parametesr from the server class
Great article. I am new to Python and this was a nice and simple explanation of how getattr works. Thanks.
It seems that a closing paren is indistinguishable from "(end)": test("1)") yields (literal 1). Similarly for brackets, braces, and commas. I'm not sure how to improve error detection in the parser.
BTW, using ideas from this article, I've managed to write a regexp parser with postfix operators that wrap their arguments instead of modifying them.
Beware of circular imports!
I just spent 24 hours of my life debugging this so watch out. Circular imports do some very strange things. In my case we have some singelton instances that suddenly stopped being singeltons (i.e. they re-instantiated on every run) all because someone added a circular import. So now you know.
In the "Tkinter Checkbutton widget" page : I needed how do using this widget. So we must ask "variablename".get() for an answer with an 0 or an 1.
.get() is very great.
Thanks for this work. Good continuation to you.
I've found the problem.
The (64bit) MSI installer for Python 2.6.2 DOES correctly place the information in the registry. The problem is that the installer packages for extensions are 32bit, and their calls for the registry data are virtualized and are redirected to HKLM/Software/Wow6432Node/Python/... instead of HKLM/Software/Python/...
I was able to use the 32bit installer for PIL by duplicating the Python registry key to the HKLM/Software/Wow6432Node key.
Didn't work for me.
I'm using Windows 7 (x64) and Python 2.6.2.
The Python 2.6 installer does add information to the registry, though the other installers can't seem to find it. This tool returns no error, and also succesfully edits the registry, though it doesn't solve my problem.
Perhaps there's a change in how Python (and the windows installers) behave in the new operating system. Anyone have any ideas?
I'd love to hear from you at effbot-public{at}piratesofpacifica{dto}.com
Confusing Example
The element type also provides a text attribute, which can be used to hold additional data associated with the element. As the name implies, this attribute is usually used to hold a text string, but it can be used for other, application-specific purposes.
from elementtree.ElementTree import Element
elem = Element("tag")
elem.text = "this element also contains text"
If there is no additional data, this attribute is set to an empty string, or None.
The element type actually provides two attributes that can be used in this way; in addition to text, there’s a similar attribute called tail. It too can contain a text string, an application-specific object, or None. The tail attribute is used to store trailing text nodes when reading mixed-content XML files; text that follows directly after an element are stored in the tail attribute for that element:
<tag><elem>this goes into elem's
text attribute</elem>this goes into
elem's tail attribute</tag>
One tends to correlate the above xml brief with the snippet due to "tag" and "elem" objects. Only by careful observation one can realize that they are unrelated.
Something that is obviously useful is to be able to find the attributes of the font that is currently being used by a widget. It is not obvious from the description here how one does that. The answer, discovered by painful experimentation and reading source code, is:
tkFont.Font(font=widget.cget('font')).actual()
It would be helpful to include that as one of your "patterns".
Very useful, clear and concise. I particularly like the examples provided.
One issue I have with the section on "datetime" is that the "tzinfo" argument is not discussed/presented as promised in the overview at the top of the section.
I am very interested in learning more about it. Can it be added?
Thanks!
Convert ElemntTree to a dictionary, huzzah.
Fortunately, it seems this was because of a bug in Python 2.6.1. It works fine again with the new Python 2.6.2. Don't know why, but there it is...
Thanks for the article!
One more thing to add: there is one parameter to the encode() method which makes it priceless for me, especially when working with HTML.
>>> my_unicode_str = u'Chuchumbé - ¡Caramba Niño!'
>>> my_unicode_str.encode('us-ascii', 'xmlcharrefreplace')
'Chuchumbé - ¡Caramba Niño!'
The result is well suited to be printed on a dumb ASCII console or on a web page.
I would agree that having to use this sort of hack is most likely an indicator of bad coding practice, and using this in a web app where most characters are non-ascii would probably be totally insane. But nobody is perfect!