簡體   English   中英

如何使用類中的函數更改先前分配的變量

[英]How to change previously assigned variable with a function in a class

我正在開發一個游戲,在該游戲中,我有一個登錄屏幕,然后是一個主菜單,然后是主游戲窗口。 在主菜單中,我有一個按鈕,該按鈕應更改游戲的難度,這將導致目標半徑的變化。 難度越高,半徑越小。 當我分配變量半徑,然后嘗試使用按鈕(在班級內部)進行更改時,它將不起作用,而是使用先前定義的半徑。

我嘗試設置許多不同的全局變量。

difficulty = -1

class Application(Frame):
    def __init__(self, master):
        super().__init__(master)

        self.difficulty = -1

        self.grid()
        self.login = self.create_main()
        self.read = None



    def changeVariable1(self):
        self.difficulty = 12

    def changeVariable2(self):
        self.difficulty = 16

    def changeVariable3(self):
        self.difficulty = 20


    def diff(self):
        global radius
        if difficulty == 12:
            radius = (30)
        elif difficulty == 16:
            radius = (20)
        elif difficulty == 20:
            radius = (10)

    def create_read(self):
        read = Toplevel()

        Button(read, text="Easy", font='Helvetica 10 bold', command=self.changeVariable1()).grid(row=3, column=2)
        Button(read, text="Medium", font='Helvetica 10 bold', command=self.changeVariable2()).grid(row=3, column=3)
        Button(read, text="Hard", font='Helvetica 10 bold', command=self.changeVariable3()).grid(row=3, column=4)

        return read

root = Tk()

app = Application(root)

root.mainloop()

我希望當我單擊一個按鈕時,容易,中等,困難會改變將半徑設置為相應值的難度。

我敢打賭,錯誤在於您如何調用Button.__init__()

    Button(read, text="Easy", font='Helvetica 10 bold', command=self.changeVariable1()).grid(row=3, column=2)
    Button(read, text="Medium", font='Helvetica 10 bold', command=self.changeVariable2()).grid(row=3, column=3)
    Button(read, text="Hard", font='Helvetica 10 bold', command=self.changeVariable3()).grid(row=3, column=4)

您正在嘗試將command分配給Button例如: command=self.changeVariable1()

在Python中,函數和方法都是實例, command需要函數實例,但是您將結果指定為self.changeVariable1() '。

刪除括號應該可以解決:

Button([...], command=self.changeVariable1)

編輯:我敢打賭大黃蜂的答案也是對的,你需要兩個修復方法:)

在您的APplication類中,您聲明了difficulty變量,並使用某些值對其進行了初始化。 這是可以使用self.difficulty訪問的類變量。 但是,當您根據difficulty變量更改半徑值時,實際上是在訪問全局實例。 您無需保留可變difficulty的全局實例。

修改為:

class Application(Frame):
    def __init__(self, master):
        super().__init__(master)

        self.difficulty = -1

        self.grid()
        self.login = self.create_main()
        self.read = None

    def changeVariable1(self):
        self.difficulty = 12

    def changeVariable2(self):
        self.difficulty = 16

    def changeVariable3(self):
        self.difficulty = 20


    def diff(self):
        global radius
        if self.difficulty == 12:
            radius = (30)
        elif self.difficulty == 16:
            radius = (20)
        elif self.difficulty == 20:
            radius = (10)

    def create_read(self):
        read = Toplevel()

        Button(read, text="Easy", font='Helvetica 10 bold', command=self.changeVariable1()).grid(row=3, column=2)
        Button(read, text="Medium", font='Helvetica 10 bold', command=self.changeVariable2()).grid(row=3, column=3)
        Button(read, text="Hard", font='Helvetica 10 bold', command=self.changeVariable3()).grid(row=3, column=4)

        return read

root = Tk()

app = Application(root)

root.mainloop()

我希望這有幫助!。

暫無
暫無

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

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