标签和空格键错误

时间:2016-01-03 23:28:44

标签: python tkinter

它只是提出一个错误,我不知道如何解决它?它说有一个不一致的选项卡和空格的使用???请有人帮助我,非常感谢! :)我有原始代码,但我不会这个工作,因为我稍微改变它,所以我可以给出原始,如果它会有所帮助。

import tkinter 
import random

class Game(object):
    block_size =100
    def __init__(self, parent):
    parent.title('Tic Tac Toe')
    self.parent = parent

    self.initialize_game()

def initialize_game(self):
    self.board = [None, None, None, None, None, None, None, None, None]
    self.map = {(0, 0): 0, (0, 1): 1, (0, 2): 2, (1, 0): 3, (1, 1): 4, (1, 2): 5, (2, 0): 6, (2, 1): 7, (2, 2):8}
    self.top_frame = tkinter.Frame(self, parent)
    self.top_frame.pack(side = tkinter.TOP)

    #add restart button on top frame
    restart_button = tkinter.Button(self.top_frame, text = 'Restart', width = 20, command = self.restart)
    restart_button.pack()

    self.bottom_frame = tkinter.Frame(self.parent)
    self.bottom_frame.pack(side = tkinter.BOTTOM)

    self.my_lbl = tkinter.Label(self.bottom_frame, text = None)
    self.my_lbl.pack()

    #create a canvas to draw our board on the top frame
    self.canvas = tkinter.Canvas(self.top_frame, width = self.block_size *3, height = self.block_size *3)

    # draw 3x3 visible blocks on the canvas
    for ro in range(3):
        for col in range(3):
            self.canvas.create_rectangle(self.block_size *col, self.block_size * ro, self.block_size * (col+ 1), self.block_size *(ro + 1), fill = 'white')

    # bind entire canvas with left click handler (play function)
    self.canvas.bind("<Button-1>", self.play)
    self.canvas.pack()

def board_full(self):
    if None not in self.board:
        return True # true for full
    else:
        return False # false for not full

def possible_moves(self):
    possible_moves = []
    for i in rnage(0, 9 ):
        if self.board[i] is None:   # if cell untaken
            possible_moves.append(i)    # append the cell number to the list 
        else:
            pass    # cell taken, dont append
    return possible_moves   # return list of possible moves

def pc_move(self):
    m = True
    while m:
        pc_move = random.randint(0, 8)  # randomly generate a number from 0 to 8
        if pc_move in self.possible_move ():    # if the number is a possible move
            self.board[pc_move] = 'O'   # mark O
            self.canvas.itemconfigure(tagOrId = (pc_move + 1), fill = 'blue')
            m = False   # exit loop
        else:           # not a possible move
            continue    # re-do
    return self

def draw_out(self):
    print(self.board[0:3])
    print(self.board[3:6])
    print(self.board[6:9])

