2015-09-24 39 views
0

我想要做一些動畫與計時器一起工作。當時間結束時,動畫應該同時完成。我正在考慮在手機上做一些像電池條一樣的東西。這就是爲什麼我在另一個窗口中有一個綠色矩形,但我不知道如何處理綠色變黑或長方形空的慢慢用計時器。如果你有另一個動畫的建議,你可以做。由於我如何做一個在python中使用set timer的動畫?


from tkinter import * 
import math 
import time 

aux=False 
segundo=60 

Ventana1 = Tk() 
Ventana1.title("Timer") 
Ventana1.geometry("500x350+100+100") 

def paso(): 
    global aux 
    global segundo 

    if aux: 
     segundo -= 1 
     tiempo["text"] = segundo 
     tiempo.after(1000, paso) 

    if segundo==0: 
     aux=False 
     tiempo.configure(text=segundo, fg="blue") 

    if segundo<21: 
     tiempo.configure(text=segundo, fg="red") 

def inicio(): 
    global aux 
    global segundo 

    segundo=segundo 
    if aux: 
     pass 

    else: 
     aux=True 
     paso() 
     tiempo.configure(text=segundo, fg="blue") 

def pausa(): 
    global aux, segundo 

    aux=False 
    tiempo.configure(text=segundo, fg="blue") 

def reset(): 
    global aux 
    global segundo 

    segundo=int(Entry.get(Segunditos)) + 60*int(Entry.get(Minuticos)) 
    aux=False 
    tiempo["text"] = segundo 
    tiempo.configure(text=segundo, fg="blue") 

tiempo = Label(Ventana1, text=segundo, font=("calibri", 200)) 
tiempo.pack() 

Button(Ventana1, text="Arranquelo", command= inicio).place(x=120, y=220)#Button start 
Button(Ventana1, text= "Parelo", command= pausa).place(x=200, y=220)#button pause 
Button(Ventana1, text= "Acabelo", command= reset).place(x=300, y=220)#button reset 

MinAviso=Label(Ventana1, text="Minutos").place(x=125, y=260) 
SegAviso=Label(Ventana1, text="Segundos").place(x=305, y=260) 

Minuticos=Entry(Ventana1) 
Minuticos.place(x=70, y=290) 

Segunditos=Entry(Ventana1) 
Segunditos.place(x=250, y=290) 

from tkinter import * 

Ventana2 = Tk() 

Canvas = Canvas(Ventana2, width=2000, height=1500) 
Canvas.pack() 
Canvas.create_rectangle(200, 200, 1000, 300, fill="green") 

mainloop() 

#Ventana1.mainloop() 
+0

你沒有得到錯誤與'從Tkinter的進口*' – visc

+0

所以我得到了你的代碼的工作......你能解釋一下你希望發生 – visc

+0

我想要什麼,矩形作品與進度條計時器。當時間減少的矩形顏色也減少,直到零 – njacome19

回答

0

最大的問題是,你正在創建的Tk兩個實例。 Tkinter的設計並非如此。如果您想要一個浮動窗口,請創建Toplevel的實例。

接下來,您需要保存對畫布上繪製的矩形的引用。

Canvas = Canvas(Ventana2, ...) 
rect = Canvas.create_rectangle(...) 

當您的計時器運行時,您可以使用該引用修改對象。

def paso(): 
    ... 
    if aux: 
     ... 
     Canvas.itemconfigure(rect, ...) 
相關問題