Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7956945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:08:52+00:00 2026-06-04T04:08:52+00:00

It’s a Python beginner question. I want to loop through several subfolders of parent

  • 0

It’s a Python beginner question.
I want to loop through several subfolders of parent folder (subfolders contain jpg and txt files). I want to show images using Tkinter. There should be an image and Next button beside – when button is clicked the next image from the list should be loaded.

How to force an application to stop on each image and wait for user reaction?
In the test code below the image loaded is from the last directory in the loop (images from other directories are not displayed although I can print their names during the loop run).

import Tkinter, os, glob
from PIL import Image, ImageTk


class simpleapp_tk(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid

        parentSrcFolder = r"D:\2012\RCIN\test"
        srcFoldersLst = os.listdir(parentSrcFolder)
        for srcFolder in srcFoldersLst:
            jpgFilesPathLst = glob.glob(os.path.join(parentSrcFolder, srcFolder, "*.jpg"))

            self.labelVariable = Tkinter.StringVar()
            label = Tkinter.Label(self,textvariable=self.labelVariable,anchor="w")
            label.grid(column=0,row=0,columnspan=2,sticky='EW')
            self.labelVariable.set(jpgFilesPathLst[0])

            cardImage = Image.open(jpgFilesPathLst[0])
            indexCard = ImageTk.PhotoImage(cardImage)
            labelImage = Tkinter.Label(self,image=indexCard)
            labelImage.image = indexCard
            labelImage.grid(column=0,row=3)

def main():
    app = simpleapp_tk(None)
    app.title('my application')
    app.mainloop()

if __name__ == '__main__':
    main()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:08:53+00:00Added an answer on June 4, 2026 at 4:08 am

    The easiest way that I can think of doing this :

    first, create a method display_next which will increment an index and display the image associated with that index in a list (assume the list is a list of filenames). Enclosing the list inquiry in a try/except clause will let you catch the IndexError that happens when you run out of images to display — At this point you can reset your index to -1 or whatever you want to happen at that point.

    get the list of filenames in __init__ and initialize some index to -1 (e.g. self.index=-1).

    create a tk.Button in __init__ like this:

    self.Button = Tkinter.Button(self,text="Next",command=self.display_next)
    

    Another side note, you can use a widget’s config method to update a widget on the fly (instead of recreating it all the time). In other words, move all the widget creation into __init__ and then in display_next just update the widget using config. Also, it’s probably better to inherit from Tkinter.Frame…

    class SimpleAppTk(Tkinter.Frame):
        def __init__(self,*args,**kwargs):
            Tkinter.Frame.__init__(self,*args,**kwargs)
    
            self.filelist=[]  #get your files here
            #it probably would look like:
            #for d in os.listdir(parentDir):
            #    self.filelist.extend(glob.glob(os.path.join(parentDir,d,'*.jpg'))
            self.index=-1
            self.setup()
            self.display_next()
    
        def setup(self):
            self.Label=Tkinter.Label(self)
            self.Label.grid(row=0,column=0)
            self.Button=Tkinter.Button(self,text="Next",command=self.display_next)
            self.Button.grid(row=0,column=1)
    
        def display_next(self):
            self.index+=1
            try:
                f=self.filelist[self.index]
            except IndexError:
                self.index=-1  #go back to the beginning of the list.
                self.display_next()
                return
    
            #create PhotoImage here
            photoimage=...
            self.Label.config(image=photoimage)
            self.Label.image=photoimage
    
    if __name__ == "__main__":
       root=Tkinter.Tk()
       my_app=SimpleAppTk(root)
       my_app.grid(row=0,column=0)
       root.mainloop()
    

    EDIT

    I’ve given an example of how to actually grid the Frame. In your previous example, you had self.grid in your initialization code. This really did nothing. The only reason you had results was because you were inheriting from Tkinter.Tk which gets gridded automatically. Typically it’s best practice to grid after you create the object because if you come back later and decide you want to put that widget someplace else in a different gui, it’s trivial to do so. I’ve also changed the name of the class to use CamelCase in agreement with PEP 8 … But you can change it back if you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the
I want to construct a data frame in an Rcpp function, but when I
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.