python - 在使用 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):
解决方案
为您的按钮定义一个子类,该子类定义了单击时要调用的方法。大部分工作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
推荐阅读
- javascript - 为什么 howSum 解决方案在 Javascript 中有效,但在 Python 中无效?(动态编程)
- xcode - (SWIFTUI) 从表单中读取数据,然后在不同视图的列表中显示数据
- python - 错误“NoneType”对象在 sqlalchemy 中没有属性“twophase”
- c# - Dictionary.ContainsValue 的复杂性是什么?
- r - 在 R 中对多行进行分组过滤
- powerbi - PowerBI Matrix Visual - 用零替换空白(比我想象的要难)
- python - 有没有办法在 Python 中获取表情符号列表
- apache-kafka - Kafka 消息存储在集群/集合中,但不会被消费者检索
- php - PHP FPDF不打印西班牙字母
- angular - 模块“AppModule”声明的意外值“HttpClientModule”。请添加@Pipe/@Directive/@Component 注解