当我单击渲染按钮时它开始没有响应,我该怎么办?

时间:2016-10-18 16:09:01

标签: python tkinter tkinter-canvas

我试图做一个小小程序,在给出详细信息时会产生腐蚀性。问题是我希望它通过3个窗格进入,在第二个窗格中它应该显示一个小的加载栏来显示它已经完成的进展,遗憾的是我从未看到这个加载栏,因为当我点击我的按钮启动我的程序时开始没有响应,但当它完成所有的计算后,它再次开始响应并向我显示我的图像。我尝试引入时间延迟,但它没有帮助。我应该怎么做才能停止响应?如果您需要任何帮助来理解代码,请告诉我。

from tkinter import *
from tkinter import ttk
from math import *
from colour import *
import time

ps1 = 1
breite1 = 150
hoehe1 = 150
s1 = 0.01
r1 = int(hoehe1/2-5)
matrix=[]

def Pixel(p,frb):
        s0,t0=int((breite1)/2),int((hoehe1)/2)
        x,y=p[0],p[1]
        s,t=s0+x,t0-y
        return canvas.create_rectangle(s-0.5*ps1, t-0.5*ps1, s+0.5*ps1, t+0.5*ps1, fill=frb, outline=frb)

def Runden(z,n):
        if z>=0:
            return int(z/n+0.5)*n
        else:
            return -int(-z/n+0.5)*n

def Strecke_Matrix(a,b):
        s0,t0=int((breite1)/2),int((hoehe1)/2)
        x0,y0=Runden(a[0],ps1),Runden(a[1],ps1)
        x1,y1=Runden(b[0],ps1),Runden(b[1],ps1)
        if x0==x1:
            ya,yb=min(y0,y1), max(y0,y1)
            y=ya
            while y<=yb:
                l,k=int((x0+s0)/ps1),int((t0-y)/ps1)
                if 0<=k<=len(matrix)-1 and 0<=l<=len(matrix[0])-1:
                    matrix[k][l]+=1
                y+=ps1
        else:
            if x0<x1:
                xa,xb=x0,x1
                ya,yb=y0,y1
            else:
                xa,xb=x1,x0
                ya,yb=y1,y0
            m=(yb-ya)/(xb-xa)
            if abs(m)<=1:
                x=xa
                while x<=xb:
                    y=Runden(m*(x-xa)+ya,ps1)
                    l,k=int((x+s0)/ps1),int((t0-y)/ps1)
                    if 0<=k<=len(matrix)-1 and 0<=l<=len(matrix[0])-1:
                        matrix[k][l]+=1
                    x+=ps1
            else:
                if yb-ya>0:
                    xna,yna=ya,xa
                    xnb,ynb=yb,xb
                else:
                    xna,yna=yb,xb
                    xnb,ynb=ya,xa
                mn=(ynb-yna)/(xnb-xna)
                x=xna
                while x<=xnb:
                    y=Runden(mn*(x-xna)+yna,ps1)
                    xtemp,ytemp=y,x
                    l,k=int((xtemp+s0)/ps1),int((t0-ytemp)/ps1)
                    if 0<=k<=len(matrix)-1 and 0<=l<=len(matrix[0])-1:
                        matrix[k][l]+=1
                    x+=ps1
            return matrix

def PointLight_Matrix(s, r, n):
    w = -1*pi
    while w < pi:
           a = 0
           while a <=  n:
                  p0 = [cos(a*w)*r, sin(a*w)*r]
                  p1 = [cos((a+1)*w)*r, sin((a+1)*w)*r]
                  Strecke_Matrix(p0,p1)
                  a = a + 1
                  time.sleep(0.02)
           w = w + s
    r2 = 0
    for x in matrix:
           for z in x:
                  if z > r2:
                         r2 = z
    return r2

