469,282 Members | 2,007 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Associate image name with list item

I'm trying to avoid a *lot* of typing in my Tkinter application by
associating image names with items in a list. Here is my sample list:

self.catlist = [
'all', 'installed', 'base', 'crypto', 'database', 'devel',
'editors', 'games', 'gnome', 'graphics', 'kde', 'languages',
'libs', 'libs_perlmods',
'libs_pythonmods', 'libs_rubymods', 'net', 'sci', 'shells',
'sound', 'text', 'web',
'x11_system', 'x11-wm', 'x11'
]

I've also already created a bunch of images with names that correspond
to the list above, i.e. self.all, self.installed, and so on. Here's the
rest of my code:

for item in self.catlist:
print item
self.categorytable.insert(END, item)
self.categorytable.cellconfigure("end,0", image=self.item)

This yields the following error:

AttributeError: item

because, of course, I don't actually have an image called self.item.

What I'm trying to do is get the value of the variable "item" and plug
it into the image name, so that self.item actually corresponds to
self.installed, self.base, etc. Trying something like

self.categorytable.cellconfigure("end,0", image=self.%s % item)

just yields a syntax error: SyntaxError: invalid syntax

Can anyone point me in the right direction?
--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
Mar 27 '07 #1
2 1611
Kevin Walzer wrote:
I'm trying to avoid a *lot* of typing in my Tkinter application by
associating image names with items in a list. Here is my sample list:

self.catlist = [
'all', 'installed', 'base', 'crypto', 'database', 'devel',
'editors', 'games', 'gnome', 'graphics', 'kde', 'languages',
'libs', 'libs_perlmods',
'libs_pythonmods', 'libs_rubymods', 'net', 'sci', 'shells',
'sound', 'text', 'web',
'x11_system', 'x11-wm', 'x11'
]

I've also already created a bunch of images with names that correspond
to the list above, i.e. self.all, self.installed, and so on. Here's the
rest of my code:

for item in self.catlist:
print item
self.categorytable.insert(END, item)
self.categorytable.cellconfigure("end,0", image=self.item)

This yields the following error:

AttributeError: item

because, of course, I don't actually have an image called self.item.

What I'm trying to do is get the value of the variable "item" and plug
it into the image name, so that self.item actually corresponds to
self.installed, self.base, etc. Trying something like

self.categorytable.cellconfigure("end,0", image=self.%s % item)

just yields a syntax error: SyntaxError: invalid syntax

Can anyone point me in the right direction?


Create a dictionary mapping the name to the image object. For example:

# Create all your images first

# Map a name to the image object
self.catdict = {'all' : self.all, 'installed' : self.installed, .......}

# Loop over the image names and do your stuff
for item in self.catdict.keys():
print item
self.categorytable.insert(END, item)
self.categorytable.cellconfigure("end,0",image=sel f.catDict.get(item))

Regards,

John
Mar 27 '07 #2
Kevin Walzer wrote:
I'm trying to avoid a *lot* of typing in my Tkinter application by
associating image names with items in a list. Here is my sample list:

self.catlist = [
'all', 'installed', 'base', 'crypto', 'database', 'devel',
'editors', 'games', 'gnome', 'graphics', 'kde', 'languages',
'libs', 'libs_perlmods',
'libs_pythonmods', 'libs_rubymods', 'net', 'sci', 'shells',
'sound', 'text', 'web',
'x11_system', 'x11-wm', 'x11'
]

I've also already created a bunch of images with names that correspond
to the list above, i.e. self.all, self.installed, and so on. Here's the
rest of my code:

for item in self.catlist:
print item
self.categorytable.insert(END, item)
self.categorytable.cellconfigure("end,0", image=self.item)

This yields the following error:

AttributeError: item

because, of course, I don't actually have an image called self.item.

What I'm trying to do is get the value of the variable "item" and plug
it into the image name, so that self.item actually corresponds to
self.installed, self.base, etc. Trying something like

self.categorytable.cellconfigure("end,0", image=self.%s % item)

just yields a syntax error: SyntaxError: invalid syntax

Can anyone point me in the right direction?
The way you have framed your question the direct answer is

self.categorytable.cellconfigure("end,0", image=getattr(self, item))

but I think not making the images attributes and using a dictionary is the
superior approach. You avoid name clashes between image names and program
logic, and you can easily deal with image names that are not valid
attribute names (like 'x11-wm').

Peter

Mar 27 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Hugo de Payns | last post: by
6 posts views Thread by Ashok | last post: by
1 post views Thread by John Thompson | last post: by
6 posts views Thread by Chris Fink | last post: by
3 posts views Thread by grzegorz.gazda | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.