對於我的班級,我創建了一個「Mandelbrot Explorer」程序。有一個主要問題:在實際繪製到畫布上時,我失去了對GUI的控制(全部使用Python 2.7中的Tkinter/Ttk編寫)。Tkinter帆布凍結程序
這裏是我的代碼:
# There is some code above and below, but only this is relevant
for real, imag in graph.PlaneIteration(self.graph.xMin, self.graph.xMax, resolution, self.graph.yMin, self.graph.yMax, resolution, master = self.graph, buffer_action = self.graph.flush):
# the above line iterates on the complex plane, updating the Canvas for every x value
c = complex(real, imag)
function, draw, z, current_iter = lambda z: z**2 + c, True, 0, 1
while current_iter <= iterations:
z = function(z)
if abs(z) > limit:
draw = False
break
current_iter += 1
self.progressbar.setValue(100 * (real + self.graph.xMax)/total)
color = self.scheme(c, current_iter, iterations, draw)
# returns a hex color value
self.graph.plot(c, color)
# self.graph is an instance of my custom class (ComplexGraph) which is a wrapper
# around the Canvas widget
# self.graph.plot just creates a line on the Canvas:
# self.create_line(xs,ys,xs+1,ys+1, fill=color)
我的問題是,在運行時,該圖形需要一段時間 - 大約30秒。在這個時候,我不能使用GUI。如果我嘗試,一旦完成繪圖,窗口就會凍結並且只會解凍。
我試圖使用線程(I所包圍的上部代碼的整體中的功能,thread_process
):
thread.start_new_thread(thread_process,())
然而,問題依然存在。
有沒有辦法解決這個問題?謝謝!
如果你想產生一個新的線程,你的線程應該繪製到某個地方的數組或圖像對象,而不是GUI上的畫布對象。然後,它可以返回可以在畫布上繪製的圖像。線程運行時,它不會以這種方式影響GUI。 –
@ChrisBarker我曾考慮過這樣做,但我想保留它的動畫(因爲它每列更新)。如果涉及到它,我將採用繪製到圖像,然後將圖像添加到畫布上,但現在我想避免這種情況:P。 –