首页 > 解决方案 > 在使用 class() 方法时从函数中的另一个函数访问变量

问题描述

我想知道是否有任何方法可以通过不同的函数访问变量?下面是我的代码,在 isWinner() 内部,我想访问按钮的文本。怎么可能做到?在此处输入图像描述

class ttt():
    master = 0
    player1 = 0
    player2 = 0
    bclick = True
    buttonCounter = 1
    win = 0
    lose = 0

    def boardDisplay(self):
        button1 = tk.Button(self.master, text=" ", command=lambda: self.buttonClick(button1), height=4, width=10)
        button1.place(x=275, y=150)
        button2 = tk.Button(self.master, text=" ", command=lambda: self.buttonClick(button2), height=4, width=10)
        button2.place(x=355, y=150)
        button3 = tk.Button(self.master, text=" ", command=lambda: self.buttonClick(button3), height=4, width=10)
        button3.place(x=435, y=150)
        button4 = tk.Button(self.master, text=" ", command=lambda: self.buttonClick(button4), height=4, width=10)
        button4.place(x=275, y=230)
        button5 = tk.Button(self.master, text=" ", command=lambda: self.buttonClick(button5), height=4, width=10)
        button5.place(x=355, y=230)
        button6 = tk.Button(self.master, text=" ", command=lambda: self.buttonClick(button6), height=4, width=10)
        button6.place(x=435, y=230)
        button7 = tk.Button(self.master, text=" ", command=lambda: self.buttonClick(button7), height=4, width=10)
        button7.place(x=275, y=310)
        button8 = tk.Button(self.master, text=" ", command=lambda: self.buttonClick(button8), height=4, width=10)
        button8.place(x=355, y=310)
        button9 = tk.Button(self.master, text=" ", command=lambda: self.buttonClick(button9), height=4, width=10)
        button9.place(x=435, y=310)

    def buttonClick(self, button):
        if button["text"] == " " and self.bclick == True:
            button["text"] = "X"
            self.bclick = False
            self.isWinner()
            self.buttonCounter += 1
        elif button["text"] == " " and self.bclick == False:
            button["text"] = "O"
            self.bclick = True
            self.isWinner()
            self.buttonCounter += 1
        else:
            tk.messagebox.showinfo("Tic Tac Toe", "Button is clicked already!")

    def isWinner(self): 

标签: pythonclasstkinter

解决方案


为您的按钮定义一个子类,该子类定义了单击时要调用的方法。大部分工作ttt也可以委托给子类。子类的每个实例都保留对“容器”的引用(在这种情况下,是 的实例ttt)。

class ttt():
    master = 0
    player1 = 0
    player2 = 0
    bclick = True
    buttonCounter = 1
    win = 0
    lose = 0

    def boardDisplay(self):
        button1 = MyButton(self, x=275, y=150)
        button2 = MyButton(self, x=355, y=150)
        button3 = MyButton(self, x=435, y=150)
        button4 = MyButton(self, x=275, y=230)
        button5 = MyButton(self, x=355, y=230)
        button6 = MyButton(self, x=435, y=230)
        button7 = MyButton(self, x=275, y=310)
        button8 = MyButton(self, x=355, y=310)
        button9 = MyButton(self, x=435, y=310)


class MyButton(tk.Button):
    def __init__(self, container, x, y, h=4, w=10):
        super().__init__(container.master, " ", command=self.onClick, height=h, width=w)
        self.container = container
        self.place(x, y)

    def onClick(self):
        if self["text" != " ":
            tk.messagebox.showinfo("Tic Tac Toe", "Button is clicked already!")
        elif self.container.bclick:
            self["text"] = "X"
            self.container.bclick = False
            self.container.isWinner()
            self.container.buttonCounter += 1
        else:
            self["text"] = "O"
            self.container.bclick = True
            self.container.isWinner()
            self.container.buttonCounter += 1


            

推荐阅读