0

I'm new to python and I'm trying this code:

import random
from random import choice
import tkinter
from tkinter import messagebox
from tkinter import *
from tkinter.filedialog import askopenfilename
import sys
from PIL import ImageTk, Image
from tkinter import filedialog
import os

num_npcs = 2
num_img_tot = 100
npc_window = {}
canvas = {}
background_label = {}

for i in range(0, num_npcs,1):
    num_img = round(random.uniform(0.5, num_img_tot+0.5))
    npc_window["npc_window{0}".format(i)] = Tk()
    canvas["canvas{0}".format(i)] = Canvas(npc_window["npc_window{0}".format(i)], bg="blue", height=250, width=300)
    filename["canvas{0}".format(i)] = PhotoImage(file=base_folder + 'biome\\'+str(char)+'\\'+str(int) + '\\'+str(num_img)+'.png')
    background_label["background_label{0}".format(i)] = Label(npc_window["npc_window{0}".format(i)], image=filename["canvas{0}".format(i)])
    background_label["background_label{0}".format(i)].place(x=0, y=0, relwidth=1, relheight=1)
    canvas["canvas{0}".format(i)].pack()

but I probably messed up a little bit... I'm not used to GUI and stuff and I'm actually experimenting. By the way the error is:

_tkinter.TclError: image "pyimage2" doesn't exist

I'm trying to make a program that opens X (random) images, that's why I tried Tk(). Also I'm using Pycharm in a Windows 10 machine.

j_4321
  • 15,431
  • 3
  • 34
  • 61
Penga
  • 3
  • 5
  • 1
    please add a minimal example including imports – dgruending Aug 31 '20 at 20:19
  • 1
    At least part of the problem is that you're creating more than one instance of `Tk`. You should only ever create one. If you need more windows you should be using `Toplevel`. See [Why are multiple instances of Tk discouraged?](https://stackoverflow.com/q/48045401/7432) – Bryan Oakley Aug 31 '20 at 20:23
  • On top of the multiple TK instance issue: The "pyimage2" error does not occur when removing the `PhotoImage` line. are you sure that your image is located in the provided position? – dgruending Aug 31 '20 at 20:45
  • As far as i can tell, i provided the path in the PhotoImage function under the argument file= . i had some problems with the path cos of the slashes and i fixed it with : base_folder = os.path.join(__file__).replace("/","\\").replace("encounter.py","") replacing the actual /, from .join, with \ and removing "encounter.py" (cos i think it was __file__ to adding it) – Penga Aug 31 '20 at 20:55
  • what is the intended path? what is `str(char)` supposed to do? – dgruending Aug 31 '20 at 21:25
  • \biome\alignment\1 Where alignment and 1 are variables, char is already a str so its a useless casting.. – Penga Aug 31 '20 at 21:38
  • I am not sure about your operating system but at least in Linux replacing the backslash with a double backslash is not necessary – dgruending Aug 31 '20 at 21:57
  • i tried with / and with \ but always: "image "pyimage2" doesn't exist" i wonder if its some problem with the PhotoImage, it should give me an image object from the path right? – Penga Aug 31 '20 at 23:13
  • I need the random cos i want to pick up random images from a specified path. – Penga Aug 31 '20 at 23:14
  • For your case, add `master=npc_window["npc_window{0}".format(i)]` in `PhotoImage(...)`. However, multiple instances of `Tk()` should be avoided. – acw1668 Sep 01 '20 at 01:11
  • if Tk() should be avoided, how can i put images on top(in precise order) of the background without messing with the background itself? – Penga Sep 01 '20 at 01:23
  • I think you could create `Tk()` on top and then maybe create `Toplevel()` inside loops instead – Delrius Euphoria Sep 01 '20 at 03:39
  • I have found the problem, i think that the garbage collector may have deleted the images, i have fixed with a reference to the images themselves using the method .image: background_label["background_label{0}".format(i)].image = filename["filename{0}".format(i)] – Penga Sep 01 '20 at 13:24

1 Answers1

0

The pyimage2 error may be due to an incorrect file path. Suggestion to improve folfer path:

base_folder = "base_folder"
alignment_list = [0.1, 0.2, 0.3]
integer_list = [1,2,3]
images = []

tk = Tk()
for alignment, integer in zip(alignment_list, integer_list):
    image_name = "img" + str(integer) + ".png"
    file_name = os.path.join(base_folder, "biome", str(alignment), str(integer), image_name)
    print(file_name)
    img = PhotoImage(file=file_name)
    images.append(img)

Which gives

base_folder/biome/0.1/1/img1.png
base_folder/biome/0.2/2/img2.png
base_folder/biome/0.3/3/img3.png
dgruending
  • 973
  • 9
  • 15