Tkinter Canvas inheritance

Markus von Ehr vonehr at ira.uka.de
Mon Mar 19 06:24:21 EST 2001


Hi,

I have a class inherited from Canvas. The create_line
command works well but when I use the create_image command, the
image isn't visible.

##################################################
from Tkinter import *
import thread

class Slider(Canvas):

    def __init__(self, parent = None, **kw):
        
	# Initialise the base class (after defining the options).
        Canvas.__init__(self, parent, kw)

	self.create_line(34, 52, 34, 127, width=2, fill="black")
	
        slider = PhotoImage(file="button.gif")
        img = self.canvas.create_image(25, 37, anchor=NW, image=slider)

	self.root = parent
	
if __name__ == '__main__':
    root = Tk()
    frame = Frame(root)
    frame.pack()
    
    c = Slider(frame, background="grey", width=50, height=141)
    
    c.pack(side=TOP)
    
    root.mainloop()



When I change the Code and create the image in the main function,
there's no problem, it works well. How can I do it in the constructor
and why doesn't it work????

############################################
class Slider(Canvas):

    def __init__(self, parent = None, **kw):
        
	# Initialise the base class (after defining the options).
        Canvas.__init__(self, parent, kw)

	self.create_line(34, 52, 34, 127, width=2, fill="black")
	
	self.root = parent
	
if __name__ == '__main__':
    root = Tk()
    frame = Frame(root)
    frame.pack()
    
    c = Slider(frame, background="grey", width=50, height=141)
    
    slider = PhotoImage(file="button.gif")
    c.create_image(25, 37, anchor=NW, image=slider)
    c.pack(side=TOP)
    
    root.mainloop()

Does anybody know where the rpoblem is?

Thanks,

Markus



More information about the Python-list mailing list