473,698 Members | 2,152 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(fil e=self.imagedir + 'folder_new.gif ')
self.save=Photo Image(file = self.imagedir + 'save.gif')
self.folder=Pho toImage(file=se lf.imagedir + 'folder.gif')
self.help=Photo Image(file=self .imagedir + 'help.gif')
self.fontsmall= PhotoImage(file =self.imagedir + 'fontsmall.gif' )
self.stop=Photo Image(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 2300
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(fil e=self.imagedir + 'folder_new.gif ')
self.save=Photo Image(file = self.imagedir + 'save.gif')
self.folder=Pho toImage(file=se lf.imagedir + 'folder.gif')
self.help=Photo Image(file=self .imagedir + 'help.gif')
self.fontsmall= PhotoImage(file =self.imagedir + 'fontsmall.gif' )
self.stop=Photo Image(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(se lf.imagedir, '*.gif')
image_names = glob.glob(patte rn)
for name in image_names:
base = os.path.basenam e(name).split(' .')[0]
pathname = os.path.join(se lf.imagedir, n)
self.setattr(ba se, 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(fil e=self.imagedir + 'folder_new.gif ')
self.save=Photo Image(file = self.imagedir + 'save.gif')
self.folder=Pho toImage(file=se lf.imagedir + 'folder.gif')
self.help=Photo Image(file=self .imagedir + 'help.gif')
self.fontsmall= PhotoImage(file =self.imagedir + 'fontsmall.gif' )
self.stop=Photo Image(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(se lf.imagedir, '*.gif')
image_names = glob.glob(patte rn)
for name in image_names:
base = os.path.basenam e(name).split(' .')[0]
pathname = os.path.join(se lf.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
4064
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 trying to work out the kinks in the process. Unable to run Idle using 2.3 the way I've got things set up, I created a super simple Tkinter test program that just pops up a Label widget. I pasted below an example of how the RH-provided 2.2 runs...
0
1510
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 without Tkinter okay, but when I try to build with Tkinter it doesn't seem to build the appropriate directories or files. For example, in version Python 2.3b1, I have the directory build/lib.darwin-6.6-Power_Macintosh-2.3, which contains...
2
6978
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 add all the files in the directory to an installer (including the tk-8.4 and tcl-8.4 directories), it builds an installer fine. But when I run the installer, and install to, say c:/Program Files/Shtoom,
8
2533
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 would be returned an instance of the class. The actual application I'm interested in writing would either have simple type attributes (int, string, etc.), or attributes using types already defined in a c-extension, although I'd prefer not to...
2
4081
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 open a python interpreter and run my subprocess command, all is well. But when I run the same command from inside a Tkinter app, I'm getting errors.
2
2842
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 don't mean a drop-down or a pop-up menu, but those options which yield to another batch of sub-options when scrolled over, (as for example, the File->New option from internet explorer).
1
2899
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 animation of sorts. What I noticed is, after holding down the key for a certain time, it would not loop through all 6 animations, instead it would just go through the first 2 and reset it self. I changed the key bind to Button-1 and did not...
2
2785
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 created the instance of this class and executed, I require the selected item to be sent back to the calling function, and the window should not be destroyed. When the button is pressed I require the control to get back to calling function and process...
7
3276
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, like, a dialog? I'm running Windows XP and using IDLE. You can assume my version of Python is the latest.
0
8608
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
8870
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...
0
7734
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6524
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
5860
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
4370
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3051
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.