Python Forum
How to reference the relative directory when creating a photoimage
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to reference the relative directory when creating a photoimage
#1
Hello everyone.
I am writing a card game and I am trying to put an image of the card suit (clubs, diamonds, hearts or spades) on a button.
I have the images saved in a subdirectory called "images".
To create the Photoimage object, I use the following code:
photo_c = PhotoImage(file = r"C:\Users\kwatt\python\images\club.png")
However, I do not want to specify "C:\Users\kwatt\python" because I want to be able to run the script on another computer which will have a different path.
I tried the following but none of them work:
photo_c = PhotoImage(file = r"images\club.png")
photo_c = PhotoImage(file = r"\images\club.png")
photo_c = PhotoImage(file = r".\images\club.png")
Below is the entire program for reference:

from tkinter import *c
import tkinter.font as font

mw = Tk()
# 999x999 is size of window, 999+999 is the location of the window
mw.geometry('800x450+400+200')
mw.title(__file__)

frame3 = Frame(mw)
framebot = Frame(mw)
frame3.pack(side=TOP,fill=X)
framebot.pack(side=BOTTOM,fill=X)

buttonFont = font.Font(family='Helvetica', size=22, weight='bold')

btn5 = Button(framebot,text='Deal',font=("Times",16),command=lambda: main_program(deck,hand1,melds1,meld_types1,hand2,melds2,meld_types2,card_buttons,count_button,computer_buttons)).pack(side="left")
btn6 = Button(framebot,text='Exit',font=("Times",16),command=mw.quit).pack(side="right")

# Creating a photoimage object to use image
photo_c = PhotoImage(file = r"C:\Users\kwatt\python\images\club.png")
photo_d = PhotoImage(file = r"C:\Users\kwatt\python\images\diamond.png")
photo_h = PhotoImage(file = r"C:\Users\kwatt\python\images\heart.png")
photo_s = PhotoImage(file = r"C:\Users\kwatt\python\images\spade.png")
photo_b = PhotoImage(file = r"C:\Users\kwatt\python\images\blank.png")

# Resizing image to fit on button
photoimage_c = photo_c.subsample(3, 3)
photoimage_d = photo_d.subsample(3, 3)
photoimage_h = photo_h.subsample(3, 3)
photoimage_s = photo_s.subsample(3, 3)
photoimage_b = photo_b.subsample(3, 3)

d2 = Radiobutton(frame3,text=" ",value=10,indicatoron=0,width=60,height=88,
		image=photoimage_b,bg='white',font=buttonFont,compound=RIGHT)
d2.grid(row=0,column=2,rowspan=2)

d2['text'] = "5"
d2['fg'] = "black"
d2['image'] = photoimage_c

mw.mainloop()
Thanks in advance.
Reply
#2
You could try something like
import os
img_dir =os.getcwd()

image = img_dir(f'images/my_image.png')
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(May-17-2021, 07:30 AM)menator01 Wrote: You could try something like
import os
img_dir =os.getcwd()

image = img_dir(f'images/my_image.png')

When I use os.getcwd() it returns "/home/kwatt", not "C:/Users/kwatt".
When I append this to the front of my file, I get the following error:

_tkinter.TclError: couldn't open "/home/kwatt/python/images/club.png": no such file or directory
Reply
#4
This works for me
import tkinter as tk
import os
from functools import partial

def print_selected(var):
    label['font'] = ('times', 30)
    label['text'] = var

suites = ['heart', 'diamond', 'club', 'spade']
img_dir = os.getcwd()
print(img_dir)
root = tk.Tk()
root.title('Card Suites')
root.geometry('800x450+250+250')

col = 0
option = tk.StringVar()
option.set(suites[0])
for suite in suites:
    suite_img = tk.PhotoImage(file=f'{img_dir}\images\{suite}.png')
    suite_img.img = suite_img
    radio = tk.Radiobutton(root, image=suite_img, value=suite, var=option, \
    command=partial(print_selected, suite), indicatoron=0)
    radio.grid(row=0, column=col)
    col += 1
label = tk.Label(root)
label['text'] = 'default text'
label.grid(column=0, row=1, rowspan=4)

root.mainloop()
output for img_dir
Output:
C:\Users\John Doe\Desktop\cards
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cloning a directory and using a .CSV file as a reference to search and replace bg25lam 2 2,101 May-31-2021, 07:00 AM
Last Post: bowlofred
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,513 Sep-07-2020, 08:02 AM
Last Post: perfringo
  pyautogui screenshotting region relative to image found on screen Bmart6969 3 6,339 Oct-05-2019, 06:20 PM
Last Post: Bmart6969
  how do i write a cross-platform relative file path? pseudo 2 2,882 Aug-23-2019, 05:07 PM
Last Post: ThomasL
  function wanted: resolve relative path Skaperen 4 3,297 Sep-06-2018, 01:52 AM
Last Post: Skaperen
  Python 3.6.5 pathlib weird behaviour when resolve a relative path on root (macOs) QbLearningPython 7 6,098 May-29-2018, 08:38 AM
Last Post: QbLearningPython
  Creating user accounts in Google Directory amankahlon 1 2,449 May-08-2018, 07:48 AM
Last Post: nilamo
  ValueError: Attempted relative import in non-package JoeB 1 11,825 Mar-08-2018, 11:01 AM
Last Post: Gribouillis
  Relative import multiple levels ? Windspar 3 4,373 Feb-02-2018, 11:55 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

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