# ========================================================= # Display card images (using canvases) # ========================================================= # # 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('Canvas 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 lt = tk.Label(f, text='Card Images', font=labelFont) lt.grid(row=0, column=0, sticky=tk.N+tk.E+tk.W) # --- 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 frame to hold canvases fc = tk.Frame(f, relief='flat') fc.grid(row=1, column=0) fc.grid_rowconfigure(0, weight=1) fc.grid_columnconfigure(0, weight=1) # --- create canvases to display images c1 = tk.Canvas(fc, width=w1, height=h1) c1.grid(row=0,column=0, sticky=tk.N+tk.S+tk.E+tk.W) c2 = tk.Canvas(fc, width=w2, height=h2) c2.grid(row=0,column=1, sticky=tk.N+tk.S+tk.E+tk.W) # --- display images c1.create_image(int(w1/2),int(h1/2),image=img1) c2.create_image(int(w2/2),int(h2/2),image=img2) # --- 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()