def Matrix1(s, r, n):
    w = -0.5*pi
    while w < 0.5*pi:
        a = 0
        while a <= n:
            p0 = [cos(-a*pi + (2*a+1)*w)*r, sin(-a*pi + (2*a+1)*w)*r]
            p1 = [cos(-(a + 1)*pi + (2*(a + 1)+1)*w)*r, sin(-(a + 1)*pi + (2*(a +1)+1)*w)*r]
            Strecke_Matrix(p0,p1)
            a = a+1
            time.sleep(0.02)
        w = w + s
    r2 = 0
    for x in matrix:
           for z in x:
                  if z > r2:
                         r2 = z
    return r2

##def maxi(matrix):
##        r2 = 0
##        for x in matrix:
##            for z in x:
##                if z > r2:
##                    r2 = z
##        return r2


def render():
              p2.forget(f1)
              p2.add(f2)
              s0,t0=int((breite1)/2),int((hoehe1)/2)
              t1 = str(typ.get())
              n1 = int(n.get())
              g1 = str(g.get())
              b1 = int(b.get())
              if t1 == "Parallel Light":
                  r2 = Matrix1(s1, r1, n1)
              else:
                  r2 = PointLight_Matrix(s1, r1, n1)
              farbe = Color(g1)
              c = 0
              q = 0
              p = 100/(len(matrix))
              u = int((q*10)/10)
              p9["maximum"] = p
              for x in matrix:
                  z = 0
                  p9["value"] = u
                  for a in x:
                      farbe.luminance = min(1,(a/r2)*b1)
                      x, y = z*ps1-s0 , t0 - c*ps1
                      Pixel([x, y],farbe)
                      z += 1
                  q += p
                  c += 1
              p2.forget(f2)
              p2.add(f3)


hor=int((breite1)/ps1)
ver=int((hoehe1)/ps1)
matrix=[]
for i in range(ver):
    zeile=[]
    for j in range(hor):
        zeile.append(0)
    matrix.append(zeile)


root = Tk()
root.title("App")


mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

p2 = ttk.Panedwindow(root, orient="horizontal")
f1 = ttk.Labelframe(p2, text="Settings", height = 150, width = 300)
f2 = ttk.Labelframe(p2, text="Rendering…", height = 150, width = 300)
f3 = ttk.Labelframe(p2, text="Caustic", height = 200, width = 200)
p2.grid(column=0, row=0, sticky=(N, W, E, S))
p2.columnconfigure(0, weight=1)
p2.rowconfigure(0, weight=1)
p2.add(f1)


canvas = Canvas(f3, height=hoehe1, width=breite1)
canvas.grid(column=6, row=1, rowspan = 20, padx=10, pady=5)


typ = StringVar()
n = StringVar()
g = StringVar()
b = StringVar()

è = ttk.Label(f1, text="Warning! The higher the number of reflections the longer it will take")
è.grid(column = 1, row = 1, columnspan=4)

type_ = ttk.Combobox(f1, textvariable=typ, state="readonly")
type_.grid(column=2, row=2, sticky=(W, E))
type_['values'] = ('Point Light', 'Parallel Light')
type_.current(0)
ttk.Label(f1, text="Type of Light:").grid(column=1, row=2, sticky=W)

q = ttk.Combobox(f1, textvariable=n)
q.grid(column=2, row=3, sticky=(W, E))
q['values'] = (1,2,3,4,5)
q.current(0)
ttk.Label(f1, text="Number of reflections:").grid(column=1, row=3, sticky=W)

g_entry = ttk.Entry(f1, width=7, textvariable=g)
g_entry.grid(column=2, row=4, sticky=(W, E))
ttk.Label(f1, text="Color of the Image:").grid(column=1, row=4, sticky=W)

s = ttk.Combobox(f1, textvariable=b, state="readonly")
s.grid(column=2, row=5, sticky=(W, E))
s['values'] = (1,2,3,4,5,6,7,8,9,10)
s.current(0)
ttk.Label(f1, text="Brightness:").grid(column=1, row=5, sticky=W)

p9 = ttk.Progressbar(f2, orient="horizontal", length=200, mode='determinate')
p9.grid(column=6, row=21,padx=5, pady=30)

b3 = ttk.Button(f1, text="Render", command=render)
b3.grid(column=2, row=6, sticky=W)
type_.focus()


root.mainloop()

0 个答案:

没有答案