在同一个类中将变量从一个函数传递到另一个函数?

2024-06-25 23:31:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个改变颜色的按钮。在

在翻阅了一本我多年没看过的Python旧书之后,我已经到了制作按钮的地步,但是我不知道如何将i传递到第二个函数中,这样它就会递增,然后被重置为0。在

我想我可以在第一个函数中增加click,但是现在我很恼火,想弄明白。在

而不是自我改变颜色我试了change_color(i)。这是一个错误。与尝试self.change_color(i)相同。在

我现在不知道该怎么办。在

import tkinter

class joeGUI:
    def __init__(self):

        i = 0
        colorArray = ['blue','DarkGreen','red','yellow']

        self.main_window = tkinter.Tk()

        self.color_button = tkinter.Button(self.main_window,
                                           text = 'Click to Change Color',
                                           command = self.change_color,
                                           bg = colorArray[i])
        self.color_button.pack()
        tkinter.mainloop()

    def change_color(self):
        if (count < 3):
            count += 1
        else:
            count = 0
        return count;


joe_gui = joeGUI()

Tags: 函数self颜色maintkinterdefcountbutton