2012-05-16 2 views
0

Tkinter에서 라벨의 배경에 두 가지 색상으로 시간이 흐려질 수있는 방법은 무엇입니까? 카운트 다운 할 때마다 내 타이머의 라벨 색상을 변경하고 싶습니다. 이들은 내가 현재하고있는 스 니펫입니다 (내가하고있는 일을 명확히하기 위해).tkinter에서 시간이 지남에 따라 레이블 색상이 교차 페이딩합니까?

… 

labelcolor = "#%02x%02x%02x" % (0, 0, 0) 

… 

def pomodoro(self, remaining = None): 
    self.button.configure(state=tk.DISABLED) 
    self.labelcolor = "#%02x%02x%02x" % (200, 32, 32) 
    self.label.configure(bg = self.labelcolor) 
    if remaining is not None: 
     self.remaining = remaining 

    if self.remaining <= 0: 
     self.label.configure(text="Time's up!") 
     self.breakcommand 
    else: 
     self.label.configure(text= time.strftime('%M:%S', time.gmtime(self.remaining))) #Integer to 'Minutes' and 'Seconds' 
     self.remaining = self.remaining - 1 
     self.after(1000, self.pomodoro) 

… 

self.label = tk.Label(self, text="Pick One", width=12, font="Helvetica 32", fg = "white", bg = self.labelcolor) 

… 
+0

문제에 대해 무엇을 이해하지 못합니까? 라벨의 배경색을 변경하는 방법을 모르거나 다양한 색상을 만드는 방법을 모르는가, 아니면 이해할 수없는 것이 있습니까? –

+0

@ 브라이언 나는 수학적인 관점에서 그 자체를 퇴색시키는 방법을 이해하지 못했고 거기에서 타이머를 기반으로하는 두 가지 색 사이의 페이딩을위한 유용한 함수를 만듭니다. 제 질문에 불명확해서 죄송합니다. –

답변

3

그라데이션이있는 색상 막대를 만들기 위해 내가 해킹 한 코드가 있습니다. 난 당신이 나중에 색상을 변경할 수 있도록 사각형에 핸들을 유지 할 수 있습니다, 물론

import Tkinter as tk 

def cinterp(x1,x2,x,c1,c2): 
    """ 
    interpolate two colors. c1 and c2 are 3-tuples of rgb values -- 
    e.g. (0,0,255) 

    x1,x2 and x are used to determine the interpolation constants. 
    """ 
    s1=float(x-x1)/(x2-x1) 
    s2=1.-s1 
    return [int(s1*i+s2*j) for i,j in zip(c1,c2)] 

root=tk.Tk() 
cvs=tk.Canvas(root,width=100,height=50) 
cvs.grid(row=0,column=0) 
width=int(cvs['width']) 
height=int(cvs['height']) 
for i in range(width): 
    fill=cinterp(0,width,i,(255,0,0),(255,255,255)) 
    fs="#%02x%02x%02x"%(fill[0],fill[1],fill[2]) 
    cvs.create_rectangle(i,1,i+1,height,fill=fs,width=0) 

root.mainloop() 

... ... 그것은 작동 당신에게 도움이 될 것인지 모르겠지만 아마도이 작업을보다 효율적으로 수행 할 수도 있지만 이는 좋은 출발점이 될 수 있습니다. 이 클래스에 많은 청소기,하지만 것

import Tkinter as tk 

def cinterp(x1,x2,x,c1,c2): 
    s1=float(x-x1)/(x2-x1) 
    s2=1.-s1 
    return [int(s1*i+s2*j) for i,j in zip(c1,c2)] 

root=tk.Tk() 
cvs=tk.Label(root,text="Hello") 
c2=(255,0,0) 
c1=(255,255,255) 
def updateCVS(dt,timeleft,deltat): 
    timeleft=timeleft-dt 
    fill=cinterp(0,deltat,deltat-timeleft,c1,c2) 
    fs="#%02x%02x%02x"%(fill[0],fill[1],fill[2]) 
    cvs.configure(bg=fs) 
    if(timeleft>0): 
     timeleft=timeleft-dt 
     cvs.after(int(dt*1000),updateCVS,dt,timeleft,deltat) 

cvs.grid(row=0,column=0) 
b=tk.Button(root,text="push me",command=lambda : updateCVS(.2,5,5)) 
b.grid(row=1,column=0) 

root.mainloop() 

편집 잘하면 당신은 아이디어를 얻을.

+0

이것은 다른 이유로 유용하지만 내가 찾고있는 것이 아닙니다. 내 카운터가 카운트 다운 할 때 시간이 지남에 따라 캔버스 배경색을 변경하고 싶습니다. 나는 희망을 갖고 내가하고 싶은 것을 분명히하기 위해 내 질문에 대해 다시 말을했다. –

+1

@RyanHasse 너무 쉽습니다. 그냥 함수 cinterp를 가져 와서'after' 호출에서 re-registration하는 것을 레이블이나 캔버스를 갱신하기 위해 호출하십시오. 지금 실제 해결책을 게시 할 시간이 없지만 아직 알지 못했다면 아침에 할 수 있습니다. – mgilson

+0

@RyanHasse : 편집 한 코드가 원하는대로 작동합니까? – mgilson

관련 문제