473,461 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1749
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: | last post by:
I am hoping a mixed ASP- Javascript programmer could help me with this scenario: I have a list box populated with randomized records (NewID()) from a Broker (Salesperson) sql server table. ...
1
by: j erickson | last post by:
with the following xsl and xml file, the display of the gif file with the <image/url> tag works. However, the gif file in the <description> tag using the name attribute "src" won't make the correct...
1
by: Hugo de Payns | last post by:
Happy newyear first of all; I am experimenting with a list and some images; the problem is text and the (LI)image are never outlined, or the image is higher than the text, or the image is below...
16
by: juglesh | last post by:
Hello, I need to look through the text on a page and replace certain words with an image or other word something like: read document find all instances of the word "blah" change all...
6
by: Ashok | last post by:
hi, i want to know how to make a specific type of file open in an application i developed in python when the user clicks on the file.(in windows) for eg. a .txt file when clicked opens in notepad,...
1
by: John Thompson | last post by:
We're sooo close. When we load the page to upload the image, all of the prms go through except the binary image data. Using SQL server with the data type set to "image". Please help! Thanks-...
6
by: Chris Fink | last post by:
Does anyone know it is possible to include a small image(.gif .jpeg) within a <SELECT><option> so that the user would see the option text as well as a little image(icon) in the option? I know this...
1
by: Niclas | last post by:
Hi, in ASP .Net, I can associate a value with and item in a dropdown list, for example when binding to a list of employees, I can store the employeeid as a value associated with each name in in...
3
by: grzegorz.gazda | last post by:
Hi all Is there any limit of images which can be added to a image list. I have a problem with this control. I added an image list in design mode and I am adding images using code. Then I want to...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.