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
#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


Messages In This Thread
RE: How to reference the relative directory when creating a photoimage - by menator01 - May-18-2021, 07:22 PM

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,170 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,593 Sep-07-2020, 08:02 AM
Last Post: perfringo
  pyautogui screenshotting region relative to image found on screen Bmart6969 3 6,455 Oct-05-2019, 06:20 PM
Last Post: Bmart6969
  how do i write a cross-platform relative file path? pseudo 2 2,984 Aug-23-2019, 05:07 PM
Last Post: ThomasL
  function wanted: resolve relative path Skaperen 4 3,389 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,210 May-29-2018, 08:38 AM
Last Post: QbLearningPython
  Creating user accounts in Google Directory amankahlon 1 2,502 May-08-2018, 07:48 AM
Last Post: nilamo
  ValueError: Attempted relative import in non-package JoeB 1 11,986 Mar-08-2018, 11:01 AM
Last Post: Gribouillis
  Relative import multiple levels ? Windspar 3 4,472 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