按顺序更改椭圆填充

时间:2016-04-28 19:59:52

标签: python tkinter tkinter-canvas

我是编程新手。我为老年人建造了康复设备,并已退休,因此在家工作。这是用于轮椅使用者腿部运动垫。我在tkinter画布上做了一个布局但是不能改变顺序的填充(红色和绿色)来显示要移动的脚和何时移动。每只脚的填充颜色需要打开约2秒,一次移动一只脚。我的代码远远是:

from tkinter import *
import tkinter 
#from time import sleep
import time

C = tkinter.Canvas(bg="blue", height=500, width=500)
oval1 = C.create_oval (171, 37.5, 196, 62.5, outline="white")#,     #fill="yellow")
oval2 = C.create_oval (304, 37.5, 329, 62.5, outline="white")#, #fill="yellow")
oval3 = C.create_oval (37.5, 137.5, 62.5, 162.5, outline="white")#, #fill="yellow")
oval4 = C.create_oval (437.5, 137.5, 462.5, 162.5, outline="white")#, #fill="yellow")
oval5 = C.create_oval (171, 237.5, 196, 262.5, outline="white")#, #fill="yellow")
oval6 = C.create_oval (304, 237.5, 329, 262.5, outline="white")#, #fill="yellow")
oval7 = C.create_oval (37.5, 337.5, 62.5, 362.5, outline="white")#, #fill="yellow")
oval8 = C.create_oval (437.5, 337.5, 462.5, 362.5, outline="white")#, #fill="yellow")
oval9 = C.create_oval (171, 437.5, 196, 462.5, outline="white")#, #fill="yellow")
oval10 = C.create_oval (304, 437.5, 329, 462.5, outline="white")#, #fill="yellow")
C.pack()       

我尽我所能,但无法使其发挥作用。请指出我正确的方向。

1 个答案:

答案 0 :(得分:0)

您好我需要让我的问题更清楚,我会更好地尝试包含我正在使用的代码。我尝试过使用After等,但程序要么停止,要么转到最后一个屏幕。与其他人的代码相比,我使用的东西看起来很幼稚,而且很长。幼稚是好的,但长时间意味着长时间的编辑,使其适合有特殊要求的人,他们将占多数。我用过#34; sleep"给予停顿。         到目前为止,该代码已完成约20%; 来自tkinter进口* 进口时间 从时间导入睡眠 import tkinter 将tkinter导入为tk 从进口时间* class App(tk.Tk):     def init (个体经营):         tk.Tk.的初始化(个体)         self.frame_Light = tk.Canvas()         self.light_on = True         self.canvas = tk.Canvas(bg ="浅蓝色",高度= 500,宽度= 500)         self.canvas.create_oval(171,37.5,196,62.5,outline =" black",tags =" light1")#,#fill         self.canvas.create_oval(304,37.5,329,62.5,outline =" black",tags =" light2")#,#fill =" yellow")         self.canvas.create_oval(37.5,137.5,62.5,162.5,outline =" black",tags =" light3")#,#fill =" yellow")         self.canvas.create_oval(437.5,137.5,462.5,162.5,outline =" black",tags =" light4")#,#fill =" yellow")         self.canvas.create_oval(171,237.5,196,262.5,outline =" black",tags =" light5")#,#fill =" yellow")         self.canvas.create_oval(304,237.5,329,262.5,outline =" black",tags =" light6")#,#fill =" yellow")         self.canvas.create_oval(37.5,337.5,62.5,362.5,outline =" black",tags =" light7")#,#fill =" yellow")         self.canvas.create_oval(437.5,337.5,462.5,362.5,outline =" black",tags =" light8")#,#fill =" yellow")         self.canvas.create_oval(171,437.5,196,462.5,outline =" black",tags =" light9")#,#fill =" yellow")         self.canvas.create_oval(304,437.5,329,462.5,outline =" black",tags =" light10")#,#fill ="黄色         self.canvas.pack(side =" top",fill =" both",expand = True)         self.draw_light()

