为什么单击按钮时按钮不改变颜色?

时间:2019-09-05 07:58:20

标签: python

我的代码没有给我我想要的东西。当我单击它时,它变成蓝色直到放开,然后又变回白色。我希望按钮在单击时能改变颜色。因此,如果我单击一次,我希望它更改为蓝色。如果我再次单击它,请从蓝色更改为红色,并用6种总颜色(白色,蓝色,绿色,红色,橙色,黄色)(白色是起始颜色)进行此操作。

我是python编码的新手,老实说,这不是我的全部代码,所以我不知道它在做什么。我仔细阅读了代码,看看可以做什么。我也研究了我的问题堆,但仍然找不到答案。我认为这是代码中的一部分。这只是(魔方的)第一面,所以无论响应/答案/更正,还是对其他五面来说……

def enter_yellow_face():
    top,mid,btm=[0,0,0],[0,0,0],[0,0,0]
    bord = [top,mid,btm]
    counter = [0, 0, 0,
               0, 0, 0,
               0, 0, 0]

def change_color(x, b=bord):
    # seems like x is the square number out of the 9 squares
    #global counter
    r=x/3
    c=x%3
    if b[r][c] == 0:
        b[r][c]= 5
        bb[x].ss.configure(bg='white', activebackground='white')
        counter[x] = 0
        yellow_face[x] = 'w'

    elif counter[x] == 0:
        bb[x].ss.configure(bg='blue', activebackground='blue')
        counter[x] = 1
        yellow_face[x] = 'b'

    elif counter[x] == 1:
        bb[x].ss.configure(bg='red', activebackground='red')
        counter[x] = 2
        yellow_face[x] = 'r'

        ### keep following the cycle of white-yellow-red...
    elif counter[x] == 2:
        bb[x].ss.configure(bg='green', activebackground='green')
        counter[x] = 3
        yellow_face[x] = 'g'

    elif counter[x] == 3:
        bb[x].ss.configure(bg='orange', activebackground='orange')
        counter[x] = 4
        yellow_face[x] = 'o'

    elif counter[x] == 4:
        bb[x].ss.configure(bg='yellow', activebackground='yellow')
        counter[x] = 5
        yellow_face[x] = 'y'

    elif counter[x] == 5:
        bb[x].ss.configure(bg='white', activebackground='white')
        counter[x] = 0
        yellow_face[x] = 'w'

root = Tk()
root.title('Enter Yellow Face')

class Knop():
    """This is the docstring of the class"""

    def __init__(self, i, master=None):
        self.nummer = i
        self.row = i/3
        self.col = i%3
        def human_move():
            change_color(self.nummer)
        self.ss = Button(root, command=human_move, bg='yellow', activebackground='yellow', width=10, height=5)
        self.ss.grid(row=self.row, column=self.col)

        next_face = Button(root, text="Next Face",  command=root.destroy)
        next_face.grid(row=4, column=1)

bb = range(9)
for i in range(9):
    bb[i]= Knop(i, master=root)

mainloop()

我认为这将创建一个弹出窗口,其中显示一些框,单击这些框将为我提供不同的颜色。就像从白色开始,当我单击我时,它会变成蓝色。如果我单击现在的蓝色框,它将变为红色。但是,当我运行它时,一旦我松开点击,它实际上就会变成蓝色并变回白色。

0 个答案:

没有答案