在python中注册屏幕更改?

时间:2017-07-16 16:16:53

标签: python-2.7 tkinter

所以使用 tkinter 我制作了一个简单的程序,每秒将画布上所有形状的颜色更改为随机颜色,我正在寻找的是一种注册每个屏幕更改的方法记下单独文件中屏幕更改之间的时间。如果可能的话,我还需要在没有太多外部库的帮助下进行。

到目前为止我的代码:

#!/usr/bin/python -W ignore::DeprecationWarning
import Tkinter
import tkMessageBox
import time
import random


colors = ['DarkOrchid1','chocolate3','gold2','khaki2','chartreuse2','deep pink','white','grey','orange']
top = Tkinter.Tk()
global b
b=0
C = Tkinter.Canvas(top, bg="blue", height=600, width=800)
bcg1=C.create_rectangle(0,0,800,100,fill=random.choice(colors))
bcg2=C.create_rectangle(0,100,800,200,fill=random.choice(colors))
bcg3=C.create_rectangle(0,200,800,300,fill=random.choice(colors))
bcg4=C.create_rectangle(0,300,800,400,fill=random.choice(colors))
bcg5=C.create_rectangle(0,400,800,500,fill=random.choice(colors))
bcg6=C.create_rectangle(0,500,800,600,fill=random.choice(colors))
bcgs=[bcg1,bcg2,bcg3,bcg4,bcg5,bcg6]
coord = 10, 50, 240, 210
rect1=C.create_rectangle(0,0,100,100,fill="green")
rect2=C.create_rectangle(700,500,800,600,fill="green")
rect3=C.create_rectangle(0,500,100,600,fill="green")
rect4=C.create_rectangle(700,0,800,100,fill="green")
def color():
    global b
    global bcgs
    global color
    if b==0:
        C.itemconfig(rect1,fill='green')
        C.itemconfig(rect2,fill='green')
        C.itemconfig(rect3,fill='green')
        C.itemconfig(rect4,fill='green')
        b=1
        count=0
        for i in range(len(bcgs)):
            C.itemconfig(bcgs[i],fill=random.choice(colors))

    elif b==1:
        C.itemconfig(rect1,fill='red')
        C.itemconfig(rect2,fill='red')
        C.itemconfig(rect3,fill='red')
        C.itemconfig(rect4,fill='red')
        b=0
        for i in range(len(bcgs)):
            C.itemconfig(bcgs[i],fill=random.choice(colors))


    top.after(2000, color)

C.pack()
color()
top.mainloop()

1 个答案:

答案 0 :(得分:1)

除非你使用实时操作系统,否则永远不会得到完美的时机。你可以在几分之一的时间内存钱。要查看多少,您可以在time.time()中计算差异。为获得最佳准确度,请将after调用移至函数中的第一个内容。

加上其他一些改进:

#!/usr/bin/python -W ignore::DeprecationWarning
import Tkinter
import time
import random
from itertools import cycle

colors = ['DarkOrchid1','chocolate3','gold2','khaki2','chartreuse2','deep pink','white','grey','orange']
rect_colors = cycle(['green', 'red'])

top = Tkinter.Tk()
C = Tkinter.Canvas(top, bg="blue", height=600, width=800)
bcg1=C.create_rectangle(0,0,800,100,fill=random.choice(colors))
bcg2=C.create_rectangle(0,100,800,200,fill=random.choice(colors))
bcg3=C.create_rectangle(0,200,800,300,fill=random.choice(colors))
bcg4=C.create_rectangle(0,300,800,400,fill=random.choice(colors))
bcg5=C.create_rectangle(0,400,800,500,fill=random.choice(colors))
bcg6=C.create_rectangle(0,500,800,600,fill=random.choice(colors))
bcgs=[bcg1,bcg2,bcg3,bcg4,bcg5,bcg6]
coord = 10, 50, 240, 210
rect1=C.create_rectangle(0,0,100,100,fill="green")
rect2=C.create_rectangle(700,500,800,600,fill="green")
rect3=C.create_rectangle(0,500,100,600,fill="green")
rect4=C.create_rectangle(700,0,800,100,fill="green")
rects = [rect1,rect2,rect3,rect4]

last_time = time.time()
def color():
    top.after(2000, color)
    global last_time

    rect_color = next(rect_colors)
    for item in rects:
        C.itemconfig(item, fill=rect_color)
    for item in bcgs:
        C.itemconfig(item, fill=random.choice(colors))

    print("{} seconds since the last run".format(time.time() - last_time))
    last_time = time.time()

C.pack()
color()
top.mainloop()