def draw_light(self):
    for i in range(1): ## start
        self.canvas.itemconfig("light1", fill="white")
        self.canvas.itemconfig("light2", fill="white")
        self.canvas.itemconfig("light3", fill="white")
        self.canvas.itemconfig("light4", fill="white")
        self.canvas.itemconfig("light5", fill="red")
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.itemconfig("light7", fill="white")
        self.canvas.itemconfig("light8", fill="white")
        self.canvas.itemconfig("light9", fill="white")
        self.canvas.itemconfig("light10", fill="white")        
        self.canvas.update()
        sleep(2)    ### 1
        self.canvas.itemconfig("light1", fill="red")    
        self.canvas.itemconfig("light5", fill="white")
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)### 2
        self.canvas.itemconfig("light1", fill="white")
        self.canvas.itemconfig("light5", fill="red")       
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)### 3
        self.canvas.itemconfig("light2", fill="green")     
        self.canvas.itemconfig("light5", fill="red")
        self.canvas.itemconfig("light6", fill="white")
        self.canvas.update()
        sleep(2)### 4
        self.canvas.itemconfig("light2", fill="white")
        self.canvas.itemconfig("light5", fill="red")     
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)### 5
        self.canvas.itemconfig("light9", fill="red")     
        self.canvas.itemconfig("light5", fill="white")
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)### 6
        self.canvas.itemconfig("light9", fill="white")
        self.canvas.itemconfig("light5", fill="red")     
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)### 7
        self.canvas.itemconfig("light10", fill="green")     
        self.canvas.itemconfig("light5", fill="red")
        self.canvas.itemconfig("light6", fill="white")
        self.canvas.update()
        sleep(2)### 8
        self.canvas.itemconfig("light10", fill="white")
        self.canvas.itemconfig("light5", fill="red")     
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)### 9
        self.canvas.itemconfig("light1", fill="red")    
        self.canvas.itemconfig("light5", fill="white")
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)### 10
        self.canvas.itemconfig("light6", fill="white")
        self.canvas.itemconfig("light1", fill="red")       
        self.canvas.itemconfig("light2", fill="green")
        self.canvas.update()
        sleep(2)### 11
        self.canvas.itemconfig("light9", fill="red")     
        self.canvas.itemconfig("light2", fill="green")
        self.canvas.itemconfig("light1", fill="white")
        self.canvas.update()
        sleep(2)### 12
        self.canvas.itemconfig("light9", fill="white")
        self.canvas.itemconfig("light1", fill="red")     
        self.canvas.itemconfig("light2", fill="green")
        self.canvas.update()
        sleep(2)### 13
        self.canvas.itemconfig("light10", fill="green")     
        self.canvas.itemconfig("light1", fill="red")
        self.canvas.itemconfig("light2", fill="white")
        self.canvas.update()
        sleep(2)### 14
        self.canvas.itemconfig("light10", fill="white")
        self.canvas.itemconfig("light1", fill="red")     
        self.canvas.itemconfig("light2", fill="green")
        self.canvas.update()
        sleep(2)### 15
        self.canvas.itemconfig("light2", fill="green")     
        self.canvas.itemconfig("light9", fill="red")
        self.canvas.itemconfig("light1", fill="white")
        self.canvas.update()
        sleep(2)### 16
        self.canvas.itemconfig("light2", fill="white")
        self.canvas.itemconfig("light9", fill="red")     
        self.canvas.itemconfig("light10", fill="green")
        self.canvas.update()            
        sleep(2) ### 17
        self.canvas.itemconfig("light9", fill="white")
        self.canvas.itemconfig("light1", fill="red")     
        self.canvas.itemconfig("light10", fill="green")
        self.canvas.update()
        sleep(2)### 18
        self.canvas.itemconfig("light10", fill="green")     
        self.canvas.itemconfig("light9", fill="red")
        self.canvas.itemconfig("light1", fill="white")
        self.canvas.update()
        sleep(2)### 19
        self.canvas.itemconfig("light10", fill="white")
        self.canvas.itemconfig("light9", fill="red")     
        self.canvas.itemconfig("light2", fill="green")
        self.canvas.update()
        sleep(2)### 20
        self.canvas.itemconfig("light10", fill="green")     
        self.canvas.itemconfig("light9", fill="red")
        self.canvas.itemconfig("light2", fill="white")
        self.canvas.update()
        sleep(2)### 21
        self.canvas.itemconfig("light9", fill="white")
        self.canvas.itemconfig("light5", fill="red")     
        self.canvas.itemconfig("light10", fill="green")
        self.canvas.update()            
        sleep(2) ### 22
        self.canvas.itemconfig("light6", fill="green")     
        self.canvas.itemconfig("light5", fill="red")
        self.canvas.itemconfig("light10", fill="white")
        self.canvas.update()
        sleep(2) ###########################################


        self.canvas.itemconfig("light3", fill="red")    
        self.canvas.itemconfig("light5", fill="white")
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light3", fill="white")
        self.canvas.itemconfig("light5", fill="red")       
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light1", fill="red")     
        self.canvas.itemconfig("light5", fill="white")
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light1", fill="white")
        self.canvas.itemconfig("light5", fill="red")     
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light2", fill="red")     
        self.canvas.itemconfig("light5", fill="white")
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light2", fill="white")
        self.canvas.itemconfig("light5", fill="red")     
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light4", fill="red")     
        self.canvas.itemconfig("light5", fill="white")
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light4", fill="white")
        self.canvas.itemconfig("light5", fill="red")     
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light4", fill="green")    
        self.canvas.itemconfig("light5", fill="red")
        self.canvas.itemconfig("light6", fill="white")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light4", fill="white")
        self.canvas.itemconfig("light5", fill="red")       
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light2", fill="green")     
        self.canvas.itemconfig("light5", fill="red")
        self.canvas.itemconfig("light6", fill="white")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light2", fill="white")
        self.canvas.itemconfig("light5", fill="red")     
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light1", fill="green")     
        self.canvas.itemconfig("light5", fill="red")
        self.canvas.itemconfig("light6", fill="white")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light1", fill="white")
        self.canvas.itemconfig("light5", fill="red")     
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light3", fill="green")     
        self.canvas.itemconfig("light5", fill="red")
        self.canvas.itemconfig("light6", fill="white")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light3", fill="white")
        self.canvas.itemconfig("light5", fill="red")     
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2) #####

        self.canvas.itemconfig("light7", fill="red")    
        self.canvas.itemconfig("light5", fill="white")
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light7", fill="white")
        self.canvas.itemconfig("light5", fill="red")       
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light9", fill="red")     
        self.canvas.itemconfig("light5", fill="white")
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light9", fill="white")
        self.canvas.itemconfig("light5", fill="red")     
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light10", fill="red")     
        self.canvas.itemconfig("light5", fill="white")
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light10", fill="white")
        self.canvas.itemconfig("light5", fill="red")     
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light8", fill="red")     
        self.canvas.itemconfig("light5", fill="white")
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light8", fill="white")
        self.canvas.itemconfig("light5", fill="red")     
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light8", fill="green")    
        self.canvas.itemconfig("light5", fill="red")
        self.canvas.itemconfig("light6", fill="white")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light8", fill="white")
        self.canvas.itemconfig("light5", fill="red")       
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light10", fill="green")     
        self.canvas.itemconfig("light5", fill="red")
        self.canvas.itemconfig("light6", fill="white")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light10", fill="white")
        self.canvas.itemconfig("light5", fill="red")     
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light9", fill="green")     
        self.canvas.itemconfig("light5", fill="red")
        self.canvas.itemconfig("light6", fill="white")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light9", fill="white")
        self.canvas.itemconfig("light5", fill="red")     
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light7", fill="green")     
        self.canvas.itemconfig("light5", fill="red")
        self.canvas.itemconfig("light6", fill="white")
        self.canvas.update()
        sleep(2)
        self.canvas.itemconfig("light7", fill="white")
        self.canvas.itemconfig("light5", fill="red")     
        self.canvas.itemconfig("light6", fill="green")
        self.canvas.update()
        sleep(2) ##### code so far

app = App() app.mainloop()