如何快速更改标签之间的颜色?

时间:2016-04-12 15:58:33

标签: python tkinter

我希望快速切换颜色以代表Connect 4计划中的赢家。我真的很难过,目前还没办法怎么做。任何帮助表示赞赏。

提前致谢。这是我的代码:

from Tkinter import *
from functools import partial
import time

top = Tk()

top.title("Juan's Zany Super Crazy Connect Four!")

turn=1

nextS=[35,36,37,38,39,40,41]

def winFound():
    for f in range(0,7):
        buttonList[0+f].config(state=DISABLED)

def checkWin():
        for o in range (0,6):
            for h in range (0,4):
                if board[(35-7*o)+h].cget('bg')==board[(36-7*o)+h].cget('bg')==board[(37-7*o)+h].cget('bg')==board[(38-7*o)+h].cget('bg')!='grey80':
                    for q in range (0,4):
                        global turn
                        board[(35-7*o)+h].config(bg='green')
                        board[(36-7*o)+h].config(bg='green')
                        board[(37-7*o)+h].config(bg='green')
                        board[(38-7*o)+h].config(bg='green')
                        winFound()
        for o in range(0,3):
            for j in range(0,7):
                if board[(0+7*o)+j].cget('bg')==board[(7+7*o)+j].cget('bg')==board[(14+7*o)+j].cget('bg')==board[(21+7*o)+j].cget('bg')!='grey80':
                    for q in range (0,4):
                        global turn
                        board[(0+7*o)+j].config(bg='green')
                        board[(7+7*o)+j].config(bg='green')
                        board[(14+7*o)+j].config(bg='green')
                        board[(21+7*o)+j].config(bg='green')
                        winFound()
        for o in range(0,4):
            for x in range(0,3): 
                if board[35-(7*x)+o].cget('bg')==board[29-(7*x)+o].cget('bg')==board[23-(7*x)+o].cget('bg')==board[17-(7*x)+o].cget('bg')!='grey80':
                    for q in range (0,4):
                        global turn
                        board[35-(7*x)+o].config(bg='green')
                        board[29-(7*x)+o].config(bg='green')
                        board[23-(7*x)+o].config(bg='green')
                        board[17-(7*x)+o].config(bg='green')
                        winFound()
        for o in range(0,4):
            for x in range(0,3):
                if board[41-(7*x)-o].cget('bg')==board[33-(7*x)-o].cget('bg')==board[25-(7*x)-o].cget('bg')==board[17-(7*x)-o].cget('bg')!='grey80':
                    for q in range (0,4):
                        global turn
                        board[41-(7*x)-o].config(bg='green')
                        board[33-(7*x)-o].config(bg='green')
                        board[25-(7*x)-o].config(bg='green')
                        board[17-(7*x)-o].config(bg='green')
                        winFound()
def buttonStuff(z):
    global turn
    if turn==1:
        board[nextS[z]].config(bg='blue')
        nextS[z]=nextS[z]-7
        turn=turn+1
        if nextS[z]<0:
            buttonList[z].config(state=DISABLED)

    elif turn==2:
        board[nextS[z]].config(bg='red')
        nextS[z]=nextS[z]-7
        turn=turn-1
        if nextS[z]<0:
            buttonList[z].config(state=DISABLED)

    checkWin()

top = Tk()
r=0
count=0
col=0

buttonList = list()
for z in range(7):
    buttonList.append(Button(text=str(z), font='times 48', command=partial(buttonStuff, z)))
    buttonList[-1].grid(row=r,column=col, sticky='NESW')
    count+=1
    col+=1

r=1
col=0
count=0

board = list()
for z in range(42):
        board.append(Label(text='', font='Helvetica 15', bg='grey80'))
        board[-1].grid(row=r,column=col, sticky='NESW', padx=2, pady=2, ipadx=2, ipady=25)
        count+=1
        col+=1
        if count==7:
            r=r+1
            count=0
            col=0


top.mainloop()

1 个答案:

答案 0 :(得分:1)

有许多使用tkinter进行动画的例子。基本思想是编写一个执行一帧动画的函数,然后使用after来安排下一帧。

程序方法

这是一个非常简单的函数实现,它会使窗口小部件闪烁两秒钟。必须让小部件闪烁,闪烁多少次,以及两种颜色。它将在颜色之间交替,每隔100毫秒调用一次。

def blink(widget, count, color1, color2):
    current_color = widget.cget("background")
    color = color2 if current_color == color1 else color1
    widget.configure(background=color)
    if count > 1:
        widget.after(100, blink, widget, count-1, color1, color2)

以下是如何使用它的示例,在默认背景和绿色之间切换:

root = tk.Tk()
label = tk.Label(root, text="Winner!")
label.pack()

blink(label, 20, "green", label.cget("background"))
root.mainloop()

面向对象的方法

这种功能正是类和继承的好处:获取现有对象并添加新功能。在这种情况下,我们要向标签添加blink方法。

以下类添加了blink方法,但其他方法与标准Label完全相同:

class BlinkableLabel(tk.Label):
    def blink(self, color):
        self._original_color = self.cget("background")
        self._blink_color = color

        print("calling blinker...")
        self._blinker()

    def _blinker(self, count=20):
        current_color = self.cget("background")
        if current_color == self._blink_color:
            self.configure(background=self._original_color)
        else:
            self.configure(background=self._blink_color)

        if count > 0:
            self.after(100, self._blinker, count-1)
        else:
            self.configure(background=self._original_color)

以下是如何使用它的示例:

root = tk.Tk()
label1 = BlinkableLabel(root, text="winner!")
label2 = BlinkableLabel(root, text="Danger!")
label1.pack(padx=20, pady=20)
label2.pack(padx=20, pady=20)

label1.blink("green")
label2.blink("red")

root.mainloop()