python 2.7 - Add an image button to top level window Tkinter -


i'm building small gui application, once button clicked new top level window open , should display images buttons.

i can image button work on root window, not on top level window. blackbox appears.

i have generic button on both windows , work.

i'm new python.

import tkinter  tkinter import * pil import imagetk, image  root = tkinter.tk()  root.title("first window")                   root.configure(background = "black")      def new_window():     win2 = toplevel(root)     win2.geometry("650x350+50+40")             win2.title("second window!")                 win2.configure(background = "white")          def close1():         win2.destroy()      img1 = imagetk.photoimage(image.open("./images/close.gif"))     c1 = button(win2, image = img1, bg ="black", command = close1)     c1.grid(row = 1)      c2= tkinter.button(win2, text='close', command = close1)     c2.grid(row = 2)       nw = tkinter.button(root, text = 'new window' , command = new_window) nw.grid(row = 1)  def close3():      root.destroy()  img3 = imagetk.photoimage(image.open("./images/close.gif")) c3 = button(root, image = img3, bg ="black", command = close3) c3.grid(row = 2)   root.mainloop() 

when create new toplevel, using local variable refer image. because of this, when method exits, garbage collector delete image. need save reference in global variable, or other way protect garbage collector

a common way save reference make attribute of button:

img1 = imagetk.photoimage(...) c1 = button(...) c1.image = img1 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -