2
我無法獲得此代碼的工作。它應該從輸入函數生成一個分形圖像。它將完美運行,然後打印出單色帆布而不是分形。當我將Mandelbrot的分形函數切換到Newton-Raphson時,出現了這個問題。與Mandelbrot不同,Newton-Raphson需要一個單獨的定義來調用函數。這是給我麻煩的一點。我輸入了一個打印語句,它似乎正在運行,但它運行不正常。它運行所有的點到最大迭代(MaxIt),然後給我一個單一的彩色畫布,認爲他們都逃到無限。以下是當前代碼的副本:無法判斷Python函數是否正在調用?
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()
提取牛頓迭代算法到一個新的功能,並從GUI代碼分開測試。 – jfs
我懷疑它與你的具體問題有什麼關係,但你不應該在分形函數中調用'mainloop()'。你已經有一個事件循環運行,你不需要兩個。 –
好的,我單獨嘗試了數學,它工作得很好。 我試着刪除額外的'mainloop()',但現在它不起作用。 – Silmarillion101