473,800 Members | 2,950 Online
Bytes | Software Development & Data Engineering Community
+ 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_pythonmod s', '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.categoryta ble.insert(END, item)
self.categoryta ble.cellconfigu re("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.categoryta ble.cellconfigu re("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 1763
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_pythonmod s', '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.categoryta ble.insert(END, item)
self.categoryta ble.cellconfigu re("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.categoryta ble.cellconfigu re("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.ke ys():
print item
self.categoryta ble.insert(END, item)
self.categoryta ble.cellconfigu re("end,0",imag e=self.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_pythonmod s', '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.categoryta ble.insert(END, item)
self.categoryta ble.cellconfigu re("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.categoryta ble.cellconfigu re("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.categoryta ble.cellconfigu re("end,0", image=getattr(s elf, 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
2111
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. Each time the user selects one of the broker items I need to display a Image of the broker in the table cell next to it. I have found a number of online js scripts that use the image selector route
1
3287
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 link to the gif file. why? <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
1
6222
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 the text, but never on the same level. Question: how can i get the image and the text on the same level?? Question 2:
16
12399
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 instances of the word "blah" to <img src="MyPicture.jpg" >
6
2386
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, a .doc file when clicked opens in MS-word. In the same way i want to make a .xyz file open in the application i developed when clicked. thanks in advance for any advice.
1
2514
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- John
6
13026
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 is not an ASP.NET related question, but I know this group is knowledgeable and quick with responses. Thanks
1
2135
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 the list so I can use this later when retrieving data from my database. I am now trying to develop a Windows forms app (.Net 2.0) and can find how to do this when using a combobox set to the dropdownlist style (assume this is the control I...
3
2855
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 use images in a listview, and each time I am trying to call an image at index 15 or above I have an error stating that there isn't any object at that index. I was debbuging it and there was more there was 22 images, but only 15 I can load. Is...
0
9690
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9550
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10501
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10273
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10250
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7574
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5603
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.