如何使用类中的函数更改先前分配的变量

问题描述 投票:0回答:2

我正在制作一个游戏,其中我有一个登录屏幕,然后是主菜单,然后是主游戏窗口。在主菜单中,我有一个按钮,可以改变游戏的难度,这将导致目标半径的变化。难度越高,半径越小。当我指定我的变量半径,然后尝试使用按钮(在类内)更改它时,它将无法工作,而是使用先前定义的半径。

我尝试过设置很多不同的全局变量。

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

我希望当我点击一个简单,中等,坚硬的按钮时,它会改变将半径设置为相应值的难度。

python tkinter
2个回答
1
投票

我打赌错误就是你怎么称呼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)

编辑:我打赌bumblebee的答案也是如此,你需要两个修复:)


0
投票

在你的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()

我希望这有帮助!。

© www.soinside.com 2019 - 2024. All rights reserved.