SooDesuNe SooDesuNe - 4 months ago 9
Python Question

Tkinter Canvas Does Not Display

I'm attempting to learn some Python and Tkinter. The sample code below is intended to put two windows on the screen, a few buttons, and a Canvas with an image in it and some lines drawn on it.

The windows and buttons appear just fine, however I'm not seeing either the canvas image or canvas lines. I'd appreciate some help to figure out what's need to make my canvas display.

from Tkinter import *
import Image, ImageTk

class App:

def __init__(self, master):

def scrollWheelClicked(event):
print "Wheel wheeled"

frame = Frame(master)
self.button = Button(frame, text = "QUIT", fg="red", command=frame.quit)

self.hi_there = Button(frame, text="Hello", command=self.say_hi)

top = Toplevel()
canvas = Canvas(master=top, width=600, height=600)

image ="c:\lena.jpg")
photo = ImageTk.PhotoImage(image)
item = canvas.create_image(0, 0, image=photo)

canvas.create_line(0, 0, 200, 100)
canvas.create_line(0, 100, 200, 0, fill="red", dash=(4, 4))
canvas.create_rectangle(50, 25, 150, 75, fill="blue")


testBtn = Button(top, text = "test button")

def say_hi(self):
print "hi there everyone!"

root = Tk()
app = App(root)


You need parenthesis when calling pack on the canvas object. Otherwise, you're just referring to the function object, not calling it.

For example:


Another example:

>>>def hello():
...    print "hello world"
...    return

>>>hello returns the function reference (function hello at 0x....)

>>>hello() actually calls the hello function