I am attempting to code a card game in Python and want to place an image, but the image wont appear on the canvas in Tkinter -


from tkinter import * frame = tk() frame.geometry("1200x900")  w = canvas(frame,width=250,height=450) w.place(x=30,y=300)  img=photoimage(file="card2.ppm") w.create_image(100,100,anchor=nw,image=img)  frame.mainloop() 

the photoimage class can read gif , pgm/ppm images files:

photo = photoimage(file="image.gif")  photo = photoimage(file="lenna.pgm") 

the photoimage can read base64-encoded gif files strings. can use embed images in python source code (use functions in base64 module convert binary data base64-encoded strings):

photo = """ r0lgoddheaaqaicaaaaaaaebaqicagmdawqebaufbqy    gbgchbwgicakjcqokcgslcwwmda0ndq4o     dg8pdxaqebererisehmtexqufbuvfrywfhcxfxgygbkz        groaghsbgxwchb0dhr4ehh8fhyagiceh 

...

    afjhtq1bap/i/gpwry4aap/yatj77x+af4abawdwrzaaap8s    /j3dwcafwaa/jsm4j/lfwd+/qma 4b8aap9ci/4holtpfwd+qv4nohvaads= """  photo = photoimage(data=photo) 

if need work other file formats, python imaging library (pil) contains classes lets load images in on 30 formats, , convert them tkinter-compatible image objects:

from pil import image, imagetk  image = image.open("lenna.jpg") photo = imagetk.photoimage(image) 

you can use photoimage instance everywhere tkinter accepts image object. example:

label = label(image=photo) label.image = photo # keep reference! label.pack() 

you must keep reference image object in python program, either storing in global variable, or attaching object.

try running:

import tkinter tk  root = tk.tk() root.title("display website image") photo = tk.photoimage(file=  r "c:\some\local\location\smile.ppm") cv = tk.canvas() cv.pack(side='top', fill='both', expand='yes') cv.create_image(10, 10, image=photo, anchor='nw') root.mainloop() 

if not work try adding reference


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -