Python Forum
tuple indices must be integers or slices, not str
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tuple indices must be integers or slices, not str
#8
https://docs.python.org/3/library/tempfile.html

Quote:class tempfile.TemporaryDirectory(suffix=None, prefix=None, dir=None, ignore_cleanup_errors=False)
This class securely creates a temporary directory using the same rules as mkdtemp(). The resulting object can be used as a context manager (see Examples). On completion of the context or destruction of the temporary directory object, the newly created temporary directory and all its contents are removed from the filesystem.

In other words, as soon as you leave the context created by this:
    with tempfile.TemporaryDirectory() as temp_folder:
The temporary directory is deleted.

There is no need to write the image data to files. Create the images and keep them in a list. If you want to write files to a temporary directory, you could wrap you program in the temp_folder context.
with tempfile.TemporaryDirectory() as temp_folder:
    window = function_that_creates_your_main_window()
    window.mainloop()
Or you could make temp_folder a global variable
def select_pdf_file():
    global temp_folder, current_index
    
    file_path = filedialog.askopenfilename(filetypes=[("PDF Files", "*.pdf")])
    if file_path:
        pdf_file = fitz.open(file_path)
        current_index = 0
        temp_folder = tempfile.TemporaryDirectory()
        image_index = 0
        for page in pdf_file:
            for xref, *_ in page.get_images():
                # extract the image
                info = pdf_file.extract_image(xref)
                image = PIL.Image.open(io.BytesIO(info ["image"]))  #<-  Why not put this in a list??
                image_file = os.path.join(temp_folder.name, f"image{image_index}.{info ["ext"]}")
                image.save(os.path.join(temp_folder.name, image_file))
                show_image(temp_folder.name, image_file)
                image_index += 1
Making temp_folder a global variable means the temporary folder stays alive until you reassign the temp_folder variable (no reference->delete folder), exit the program, or call the cleanup() function (temp_folder.cleanup()).

One last thing, this does not belong inside select_pdf_file()
            def on_key_press(event):
                global current_index
                if event.keysym == "Right":
                    current_index = (current_index + 1) % len(image_files)
                    pil_image = PIL.Image.open(os.path.join(temp_folder, image_files[current_index]))
                    tk_image = ImageTk.PhotoImage(pil_image)
                    image_label.config(image=tk_image)
                elif event.keysym == "Left":
                    current_index = (current_index - 1) % len(image_files)
                    pil_image = PIL.Image.open(os.path.join(temp_folder, image_files[current_index]))
                    tk_image = ImageTk.PhotoImage(pil_image)
                    image_label.config(image=tk_image)
 
            root.bind("<Key>", on_key_press)
You can embed a function inside another function, but you should only do this with "helper" functions, or if you are making a closure. I don't think either applies in this case.
Reply


Messages In This Thread
RE: tuple indices must be integers or slices, not str - by deanhystad - Feb-28-2023, 05:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,312 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,374 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,599 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 2,129 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error "list indices must be integers or slices, not str" dee 2 1,532 Dec-30-2022, 05:38 PM
Last Post: dee
  TypeError: string indices must be integers JonWayn 12 3,604 Aug-31-2022, 03:29 PM
Last Post: deanhystad
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,707 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  string indices must be integers when parsing Json ilknurg 3 6,552 Mar-10-2022, 11:02 AM
Last Post: DeaD_EyE
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,913 Nov-04-2020, 11:26 AM
Last Post: Aggam
  TypeError: string indices must be integers hendern 2 3,075 Oct-02-2020, 10:16 PM
Last Post: hendern

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020