# ========================================================= # Display card images (using labels) # the cards displayed are picked randomly and new # random cards can be displayed by clicking on the # "Cards" button # ========================================================= # # 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 from random import randint if sys.version_info.major is 3: import tkinter as tk py3 = True else: import Tkinter as tk py3 = False from PIL import Image import card_values as cv # --------------------------------------------------------- # --- global variables # --------------------------------------------------------- buttonFont = ('arial', 15, 'bold') labelFont = ('arial', 18, 'bold') cardDir = 'cards/' # --------------------------------------------------------- # --- functions # --------------------------------------------------------- def quit(): sys.exit() def newCards(): new1 = tk.PhotoImage(file=randomCard(cards)) l1.configure(image=new1) l1.card = new1 new2 = tk.PhotoImage(file=randomCard(cards)) l2.configure(image=new2) l2.card = new2 def randomCard(cards): i = randint(0, len(cards)-1) return cardDir + cards[i] # --------------------------------------------------------- # --- main # --------------------------------------------------------- # --- card image files cards = cv.cardsvalues.keys() # --- 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=randomCard(cards)) img2 = tk.PhotoImage(file=randomCard(cards)) # --- 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.card = img1 l1.grid(row=0, column=0) l2 = tk.Label(fl, width=w2, height=h2, anchor=tk.CENTER, image=img2) l2.card = img2 l2.grid(row=0, column=1) # --- Buttons fb = tk.Frame(f) fb.grid(row=2, column=0) fb.grid_rowconfigure(0, weight=0) fb.grid_columnconfigure(0, weight=1) bq = tk.Button(fb, text='Quit', font=buttonFont, command=quit) bq.pack(side=tk.LEFT) bn = tk.Button(fb, text='Cards', font=buttonFont, command=newCards) bn.pack(side=tk.LEFT) # --- main loop tk.mainloop()