473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Function to create Tkinter PhotoImages from directory?

I am trying to create a number of Tk PhotoImages from a single
directory. Currently I am hard-coding file names and image names, like so:

def makeImages(self):
self.imagedir = (os.getcwd() + '/images/')
self.folder_new=PhotoImage(file=self.imagedir + 'folder_new.gif')
self.save=PhotoImage(file = self.imagedir + 'save.gif')
self.folder=PhotoImage(file=self.imagedir + 'folder.gif')
self.help=PhotoImage(file=self.imagedir + 'help.gif')
self.fontsmall=PhotoImage(file=self.imagedir + 'fontsmall.gif')
self.stop=PhotoImage(file=self.imagedir + 'stop.gif')

What I'd like to do is glob the directory and create images with the
same name as the gif file, but I can't figure out how to get that to
work. Can anyone help?

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
Jan 29 '07 #1
2 2286
Kevin Walzer wrote:
I am trying to create a number of Tk PhotoImages from a single
directory. Currently I am hard-coding file names and image names, like so:

def makeImages(self):
self.imagedir = (os.getcwd() + '/images/')
self.folder_new=PhotoImage(file=self.imagedir + 'folder_new.gif')
self.save=PhotoImage(file = self.imagedir + 'save.gif')
self.folder=PhotoImage(file=self.imagedir + 'folder.gif')
self.help=PhotoImage(file=self.imagedir + 'help.gif')
self.fontsmall=PhotoImage(file=self.imagedir + 'fontsmall.gif')
self.stop=PhotoImage(file=self.imagedir + 'stop.gif')

What I'd like to do is glob the directory and create images with the
same name as the gif file, but I can't figure out how to get that to
work. Can anyone help?
This is platform independent (and untested):

import glob
import os

def makeImages(self):
import glob
import os
self.imagedir = os.path.join(os.getcwd(), 'images')
pattern = os.path.join(self.imagedir, '*.gif')
image_names = glob.glob(pattern)
for name in image_names:
base = os.path.basename(name).split('.')[0]
pathname = os.path.join(self.imagedir, n)
self.setattr(base, Photoimage(file=pathname))

James
Jan 29 '07 #2
Kevin Walzer wrote:
I am trying to create a number of Tk PhotoImages from a single
directory. Currently I am hard-coding file names and image names, like so:

def makeImages(self):
self.imagedir = (os.getcwd() + '/images/')
self.folder_new=PhotoImage(file=self.imagedir + 'folder_new.gif')
self.save=PhotoImage(file = self.imagedir + 'save.gif')
self.folder=PhotoImage(file=self.imagedir + 'folder.gif')
self.help=PhotoImage(file=self.imagedir + 'help.gif')
self.fontsmall=PhotoImage(file=self.imagedir + 'fontsmall.gif')
self.stop=PhotoImage(file=self.imagedir + 'stop.gif')

What I'd like to do is glob the directory and create images with the
same name as the gif file, but I can't figure out how to get that to
work. Can anyone help?
Should be (wrote setattr to fast):
def makeImages(self):
import glob
import os
self.imagedir = os.path.join(os.getcwd(), 'images')
pattern = os.path.join(self.imagedir, '*.gif')
image_names = glob.glob(pattern)
for name in image_names:
base = os.path.basename(name).split('.')[0]
pathname = os.path.join(self.imagedir, n)
setattr(self, base, Photoimage(file=pathname))
Jan 29 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rob Andrews | last post by:
I'm on a Red Hat 9 system, which has Python 2.2.2 installed, and I installed 2.3 separately into /home/rob/Python-2.3/ (creating the symbolic link "py23" to point to my 2.3 installation). Now I'm...
0
by: Dean N. Williams | last post by:
Dear Python Help, I am trying to install Python with Tkinter on my Mac OS X PowerBook G4 platform, but having some difficulties apparently with missing directories and files. I can build Python...
2
by: Anthony Baxter | last post by:
I'm trying to use py2exe and Inno Setup to build an installer for shtoom, which uses tkinter. If I take the py2exe generated directory, and run the executable from there, it works fine. If I...
8
by: Mark English | last post by:
I'd like to write a Tkinter app which, given a class, pops up a window(s) with fields for each "attribute" of that class. The user could enter values for the attributes and on closing the window...
2
by: Stewart Midwinter | last post by:
this has me puzzled; I've created a small test app to show the problem I'm having. I want to use subprocess to execute system commands from inside a Tkinter app running under Cygwin. When I...
2
by: ishtar2020 | last post by:
Hi everybody I'd appreciate some help on creating a tear off menu with TkInter. I've been reading some documentation but still no luck. Please don't get confused: when I mean "tear off" menu I...
1
by: apriebe47 | last post by:
Hello, I have created a simple canvas in Tkinter to display a number of PhotoImages, I then bind a key (in my case, <Up>) to start a loop that plays through a list of PhotoImages to make it an...
2
by: sumanthsclsdc | last post by:
Hello friends, I have a problem, I implemented a class which uses tkinter and displays the window as required, the class will create a window with listbox and inserts some items into it, I...
7
by: Protected | last post by:
Hello. I'm a complete newbie trying to learn Python. I decided to try some Tkinter examples, including the one from the library reference, but they don't seem to do anything! Shouldn't there be,...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.