首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tkinter create image函数错误(pyimage1不存在)

Tkinter create image函数错误(pyimage1不存在)
EN

Stack Overflow用户
提问于 2014-04-23 00:02:06
回答 3查看 39.7K关注 0票数 5

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

代码语言:javascript
复制
    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行之前,该错误不会出现。如果还有其他我能提供的信息,请告诉我。谢谢!:)

代码语言:javascript
复制
    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()
EN

回答 3

Stack Overflow用户

发布于 2014-04-23 04:02:02

试着定义一个master:

代码语言:javascript
复制
PhotoImage(master = canvas, width = WIDTH, height = HEIGHT)

如果未定义母版,则此图像将使用创建的第一个Tk(),如果删除了该Tk,则不会显示任何图像。

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

票数 13
EN

Stack Overflow用户

发布于 2014-04-24 02:27:02

好的,谢谢你们的投入,各位!我设法修复了它,将window = Tk()更改为window = Toplevel,并将anchor = tkinter.NW替换为anchor = NW。现在它完全按照我的预期运行了。打开以完成输入GUI!:D

票数 6
EN

Stack Overflow用户

发布于 2022-02-24 07:06:06

解决它的方法是

做这些就行了

将tkinter作为tk导入

window=tk.TK()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23224574

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档