Tkinter는 프로세스 중간에 캔버스를 업데이트해야합니다. 완료되었을 때만 업데이트됩니다.

sixD

함수 호출 중에 Tkinter 캔버스어떻게 업데이트 할 수 있습니까? 와 비슷한 문제가 있음을 알 수 있습니다. 하지만 내 상황에 대한 대답을 번역 할 수 없습니다. 그 대답에서 자신을 호출하는 함수의 솔루션은 원칙적으로 괜찮지 만 목록 a을 호출하는 것이 아니라 함수 에 전달해야 합니다.

btw, 그래 나는 프로그램이 작동한다는 것을 안다 : a첫 번째 줄인 목록 인쇄 # 'd out in def renderGUI(a):works just fine

질문 : Tkinter가 목록 a(0은 조명이 꺼져 있음)을 켜고 끄 도록 애니메이션을 적용하려면 어떻게해야 합니까?

나는 또한 시도했다 유의하시기 바랍니다 w.update_idletasks()어떤 친구들이 꽤 아래로이 방법에 대한 것 같다하지만, 너무.

코드는 다음과 같습니다.

미리 감사드립니다.

from sys import stdout
from Tkinter import *
from time import sleep

numLights = 30
a     = [0,0,0,0,0,0,5,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
speed = [0,0,0,0,0,0,5,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]

# now set the speeds
for i in range (0,numLights):
    #print "i: " + str(i)
    if a[i] > 0:
        speed[i] = a[i]

# speed[n] is the speed that the nth light will travel at.  the bigger the number the slower: its the number of passes it dwells on each light

master = Tk()
w = Canvas(master, width=1000, height=100)
master.title("Speedlights")
w.pack()

size = 8
space = 12
offsetL = 10
offsetD = 35

for i in range (1,numLights):
    w.create_oval((i*space)+offsetL,offsetD,(i*space)+size+offsetL,offsetD+size)

def renderGUI(a):
    #print a
    for k in range (0,numLights):
        if a[k]>0:
            #then it must be 'on' #tg='L'+str(i)
            w.create_oval((k*space)+offsetL,offsetD,(k*space)+size+offsetL,offsetD+size, fill="red")#, tags=(tg))
        else:
            #turn it off
            w.create_oval((k*space)+offsetL,offsetD,(k*space)+size+offsetL,offsetD+size, fill="grey")#, tags=(tg))

# set this up as a function?
for i in range (0,numLights):
    master.after(100, renderGUI(a))
    w.update_idletasks()    
    for n in range(0,numLights):
        if a[n]>1:
            a[n] = a[n]-1
        elif a[n] == 1:
                #then its about to become nought, right?
                a[n-1] = speed[n]
                speed[n-1] = speed[n]
                a[n] = a[n]-1
                continue
        continue

btn = Button(master, text='Stop', width=20, command=master.destroy)
btn.pack()
master.mainloop()
나태

프로그램을 작동 시키려면 for함수 내부의 루프를 사용하고 다음을 통해 호출하십시오 after.

def f():
    for i in range (0,numLights):
        master.after(100, renderGUI(a))
        w.update_idletasks()    
        for n in range(0,numLights):
            if a[n]>1:
                a[n] = a[n]-1
            elif a[n] == 1:
                    #then its about to become nought, right?
                    a[n-1] = speed[n]
                    speed[n-1] = speed[n]
                    a[n] = a[n]-1
                    continue
            continue

btn = Button(master, text='Stop', width=20, command=master.destroy)
btn.pack()
master.after(1, f)
master.mainloop()

그러나를 제거 update_idletasks하려면 코 루틴을 사용할 수 있습니다 (여기에서 데코레이터로 래핑 됨).

def updatesdisplay(root, t=0):
    def _updatesdisplay(func):
        def driver(iterator):
            try: next(iterator)
            except StopIteration: pass
            else: 
                if t: root.after(t, driver, iterator)
                else: root.after_idle(driver, iterator)
        def wrapped():
            driver(func())
        return wrapped
    return _updatesdisplay

@updatesdisplay(master, 100) # 100 because we want to slow things down a bit
def f():
    for i in range (0,numLights):
        renderGUI(a)
        yield # window gets updated here
        for n in range(0,numLights):
            if a[n]>1:
                a[n] = a[n]-1
            elif a[n] == 1:
                    #then its about to become nought, right?
                    a[n-1] = speed[n]
                    speed[n-1] = speed[n]
                    a[n] = a[n]-1
                    continue
            continue

btn = Button(master, text='Stop', width=20, command=master.destroy)
btn.pack()
master.after(1, f)
master.mainloop()

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관