Monday, 15 June 2015

python 3.x - How to insert an image into a tkinter sub frame -



python 3.x - How to insert an image into a tkinter sub frame -

i trying develop programme has main frame (root) other frames opened , closed via menu. main frame has image inserted ok open frame , seek insert new image using:

canvas = canvas(date-time_window, width = 200, height = 200, bg = 'yellow') canvas.create_image(0, 0, image = moon_photo, anchor = nw)

i error pyimage2 doesnt exist, if remove reference frame:

canvas = canvas(width = 200, height = 200, bg = 'yellow')

the image appears in root frame ok, of course of study not in 1 want in.

is possible display images in more 1 frame @ time?

i have noticed same error image displayed in main frame if close main frame , re-open out total restart. 1 time again pyimage2 not exist plus canvas in root frame blank.

i have read comments on how tkinter deals garbage , suspect related. have read comment saying image should have sec reference attached have not been able see how implement action yet.

can done within tkinter or need using pillow instead?

sample code, original far long include.

the code called menu in root window , meant display local date , time info. trying image of nowadays moon phase included well:

def date_time(): date_time_window = tk() date_time_window.title('date & time') date_time_window.geometry('650x350') # widgets present_date = today main_heading = label(date_time_window, text = 'date & time') line1 = canvas(date_time_window, width = 650, height = 3) line1.create_line(0,2 , 650,2, width = 1, fill = 'cyan') date_txt = label(date_time_window,text = 'date: ') date_value = label(date_time_window, text = today.strftime('%d') + '/' + today.strftime('%m') + '/' + today.strftime('%y')) time_utc_txt = label(date_time_window, text = 'utc time:') time_utc_value = label(date_time_window, text = str(datetime.utcnow())[11:19]) dls_txt = label(date_time_window, text = 'dls on/off: ') if moon_phase_number == 0: moon_photo = photoimage( file = 'images\moon_phase_0.gif') elif moon_phase_number == 1: moon_photo = photoimage( file = 'images\moon_phase_1.gif') canvas = canvas(date_time_window, width = 200, height = 200, bg = 'yellow') canvas.create_image(0, 0, image = moon_photo, anchor = nw) canvas.place( x = 300, y = 150) close_btn.configure( command = date_time_window.destroy) date_time_window.mainloop()

i can't debug code sample given without sscce, i'll effort reply remaining questions satisfactorily.

is possible display images in more 1 frame @ time?

yes, there's no particular restriction on number of frames may contain images.

i have read comment saying image should have sec reference attached have not been able see how implement action yet.

that comment in reference this document:

note: when photoimage object garbage-collected python (e.g. when homecoming function stored image in local variable), image cleared if it’s beingness displayed tkinter widget.

to avoid this, programme must maintain reference image object. simple way assign image widget attribute, this:

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

similarly, should attach moon_photo 1 of widgets. canvas.image = moonphoto sufficient.

can done within tkinter or need using pillow instead?

people recommend pil/pillow displaying images, because photoimage can display gif , pgm images. pil's imagetk.photoimage class supports many more kinds of formats. however, since you're trying render gifs here, don't need other tkinter.

python-3.x tkinter photoimage

No comments:

Post a Comment