def play(self, event):  #This method is invoked when the user clicks on a square
    print('clicked', event,y, event.x)
    cx = self.canvas.canvasx(event.x)
    cy = self.canvas.canvasy(event.y)
    cid = self.canvas.find_closesy(cx, cy)[0]
    my_move = self.map[(cy // self.block_size, cx // self.block_size)]
    if self.board[my_move] is None:
        self.Board[my_move] = 'X'
        self.canvas.itemconfigure(cid, fill = 'green')
    else:
        return None

    self.draw_out()
    if self.check_gmae()is not None:
        print(self.check_game())
    else:
        pass

    self.possible_moves()
    self.pc_move()
    self.draw_out()

    if self.check_game()is not None:
        print(self.check_game())
    else:
        pass

    return self # when the board is full, return
    def check_game(self):
            result=None
            if (self.board[0] == self.board[1] == self.board[2] == 'X') or (
                                        self.board[3] == self.board[4] == self.board[5] == 'X') or (
                                        self.board[6] == self.board[7] == self.board[8] == 'X') or (
                                        self.board[0] == self.board[3] == self.board[6] == 'X') or (
                                        self.board[1] == self.board[4] == self.board[7] == 'X') or (
                                        self.board[2] == self.board[5] == self.board[8] == 'X') or (
                                        self.board[0] == self.board[4] == self.board[8] == 'X') or (
                                        self.board[2] == self.board[4] == self.board[6] == 'X'):
                        result = 'You win!'  # player win
                        self.my_lbl.configure(text=result)
                    elif (self.board[0] == self.board[1] == self.board[2] == 'O') or (
                                        self.board[3] == self.board[4] == self.board[5] == 'O') or (
                                        self.board[6] == self.board[7] == self.board[8] == 'O') or (
                                        self.board[0] == self.board[3] == self.board[6] == 'O') or (
                                        self.board[1] == self.board[4] == self.board[7] == 'O') or (
                                        self.board[2] == self.board[5] == self.board[8] == 'O') or (
                                        self.board[0] == self.board[4] == self.board[8] == 'O') or (
                                        self.board[2] == self.board[4] == self.board[6] == 'O'):
                        result = 'You lost!'  # player lose
                        self.my_lbl.config(text=result)
                    elif self.board_full()is True:
                            result = 'A tie!'  # tie
                            self.my_lbl.configure(text=result)
                    else:
                        pass
                    return result                      





    def restart(self):
            """ Reinitialize the game and board after restart button is pressed """
            self.top_frame.destroy()
            self.bottom_frame.destroy()
            self.initialize_game()


def main():
    root = tkinter.Tk()  # Instantiate a root window
    my_game = Game(root)  # Instantiate a Game object
    root.mainloop()  # Enter the main event loop


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

以下是修正缩进的代码:

import tkinter
import random

class Game(object):
    block_size =100
    def __init__(self, parent):
        parent.title('Tic Tac Toe')
        self.parent = parent

        self.initialize_game()

    def initialize_game(self):
        self.board = [None, None, None, None, None, None, None, None, None]
        self.map = {(0, 0): 0, (0, 1): 1, (0, 2): 2, (1, 0): 3, (1, 1): 4, (1, 2): 5, (2, 0): 6, (2, 1): 7, (2, 2):8}
        self.top_frame = tkinter.Frame(self, parent)
        self.top_frame.pack(side = tkinter.TOP)

        #add restart button on top frame
        restart_button = tkinter.Button(self.top_frame, text = 'Restart', width = 20, command = self.restart)
        restart_button.pack()

        self.bottom_frame = tkinter.Frame(self.parent)
        self.bottom_frame.pack(side = tkinter.BOTTOM)

        self.my_lbl = tkinter.Label(self.bottom_frame, text = None)
        self.my_lbl.pack()

        #create a canvas to draw our board on the top frame
        self.canvas = tkinter.Canvas(self.top_frame, width = self.block_size *3, height = self.block_size *3)

        # draw 3x3 visible blocks on the canvas
        for ro in range(3):
            for col in range(3):
                self.canvas.create_rectangle(self.block_size *col, self.block_size * ro, self.block_size * (col+ 1), self.block_size *(ro + 1), fill = 'white')

        # bind entire canvas with left click handler (play function)
        self.canvas.bind("<Button-1>", self.play)
        self.canvas.pack()

    def board_full(self):
        if None not in self.board:
            return True # true for full
        else:
            return False # false for not full

    def possible_moves(self):
        possible_moves = []
        for i in rnage(0, 9 ):
            if self.board[i] is None:   # if cell untaken
                possible_moves.append(i)    # append the cell number to the list
        else:
            pass    # cell taken, dont append
        return possible_moves   # return list of possible moves

    def pc_move(self):
        m = True
        while m:
            pc_move = random.randint(0, 8)  # randomly generate a number from 0 to 8
            if pc_move in self.possible_move ():    # if the number is a possible move
                self.board[pc_move] = 'O'   # mark O
                self.canvas.itemconfigure(tagOrId = (pc_move + 1), fill = 'blue')
                m = False   # exit loop
            else:           # not a possible move
                continue    # re-do
                return self

    def draw_out(self):
        print(self.board[0:3])
        print(self.board[3:6])
        print(self.board[6:9])

    def play(self, event):  #This method is invoked when the user clicks on a square
        print('clicked', event,y, event.x)
        cx = self.canvas.canvasx(event.x)
        cy = self.canvas.canvasy(event.y)
        cid = self.canvas.find_closesy(cx, cy)[0]
        my_move = self.map[(cy // self.block_size, cx // self.block_size)]
        if self.board[my_move] is None:
            self.Board[my_move] = 'X'
            self.canvas.itemconfigure(cid, fill = 'green')
        else:
            return None

        self.draw_out()
        if self.check_gmae()is not None:
            print(self.check_game())
        else:
            pass

        self.possible_moves()
        self.pc_move()
        self.draw_out()

        if self.check_game()is not None:
            print(self.check_game())
        else:
            pass
        return self # when the board is full, return    

    def check_game(self):
        result=None
        if (self.board[0] == self.board[1] == self.board[2] == 'X') or (
                                self.board[3] == self.board[4] == self.board[5] == 'X') or (
                                self.board[6] == self.board[7] == self.board[8] == 'X') or (
                                self.board[0] == self.board[3] == self.board[6] == 'X') or (
                                self.board[1] == self.board[4] == self.board[7] == 'X') or (
                                self.board[2] == self.board[5] == self.board[8] == 'X') or (
                                self.board[0] == self.board[4] == self.board[8] == 'X') or (
                                self.board[2] == self.board[4] == self.board[6] == 'X'):
            result = 'You win!'  # player win
            self.my_lbl.configure(text=result)
        elif (self.board[0] == self.board[1] == self.board[2] == 'O') or (
                                self.board[3] == self.board[4] == self.board[5] == 'O') or (
                                self.board[6] == self.board[7] == self.board[8] == 'O') or (
                                self.board[0] == self.board[3] == self.board[6] == 'O') or (
                                self.board[1] == self.board[4] == self.board[7] == 'O') or (
                                self.board[2] == self.board[5] == self.board[8] == 'O') or (
                                self.board[0] == self.board[4] == self.board[8] == 'O') or (
                                self.board[2] == self.board[4] == self.board[6] == 'O'):
            result = 'You lost!'  # player lose
            self.my_lbl.config(text=result)
        elif self.board_full()is True:
            result = 'A tie!'  # tie
            self.my_lbl.configure(text=result)
        else:
            pass
            return result

        def restart(self):
            """ Reinitialize the game and board after restart button is pressed """
            self.top_frame.destroy()
            self.bottom_frame.destroy()
            self.initialize_game()


def main():
    root = tkinter.Tk()  # Instantiate a root window
    my_game = Game(root)  # Instantiate a Game object
    root.mainloop()  # Enter the main event loop


if __name__ == '__main__':
    main()

警告您的代码仍有问题,但它们与缩进无关。如果您希望修复这些问题,我建议您在发现问题时更新到新问题,因为问题似乎深入到您的代码中。