0

I have written an app to abstract data and save to excel sheet.

I plan to package the app using PyInstaller (which is working fine). However I am having issues referencing an image path that I am using for the title screen using Tkinter.

My thinking is the easiest way would be save the title image in a documents folder on the laptop and write the programme to reference this path for the image. However, I plan to share the app with a colleague so need the path name needs to be dynamic but have the final reference as /Documents/xxxxx.png

Below is the code to add context:

image = Image.open('MG_title.png') #this needs to be updated to include the dynamic pathname irrespective of system / user
    
#add function to resize Image
def resize_image(event):
    new_width = event.width
    new_height = event.height
    image = copy_of_image.resize((new_width, new_height))
    photo = ImageTk.PhotoImage(image)
    label.config(image = photo)
    label.image = photo #avoid garbage collection

#title page for programme
root = tk.Tk()
root.title('Manchester Giants')
root.resizable(False, False)
w = 600     # popup window width
h = 330     # popup window height
sw = root.winfo_screenwidth()
sh = root.winfo_screenheight()
x = (sw - w)/2
y = (sh - h)/2
root.geometry('%dx%d+%d+%d' % (w, h, x, y))

copy_of_image = image.copy()
photo = ImageTk.PhotoImage(image)
label = Label(root, image = photo)
label.bind('<Configure>', resize_image)
label.pack(fill=BOTH, expand = YES)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Adam Sewell
  • 129
  • 3
  • 10
  • Does this answer your question? [How to read a (static) file from inside a Python package?](https://stackoverflow.com/questions/6028000/how-to-read-a-static-file-from-inside-a-python-package) – Tomerikoo Jun 27 '21 at 20:37
  • What is your question? How does the code relate to the description – it seems to neither open a PNG, nor access /Documents/. – MisterMiyagi Jun 27 '21 at 20:37
  • @MisterMiyagi At the moment the png is in the same folder as the programme so runs fine. However, when I package the app using Pyinstaller and move the executable app to my desktop, the programme can't find the file path. Which is why I would like move the png to the general documents folder on my computer and reference it to it there. But needs to work on other machines too as wish to share the app – Adam Sewell Jun 27 '21 at 20:51
  • I have no experience with `PyInstaller` but it seems to me that storing and using a base64 representation of the image in your python file could solve the issue. – RJ Adriaansen Jun 27 '21 at 20:55

0 Answers0