【问题标题】:Why tkinter canvas doesn't update when used in a class为什么 tkinter 画布在课堂上使用时不更新
【发布时间】:2014-07-13 20:01:42
【问题描述】:

我正在编写一个小程序,我想在画布上画一些东西。这段代码对我有用;

import tkinter as tk
from PIL import Image, ImageTk
from l_systems import Lindenmayer

if __name__ == "__main__":

    root = tk.Tk()
    root.title("Draw Shapes with L-Equations")

    cv = tk.Canvas(width=600, height=600, bg='white')
    cv.pack()

    image1 = Image.new("RGB", (600, 600), (255,255,255))
    koch = Lindenmayer(image1)

    koch.init(
        iterations = 6,
        angle      = 25,
        axiom      = "---X",
        rules      = {"X":"2F-[1[X]+3X]4+F[3+FX]-X", "F":"FF"},
        constants   = "X") # This creates a drawing on PIL image

    # Canvas.create_image expects a PhotoImage
    photo = ImageTk.PhotoImage(image1) 
    cv.create_image((300,300), image=photo)


    root.mainloop()

但是,我想将我的 tkinter 应用程序组织为一个类,因此我尝试了这段代码,

class main(tk.Frame):

    w = 600
    h = 600

    def __init__(self,parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)

        self.parent = parent
        self.cv = tk.Canvas(width=self.w, height=self.h, bg='white')
        self.cv.pack()

        self.render_image()

    def render_image(self):
        image1 = Image.new("RGB", (self.w, self.h), (255,255,255))
        koch = Lindenmayer(image1)

        koch.init(
            iterations = 6,
            angle      = 25,
            axiom      = "---X",
            rules      = {"X":"2F-[1[X]+3X]4+F[3+FX]-X", "F":"FF"},
            constants   = "X"
        )

        photo = ImageTk.PhotoImage(image1)
        self.cv.create_image((self.w/2,self.h/2), image=photo)

if __name__ == "__main__":

    root = tk.Tk()
    root.title("Draw Shapes with L-Equations")
    app = main(root).pack()
    root.mainloop()

在第二种情况下,我在画布上看不到任何绘图。它只是一个白色背景。我该如何解决这个问题?

【问题讨论】:

  • 一如既往地在代码中添加print,看看变量发生了什么以及执行了哪部分代码。
  • 顺便说一句:PhotoImage 在类和函数中可能存在问题。 Garbage collector 可以将其从内存中删除。
  • @furas 我认为你是对的,将photo 更改为self.photo 解决了问题。如果您愿意,可以将其添加为答案。
  • 谢谢,我在回答中添加了一些关于课程的信息。

标签: python oop tkinter


【解决方案1】:

PhotoImage 在类和函数中可能有问题。 Garbage collector 可以将其从内存中删除。


编辑:

我可以检查一下(因为我必须 Lindenmayer 模块) 但你的班级可能看起来像这样:

几乎所有东西都在课堂上。

类名通常应使用CapWords 约定。 - 见PEP 8 -- Style Guide for Python Code。事件 SO 使用该规则来识别代码中的类并使用浅蓝色。

import tkinter as tk
from PIL import Image, ImageTk
from l_systems import Lindenmayer

class Main(tk.Frame):

    def __init__(self,parent, *args, **kwargs):

        tk.Frame.__init__(self, parent, *args, **kwargs)

        self.w = 600
        self.h = 600

        self.parent = parent

        self.parent.title("Draw Shapes with L-Equations")

        self.cv = tk.Canvas(width=self.w, height=self.h, bg='white')
        self.cv.pack()

        self.render_image()

        self.parent.pack()

    def render_image(self):

        image1 = Image.new("RGB", (self.w, self.h), (255,255,255))
        koch = Lindenmayer(image1)

        koch.init(
            iterations = 6,
            angle      = 25,
            axiom      = "---X",
            rules      = {"X":"2F-[1[X]+3X]4+F[3+FX]-X", "F":"FF"},
            constants   = "X"
        )

        self.photo = ImageTk.PhotoImage(image1)
        self.cv.create_image((self.w/2,self.h/2), image=self.photo)

    def run(self):
        self.parent.mainloop()

if __name__ == "__main__":

    Main(tk.Tk()).run()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    相关资源
    最近更新 更多