gpt4 book ai didi

python - 无法判断 Python 函数是否正在调用?

转载 作者:太空宇宙 更新时间:2023-11-04 06:03:24 25 4
gpt4 key购买 nike

我无法让这段代码工作。它应该从输入函数生成分形图像。它将完美运行,然后打印出单色 Canvas 而不是分形。当我将分形函数从 Mandelbrot 切换到 Newton-Raphson 时出现问题。与 Mandelbrot 不同,Newton-Raphson 需要单独定义要调用的函数。这是给我带来麻烦的一点。我输入了打印语句,它似乎在运行,但运行不正常。它运行所有的点到最大迭代(MaxIt),然后给我一个单一的彩色 Canvas ,认为它们都逃逸到无穷大。这是当前代码的副本:

from tkinter import *
from math 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):
maxIt = 2
ds = 0.2e-1
eps = 5e-5
#Replace non-input
content = self.width.get()
content2 = self.height.get()

if content == "":
content = 500

if content2 == "":
content2 = 500

def f(z):
return z**3 + 5
print ('lalala')

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

window = Toplevel()
canvas = Canvas(window, width = WIDTH, height = HEIGHT, bg = "#000000")
img = PhotoImage(width = WIDTH, height = HEIGHT)
canvas.create_image((0, 0), image = img, state = "normal", anchor = NW)

#The Newton-Raphson iteration
h = HEIGHT
for y in range(HEIGHT):
print (h)
h = h - 1
zy = y * (yb - ya) / (HEIGHT - 1) + ya
for x in range(WIDTH):
zx = x * (xb - xa) / (WIDTH - 1) + xa
z = complex(zx, zy)
for i in range(maxIt):
dz = (f(z + complex(ds, ds)) - f(z)) / complex(ds, ds)
z0 = z - f(z) / dz
if abs(z0 - z) < eps:
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, (x, y))


#Run GUI
canvas.pack()
mainloop()

#Run the class and everything else
root = Tk()
root.title("Fractal GUI")
root.geometry("300x200")
app = Imagespecs(root)

root.mainloop()

最佳答案

尽管您的代码中存在错误,例如缺少 z = z0 行,这里的罪魁祸首是 tkinter 的 PhotoImage 的一个已知功能,该功能与不保存对图像和垃圾收集的引用有关:

Why do my Tkinter images not appear?

我重新编写了您的代码以生成分形并尝试修复我注意到的那些问题:

from tkinter import *

MAX_ITERATIONS = 300
DS = 0.2e-1
EPS = 5e-5

# Create window specs
XA, XB = -1.0, 1.0
YA, YB = -1.0, 1.0

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

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

# Y axis input
def y_axis(self):
instruction = Label(self, text="How many pixels high do you want the image?")
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
submit_button = Button(self, text="Submit", command=self.fractals)
submit_button.grid(row=14, column=2, sticky=E)

# X axis input
def x_axis(self):
instruction = Label(self, text="How many pixels wide do you want the image?")
instruction.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):

def f(z):
return z**3 + 5

# Replace non-input
try:
width = int(self.width.get())
except ValueError:
width = 500

try:
height = int(self.height.get())
except ValueError:
height = 500

canvas = Canvas(Toplevel(), width=width, height=height, bg="#000000")
img = PhotoImage(width=width, height=height)
canvas.create_image((0, 0), image=img, state="normal", anchor=NW)

# The Newton-Raphson iteration
for y in range(height):
zy = y * (YB - YA) / (height - 1) + YA

for x in range(width):
zx = x * (XB - XA) / (width - 1) + XA
z = complex(zx, zy)

i = 0 # avoid undefined variable after loop

for i in range(MAX_ITERATIONS):
dz = (f(z + complex(DS, DS)) - f(z)) / complex(DS, DS)
z0 = z - f(z) / dz
if abs(z0 - z) < EPS:
break
z = z0

red = i % 4 * 64
green = i % 8 * 32
blue = i % 16 * 16
img.put("#%02x%02x%02x" % (red, green, blue), (x, y))

# Run GUI
canvas.pack()
self.image = img # save reference so image isn't GC'd!

# Run the class and everything else
root = Tk()
root.title("Fractal GUI")
root.geometry("400x150")

app = Imagespecs(root)

root.mainloop()

enter image description here

关于python - 无法判断 Python 函数是否正在调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23481803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com