Tkinter 画布不显示

发布于 2024-08-20 12:11:21 字数 1128 浏览 9 评论 0原文

我正在尝试学习一些 Python 和 Tkinter。下面的示例代码旨在在屏幕上放置两个窗口、几个按钮和一个画布,其中包含图像并在其上绘制了一些线条。

窗口和按钮看起来很好,但是我没有看到画布图像或画布线。我希望得到一些帮助来弄清楚需要什么来使我的画布显示。

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()

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)
    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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骄傲 2024-08-27 12:11:21

我解决了这个问题:

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

对ImageTk实例的引用必须存储在某个地方,否则当你的App.__init__()方法返回时,它将被垃圾收集,并且画布将无法显示它。 (Tkinter 不保留对图像的引用。)

保留对图像的引用的一种方法是将其存储在“self.photo”或名为“photo”的变量中,或者像大多数程序员对常量变量(变量)所做的那样不会改变,例如“十= 10”),“照片= PhotoImage(...)”为什么...我不知道。导入模块“gc”(Python 3 的垃圾收集模块,内置)并运行 gc.disable() 不起作用。 (如果你想尝试一下:https://docs.python.org/3/library /gc.html)

I solved this problem:

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

A reference to the ImageTk instance must be stored somewhere, or when your App.__init__() method returns, it will be garbage collected, and the canvas will not be able to display it. (Tkinter does not keep a reference to the image.)

One way to keep a reference to it is by storing it in "self.photo", or a variable named 'photo', or, like most programmers do for constant variables (variables that don't change, like 'TEN = 10'), 'PHOTO = PhotoImage(...)' Of why... I have no idea. Importing the module 'gc' (Python 3's garbage collection module, built in) and running gc.disable() doesn't work. (If you want to try it: https://docs.python.org/3/library/gc.html)

静谧幽蓝 2024-08-27 12:11:21

在画布对象上调用 pack 时需要括号。否则,您只是引用函数对象,而不是调用它。

例如:又如

canvas.pack()

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

>>>hello 返回函数引用(function hello at 0x....)

>>>hello()实际上调用了 hello 函数

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

For example:

canvas.pack()

Another example:

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

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

>>>hello() actually calls the hello function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文