How to reference the relative directory when creating a photoimage - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to reference the relative directory when creating a photoimage (/thread-33687.html) |
How to reference the relative directory when creating a photoimage - kenwatts275 - May-17-2021 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. RE: How to reference the relative directory when creating a photoimage - menator01 - May-17-2021 You could try something like import os img_dir =os.getcwd() image = img_dir(f'images/my_image.png') RE: How to reference the relative directory when creating a photoimage - kenwatts275 - May-18-2021 (May-17-2021, 07:30 AM)menator01 Wrote: You could try something like 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 RE: How to reference the relative directory when creating a photoimage - menator01 - May-18-2021 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
|