Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 制表符和空格键错误_Python_Tkinter - Fatal编程技术网

Python 制表符和空格键错误

Python 制表符和空格键错误,python,tkinter,Python,Tkinter,它只是带来了一个错误,我不知道如何修复它??它说制表符和空格的使用不一致???请有人帮助我,非常感谢!:)我有原始代码,但我不会让这一个工作,因为我有轻微的修改,所以我可以给出原始的,如果它会有所帮助 import tkinter import random class Game(object): block_size =100 def __init__(self, parent): parent.title('Tic Tac Toe') self.paren

它只是带来了一个错误,我不知道如何修复它??它说制表符和空格的使用不一致???请有人帮助我,非常感谢!:)我有原始代码,但我不会让这一个工作,因为我有轻微的修改,所以我可以给出原始的,如果它会有所帮助

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()
导入tkinter
随机输入
班级游戏(对象):
块大小=100
定义初始化(自身,父级):
父标题(“Tic Tac Toe”)
self.parent=parent
self.initialize_game()
def初始化_游戏(自我):
self.board=[无,无,无,无,无,无,无,无]
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)
自顶部框架组件(侧面=顶部)
#在顶部框架上添加重新启动按钮
重新启动按钮=tkinter.button(self.top_框架,文本='restart',宽度=20,命令=self.restart)
重新启动按钮。包()
self.bottom\u frame=tkinter.frame(self.parent)
自底包装(侧面=底部)
self.my_lbl=tkinter.Label(self.bottom_frame,text=None)
self.my_lbl.pack()
#创建画布,在顶部框架上绘制我们的板
self.canvas=tkinter.canvas(self.top\u框架,宽度=self.block\u大小*3,高度=self.block\u大小*3)
#在画布上绘制3x3个可见块
对于范围(3)内的ro:
对于范围(3)中的列:
self.canvas.create_矩形(self.block_size*col,self.block_size*ro,self.block_size*(col+1),self.block_size*(ro+1),fill='white')
#使用左键单击处理程序绑定整个画布(播放功能)
self.canvas.bind(“,self.play)
self.canvas.pack()
def板已满(自身):
如果self.board中没有:
返回真值#完全返回真值
其他:
返回False#False表示未满
def可能的_移动(自):
可能的_移动=[]
对于rnage中的i(0,9):
如果self.board[i]为None:#如果单元格未打开
可能的移动。追加(i)#将单元格编号追加到列表中
其他:
通过#单元格已占用,不追加
返回可能的移动#返回可能移动的列表
def pc_移动(自):
m=真
而m:
pc_move=random.randint(0,8)#随机生成一个从0到8的数字
if pc_move in self.mable_move():#如果号码是可能的移动
self.board[pc_move]=“O”#标记O
self.canvas.itemconfigure(tagOrId=(pc_move+1),fill='blue')
m=假#退出循环
否则:#不可能采取行动
继续#重做
回归自我
def抽出(自):
打印(自板[0:3])
打印(self.board[3:6])
打印(self.board[6:9])
def play(self,event):#当用户单击正方形时调用此方法
打印('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\u move=self.map[(cy//self.block\u size,cx//self.block\u size)]
如果self.board[我的移动]为无:
self.Board[我的移动]=“X”
self.canvas.itemconfigure(cid,fill='green')
其他:
一无所获
self.draw_out()
如果self.check_gmae()不是无:
打印(self.check_game())
其他:
通过
self.可能的_移动()
self.pc_move()
self.draw_out()
如果self.check_game()不是无:
打印(self.check_game())
其他:
通过
return self#当棋盘已满时,返回
def检查_游戏(自我):
结果=无
如果(self.board[0]==self.board[1]==self.board[2]=='X')或(
self.board[3]==self.board[4]==self.board[5]=='X')或(
self.board[6]==self.board[7]==self.board[8]=='X')或(
self.board[0]==self.board[3]==self.board[6]=='X')或(
self.board[1]==self.board[4]==self.board[7]=='X')或(
self.board[2]==self.board[5]==self.board[8]=='X')或(
self.board[0]==self.board[4]==self.board[8]=='X')或(
self.board[2]==self.board[4]==self.board[6]=='X'):
结果=‘你赢了!’球员获胜
self.my lbl.configure(文本=结果)
elif(self.board[0]==self.board[1]==self.board[2]==O')或(
self.board[3]==self.board[4]==self.board[5]=='O')或(
self.board[6]==self.board[7]==self.board[8]=='O')或(
self.board[0]==self.board[3]==self.board[6]=='O')或(
self.board[1]==self.board[4]==self.board[7]=='O')或(
self.board[2]==self.board[5]==self.board[8]=='O')或(
self.board[0]==self.board[4]==self.board[8]=='O')或(
self.board[2]==self.board[4]==self.board[6]==O'):
结果=‘你输了!’玩家输
self.my_lbl.config(text=result)
elif self.board_full()为真:
结果=‘平局!’领带
self.my lbl.configure(文本=结果)
其他:
通过
返回结果
def重启(自我):
“”“按下重新启动按钮后重新初始化游戏和棋盘”“”
self.top_frame.destroy()
self.bottom_frame.destroy()
self.initialize_game()
def main():
root=tkinter.Tk()#实例化一个根窗口
我的游戏=游戏(根)#
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()