Tkinter canvas not showing

I am trying to learn some Python and Tkinter. The code example below is designed to display two windows, several buttons and a canvas with an image in it and some lines drawn on it.

Windows and buttons are displayed very well, however I do not see any canvas or canvases. I would appreciate help finding out what needs to be done for my canvas.

from Tkinter import *
import Image, ImageTk

class App:

    def __init__(self, master):

    def scrollWheelClicked(event):
        print "Wheel wheeled"

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

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

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

    image = Image.open("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")

    canvas.pack

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

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

root = Tk()
app = App(root)
root.mainloop()
+3
source share
2 answers

You need brackets when calling the package on the canvas object. Otherwise, you simply access the function object, but do not call it.

For instance:

canvas.pack()

Another example:

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

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

>>>hello() hello

+2

:

self.photo = ImageTk.PhotoImage(image)
self.item = canvas.create_image(0, 0, image=self.photo)

ImageTk - App.__init__() , , . (Tkinter .)

- "self.photo" "" , (, , "TEN = 10 '),' PHOTO = PhotoImage (...) '... . " gc "( Python 3, ) gc.disable() . ( : https://docs.python.org/3/library/gc.html)

+5

Source: https://habr.com/ru/post/1731908/


All Articles