canvas - Tkinter 创建图像函数错误(pyimage1 不存在)

标签 canvas tkinter python-3.3 fractals

我是来自外部世界的学生,之前没有编程经验。我一直在学习 Python 作为数学课的延伸。我一直在尝试创建一个使用 Tkinter 生成分形的程序。代码本身运行良好,但包含用户输入 GUI 会导致它给出错误:

    Exception in Tkinter callback
    Traceback (most recent call last):
      File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
        return self.func(*args)
      File "C:\Python33\FractalGUI.py", line 74, in fractals
        canvas.create_image((0, 0), image = img, state = "normal", anchor = tkinter.NW)
      File "C:\Python33\lib\tkinter\__init__.py", line 2319, in create_image
        return self._create('image', args, kw)
      File "C:\Python33\lib\tkinter\__init__.py", line 2310, in _create
        *(args + self._options(cnf, kw))))
    _tkinter.TclError: image "pyimage1" doesn't exist

代码本身如下。请注意,直到 canvas.create_image 才会出现该错误。线运行。如果我可以提供任何其他信息,请告诉我。谢谢! :)
    import tkinter
    from tkinter import *

    #Creates widgets for user input
    class Imagespecs(Frame):

        def __init__(self,master):
            Frame.__init__(self,master)
             self.grid()
             self.y_axis()
             self.x_axis()

    #Y axis input
         def y_axis(self):
            self.instruction = Label(self,text = "How many pixels high do you want the image?")
            self.instruction.grid(row = 8, column = 0, columnspan = 2, sticky = N)

            self.height = Entry(self)
            self.height.grid(row = 10, column = 1, sticky = E)

    #Enters info to run fractal generation
            self.submit_button = Button(self,text = "Submit", command = self.fractals)
            self.submit_button.grid(row = 14, column = 2, sticky = E)

    #X axis input
         def x_axis(self):
             self.instruction2 = Label(self,text = "How many pixels wide do you want the image?")
             self.instruction2.grid(row = 4, column = 0, columnspan = 2, sticky = E)

            self.width = Entry(self)
            self.width.grid(row = 6, column = 1, sticky = E)

      #generates fractal
         def fractals(self):
             #Replace non-input
             content = self.width.get()
             content2 = self.height.get()

             if content == "":
                content = 500

             if content2 == "":
                content2 = 500

            #Create window specs
            WIDTH = int(content2); HEIGHT = int(content)
            xa = -2.0; xb = 1.0
            ya = -1.5; yb = 1.5
            maxIt = 256

             window = Tk()
             canvas = Canvas(window, width = WIDTH, height = HEIGHT, bg = "#000000")
             img = PhotoImage(width = WIDTH, height = HEIGHT)

             #The Newton-Raphson iteration
             h = HEIGHT
            for ky in range(HEIGHT):
                print (h)
                h = h - 1
                for kx in range(WIDTH):
                    c = complex(xa + (xb - xa) * kx / WIDTH, ya + (yb - ya) * ky / HEIGHT)
                    z = complex(0.0, 0.0)
                     for i in range(maxIt):
                        z = z * z + c
                        if abs(z) >= 2.0:
                            break
                     rd = hex(i % 4 * 64)[2:].zfill(2)
                     gr = hex(i % 8 * 32)[2:].zfill(2)
                     bl = hex(i % 16 * 16)[2:].zfill(2)
                     img.put("#" + rd + gr + bl, (kx, ky))

             canvas.create_image((0, 0), image = img, state = "normal", anchor = tkinter.NW)

             #Run GUI
             canvas.pack()
             mainloop()

     root = Tk()
     root.title("Fractal GUI")
     root.geometry("300x200")
     app = Imagespecs(root)

     root.mainloop()

最佳答案

尝试定义一个主人:

PhotoImage(master = canvas, width = WIDTH, height = HEIGHT)

如果你没有定义一个主,那么这个图像使用第一个 Tk()这是创建的,如果是 Tk被删除没有图像可显示。

告诉我它是否有效,我猜。

关于canvas - Tkinter 创建图像函数错误(pyimage1 不存在),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23224574/

相关文章:

javascript - 如何创建 nxm HTML5 canvas 对象网格(彩色方 block )?

python - 使用字典,练习代码

python-3.3 - 我从哪里获得 python 3.3 的 pythonnet?

python-3.x - 如何在执行使用 pyinstaller 创建的 exe 文件时在我的 tkinter 窗口而不是 CMD 窗口上获取 python 控制台日志

python - TKinter 选项菜单 : How to get the selected choice?

python - 拆分多个文件 tkinter 框架

python - 是否可以将 QTableWidget 水平添加到布局中?

javascript - Canvas 线宽太粗

java - 如何在全屏 Canvas 上绘制位图?

javascript - 在 ReactJS 中每次重新渲染时获取一个新的 FabricJS-Canvas