May-17-2021, 02:51 AM
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:
I tried the following but none of them work:
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.