簡體   English   中英

將變量從一個函數傳遞到同一類中的另一個函數?

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

我正在嘗試創建一個可在單擊時更改顏色的按鈕。

在翻閱了我多年沒有看過的一本老Python書之后,我已經了解到了制作按鈕的要點,但是我不知道如何將i傳遞給第二個函數,因此它會遞增和然后將重置為0

我想我可以在第一個功能中單擊以增加我的數量,但是現在我很煩,想找出答案。

我嘗試使用change_color(i)而不是self.change_color。 那引發了一個錯誤。 與嘗試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()

i存儲為類屬性( self.i = 0 ),並將count的引用更改為self.i

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM