本文介绍了tkinter非常慢 - 如何加速或使用不同的库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用tkinter生成2D形态图。我觉得它非常慢。例如,这个脚本在我的8核Xeon上花费了将近10秒钟的时间: #!/ usr / bin / env python3
进口随机
进口tkinter为tk
A = 3.419384662527591
B = 2.0158889752347022
C = 19.479697084985673
D = 61.006212908774614
F = 1.3449991745874286
G = 1.9590223710983992
H = 5.129501734860241
WIDTH = 800
HEIGHT = 600
类形态(tk .frame):
$ b $ def __init __(self,root):
tk.Frame .__ init __(self,root)
self.canvas = tk.Canvas(width = WIDTH, height = HEIGHT,borderwidth = 1)
self.canvas.pack(side =top,fill =both,expand = True)
def rand(self):
A = 3 + random.random()* 10
B = 1 + random.random()* 5
C = 1 + random.random()* 20
D = 1 + random.random()* 200
F = 1 + random.random()* 2
G = 1 + random.random()* 20
H = 1 + random.random ) * 20
def draw(self):
points = [0] * HEIGHT
points_space = [0] * HEIGHT
points_energy = [0] * HEIGHT
w = WIDTH
h = HEIGHT
min_x = 0
last_x = 0
last_y = 0
,而min_x< w:
min_x = w
lrand = 0
对于范围(1,h)中的y:
points_space [y] = points_space [y] + \
(points_space [y - 1] - points_space [y])/ F
在范围内(0,h):
lrand = lrand +(random.random( ) - 0.5)/ C
new_point = points [y] + points_space [y] + lrand
points_space [y] =(new_point - points [y])+ \
points_energy [y ] / G
points_space [y] = points_space [y] +(A - points_space [y])/ D
points_energy [y] = points_energy [y] / H
如果(points_space [y] points_energy [y] = points_space [y] - B
points_space [y] = B
points [y] = points [y] + points_space [y]
if(min_x> points [y]):
min_x = point s [y]
self.canvas.create_line(last_x,last_y,points [y],y)
last_x = points [y]
last_y = y
if __name__ ==__main__:
root = tk.Tk()
m =形态(根)
m.pack(fill =both, expand = True)
m.rand()
m.draw()
root.mainloop()
tkinter对于这类工作太弱了吗?我应该在看一个不同的2D库吗?
解决方案
我也使用Tk作为应用程序。
这是一个使用XML文件自动生成GUI的应用程序,而TK太慢了。
我转换到PYside,并对此非常高兴。它不像Tkinter那么容易使用,但它的速度更快,并具有很多可能性
I am generating 2D morphogenic plots using tkinter. I find it incredibly slow. For example, this script takes almost 10 seconds on my 8-core Xeon:
#!/usr/bin/env python3
import random
import tkinter as tk
A = 3.419384662527591
B = 2.0158889752347022
C = 19.479697084985673
D = 61.006212908774614
F = 1.3449991745874286
G = 1.9590223710983992
H = 5.129501734860241
WIDTH = 800
HEIGHT = 600
class Morphogenic(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root)
self.canvas = tk.Canvas(width=WIDTH, height=HEIGHT, borderwidth=1)
self.canvas.pack(side="top", fill="both", expand=True)
def rand(self):
A = 3 + random.random() * 10
B = 1 + random.random() * 5
C = 1 + random.random() * 20
D = 1 + random.random() * 200
F = 1 + random.random() * 2
G = 1 + random.random() * 20
H = 1 + random.random() * 20
def draw(self):
points = [0] * HEIGHT
points_space = [0] * HEIGHT
points_energy = [0] * HEIGHT
w = WIDTH
h = HEIGHT
min_x = 0
last_x = 0
last_y = 0
while min_x < w:
min_x = w
lrand = 0
for y in range(1, h):
points_space[y] = points_space[y] + \
(points_space[y - 1] - points_space[y]) / F
for y in range(0, h):
lrand = lrand + (random.random() - 0.5) / C
new_point = points[y] + points_space[y] + lrand
points_space[y] = (new_point - points[y]) + \
points_energy[y] / G
points_space[y] = points_space[y] + (A - points_space[y]) / D
points_energy[y] = points_energy[y] / H
if (points_space[y] < B):
points_energy[y] = points_space[y] - B
points_space[y] = B
points[y] = points[y] + points_space[y]
if (min_x > points[y]):
min_x = points[y]
self.canvas.create_line(last_x, last_y, points[y], y)
last_x = points[y]
last_y = y
if __name__ == "__main__":
root = tk.Tk()
m = Morphogenic(root)
m.pack(fill="both", expand=True)
m.rand()
m.draw()
root.mainloop()
Is tkinter just too weak for this kind of work? Should I be looking at a different 2D library?
解决方案
I used Tk for an application as well.
It was an application with an auto generated GUI from XML files, and TK was simply too slow.
I Switched to PYside and have been very happy about it. Its not as easy to use as Tkinter, but its much faster and has a lot of posibilities
这篇关于tkinter非常慢 - 如何加速或使用不同的库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!