简体   繁体   English

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

[英]Passing variable from one function to another in same class?

I'm trying to create a button that changes colors on click. 我正在尝试创建一个可在单击时更改颜色的按钮。

After digging around in an old Python book I haven't looked at in years, I've gotten to the point where I make the the button, but I can't figure out how to pass i into the second function so it increments and is then is reset to 0 . 在翻阅了我多年没有看过的一本老Python书之后,我已经了解到了制作按钮的要点,但是我不知道如何将i传递给第二个函数,因此它会递增和然后将重置为0

I suppose I could just increment I on click in the first function, but now I'm annoyed and want to figure it out. 我想我可以在第一个功能中单击以增加我的数量,但是现在我很烦,想找出答案。

Instead of self.change_color I tried change_color(i) . 我尝试使用change_color(i)而不是self.change_color。 That threw an error. 那引发了一个错误。 Same with trying self.change_color(i) . 与尝试self.change_color(i)

I'm not sure what to do at this point. 我目前不确定该怎么办。

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()

i存储为类属性( self.i = 0 ),并将count的引用更改为self.i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM