card_display_2.py

# =========================================================
# Display card images (using labels)
# =========================================================
#
# Uses pillow to manipulate/display images
# (PIL is a discontinued project)
#
# --- remove PIL if installed
#
# sudo apt-get remove python-pil
# sudo apt-get remove python3-pil
# sudo pip remove PIL
# sudo pip3 remove PIL
#
# --- install Pillow
#
# sudo pip install Pillow
# sudo pip3 install Pillow
#
# =========================================================

import sys

if sys.version_info.major is 3:
    import tkinter as tk
    py3 = True
else:
    import Tkinter as tk
    py3 = False

from PIL import Image

# ---------------------------------------------------------
# --- global variables
# ---------------------------------------------------------

buttonFont = ('arial', 15, 'bold')
labelFont  = ('arial', 18, 'bold')


# ---------------------------------------------------------
# --- functions
# ---------------------------------------------------------

def quit():
    sys.exit()


# ---------------------------------------------------------
# --- main
# ---------------------------------------------------------

# --- TK root window

root = tk.Tk()
root.title('Label Card Test')

root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

# --- create a frame to put everything in

f = tk.Frame(root, relief='flat')
f.grid(row=0, column=0, sticky=tk.N+tk.S+tk.E+tk.W)

f.grid_rowconfigure(0, weight=1)
f.grid_columnconfigure(0, weight=1)

# --- create a title label

ft = tk.Frame(f, relief='flat')
ft.grid(row=0, column=0)

ft.grid_rowconfigure(0, weight=0)
ft.grid_columnconfigure(0, weight=1)

lt = tk.Label(ft, text='Card Images', font=labelFont, anchor=tk.N)
lt.grid(row=0, column=0)

# --- read images and create photoImage objects

img1 = tk.PhotoImage(file='cards/AH.png')
img2 = tk.PhotoImage(file='cards/10S.png')

# --- get images width,height

w1 = img1.width()
h1 = img1.height()
w2 = img2.width()
h2 = img2.height()

# --- create a frame to hold labels

fl = tk.Frame(f, relief='flat')
fl.grid(row=1, column=0)

fl.grid_rowconfigure(0, weight=1)
fl.grid_columnconfigure(0, weight=1)

# --- create labels to display images

l1 = tk.Label(fl, width=w1, height=h1, anchor=tk.CENTER, image=img1)
l1.grid(row=0,column=0)

l2 = tk.Label(fl, width=w2, height=h2, anchor=tk.CENTER, image=img2)
l2.grid(row=0,column=1)

# --- Buttons

fb = tk.Frame(f)
fb.grid(row=2, column=0, sticky=tk.S+tk.E+tk.W)

fb.grid_rowconfigure(0, weight=0)
fb.grid_columnconfigure(0, weight=1)

bq = tk.Button(fb, text='Quit', font=buttonFont, command=quit)
bq.grid(row=0,column=0)

# --- main loop

tk.mainloop()