from tkinter import *
player1 = []
player2 = []
class MakeSq:
def __init__(self, master):
self.master = master
self.sqs()
self.again()
def sqs(self):
self.square_frame = (self.master)
self.square_frame.grid()
self.quan = 17 # make this game universal, you can add more squares
for s in range(1, self.quan * self.quan + 1, 1):
self.b = s
self.b = Button(self.master, width=3, command=lambda x=s: self.mark(x), font=('times', 16))
self.b['command'] = lambda x=s, which=self.b: self.mark(x, which)
self.place_it(s) # place square on the right place
self.b.grid(row=self.r, column=self.c)
def place_it(self, s):
self.r = 0
while s > self.quan:
s -= self.quan
self.r += 1
self.c = s % (self.quan + 1)
def mark(self,x , which):
if len(player1) == len(player2):
player1.append(x)
else:
player2.append(x)
if len(player1) == len(player2):
p = 'O'
p_c = 'dark blue'
else:
p = 'X'
p_c = 'orange'
which.config(text=p, fg=p_c, command='')
self.check_win(player1)
self.check_win(player2)
def check_win(self, player):
for p in player: # iba p + 4 moze byt delitelne 17, kluc k vyrieseniu problemu s riadkami
if p + 1 in player and p + 2 in player and p + 3 in player and p + 4 in player:
print('win')
elif p + 17 in player and p + 34 in player and p + 51 in player and p + 68 in player:
print('win')
elif p + 18 in player and p + 36 in player and p + 54 in player and p + 72 in player:
print('win')
elif p + 16 in player and p + 32 in player and p + 48 in player and p + 64 in player:
print('win')
def again(self):
self.again_bt = Button(self.master, text='New game', command=self.restart)
self.again_bt.grid(column=200)
def restart(self):
self.sqs()
player1.clear()
player2.clear()
def show_result(self):
pass
root = Tk()
plan = MakeSq(root)
root.mainloop()
此代码创建289个按钮。我如何具体说明每个按钮?例如,如果我单击按钮编号50,则此按钮将激活功能self.mark()
,但如何在不单击按钮的情况下激活特定按钮的功能self.mark()
。
修改
我编辑了我的代码,你现在可以运行它。我想创造Tic-Tac-Toe游戏。此代码应为两个玩家创建游戏。如果单击按钮,按钮文本将更改。功能self.mark()
的目的是更改按钮的文本并检查是否有人获胜。它有效,但是我想要添加机器人/计算机来对付我。所以在我移动(点击按钮)之后,机器人将会采取行动。将会有找到最佳动作的功能,并执行此操作(它应该更改特定按钮的文本而不单击此按钮)。 如何在不点击此按钮的情况下更改特定按钮的文字?
答案 0 :(得分:1)
您正在使用.grid()方法来放置/分组您的小部件(主要是按钮),以便您可以使用它来识别按钮所在的行/列。
def mark(self,x , which):
...
# Lets say human is using 'X' marks (player1)
# So the bot is left with 'O'.
if p == 'X': self.BotMakeMove(which) # Human made a move so it's Bots turn
def BotMakeMove(self, button):
row = int(button.grid_info()['row'])
column = int(button.grid_info()['column'])
print ("Human pressed button at index: {} {}".format(row, column))
# Do some calculation
#
#
# You're done and lets say you wont to press the button right of humans
row = row # ...
column = column + 1
self.press(row, column)
def press(self, row, column):
for children in self.master.children.values():
if children.grid_info()['row'] == str(row) and children.grid_info()['column'] == str(column) and children['command']:
player2.append(0) # You should pass that 'x' value here.
children.config(text='O', fg='dark blue', command='')
return True
print ("Button at index {} {} doesnt exist or is being used.".format(row, column))