2016-07-23 126 views
1
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()python,tkinter特定按钮

编辑: 我编辑我的代码,你现在可以运行它。我想创建井字棋游戏。这个代码应该为两个玩家创建游戏。如果你点击按钮,按钮的文字将会改变。功能self.mark()的目的是改变按钮的文本,并检查是否有人胜利。它的工作原理,但我想加我的机器人/电脑。因此,当我做我的举动后(点击按钮),机器人会做他的举动。会有什么功能找到最佳的举动,并做这个动作(它应该改变特定的按钮的文本,而不需要点击此按钮)。 如何在不点击此按钮的情况下更改特定按钮的文本?

+1

如何运行此代码? –

+0

他们每个人都会调用相同的功能吗? –

+0

我编辑了我的问题。 – Marty

回答

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