简体   繁体   English

我如何对 python 中的对象列表进行深度复制

[英]how can i deepcopy list of objects in python

Hello guys i have been in this bug for 4 days and i still did not finish the project and never solved the bug.大家好,我在这个错误中已经有 4 天了,但我仍然没有完成项目并且从未解决过这个错误。 the bug appears while im trying to deepcopy tkinter button in tictactoe project to check for player win in player vs pc mode, but everytime i copy a button and change it to x for example to check for win of the pc's enemy as if it is ai, it is also changing the original button that is breaking the app and couldn't do anything next.当我尝试在 tictactoe 项目中深度复制 tkinter 按钮以检查玩家与 PC 模式下的玩家胜利时,会出现该错误,但每次我复制一个按钮并将其更改为 x 时,例如检查 PC 敌人的胜利,就好像它是 ai ,它也改变了破坏应用程序的原始按钮,接下来无法执行任何操作。 this is the last step of my project.这是我项目的最后一步。 However, i tried numpy.copy() which didn't work and copy.deepcopy() which raised:但是,我尝试了 numpy.copy() 没有工作和 copy.deepcopy() 提出:

TypeError: can't pickle _tkinter.tkapp objects TypeError:无法腌制 _tkinter.tkapp 对象

That's the part of the code that has the error specifically with the commented lines那是代码的一部分,特别是带有注释行的错误

def free_spot(i, j): # Check for free spots in the board
    return buttons_grid[i][j]['text'] == ''

def enemy_strategy():
        global turn

        corners = [[0, 0], [2, 0], [2, 2], [0, 2]]
        if len(free_places()) == 9:
            any_corner = rd.choice(corners)
            buttons_grid[any_corner[0]][any_corner[-1]].config(text='O')
            turn = 'player'
            sign_label.config(text='Your turn')

        elif free_spot(1, 1):
            buttons_grid[1][1].config(text='O')
            turn = 'player'

        else:
                 
            for x, i in enumerate(free_places()):
                grid_copy = array(buttons_grid)
                grid_copy[i[0]][i[-1]].config(text='O')

                # board_copy[i[0]][i[-1]] = np.copy(buttons_grid)
                # board_copy[..][..].config(text='X')

                if game_check(grid_copy) == 1:
                    buttons_grid[i[0]][i[-1]].config(text='O')
                    sign_label.config(text="Enemy Won!")
                    break
                
                # elif game_check(board_copy) == 1:
                #     return buttons_grid[..][..].config(text='O')
                # else:
                #     board_copy[..][..].config(text='')

                elif game_check(grid_copy) == 0:
                    buttons_grid[i[0]][i[-1]].config(text='O')
                    sign_label.config(text="Draw")
                    break

                elif game_check(grid_copy) == -1:
                    grid_copy[i[0]][i[-1]].config(text='')

                    if x == len(free_places())-1:
                        buttons_grid[i[0]][i[-1]].config(text='O')
                        turn = 'player'
                        sign_config(sign_label, 'Your')
                    else:
                        continue

I've already Done a Rock Paper Scissors app with tkinter and i'm always wanting to move forward and do a more complex project each time.我已经用 tkinter 完成了一个石头剪刀布应用程序,而且我总是想继续前进,每次都做一个更复杂的项目。 please help me as if i'm still a beginner 18 years old请帮助我,就好像我还是 18 岁的初学者一样

furthermore, i saw an algo called minimax but i think that over complexity compared to my level that's why i used normal functions and if statements instead of minimax此外,我看到了一个名为 minimax 的算法,但我认为与我的水平相比过于复杂,这就是我使用普通函数和 if 语句而不是 minimax 的原因

Thanks a lot in advance.提前非常感谢。

Note: buttons_grid is a 3x3 nested list of tkinter buttons注意:buttons_grid 是一个 3x3 嵌套的 tkinter 按钮列表

You cannot deep copy or pickle tkinter objects.您不能深度复制或腌制 tkinter 对象。 There are no workarounds for that.没有解决方法。 Tkinter objects are just a wrapper around objects that exist inside of an embedded Tcl interpreter. Tkinter 对象只是存在于嵌入式 Tcl 解释器内部的对象的包装器。 This Tcl interpreter knows nothing about python objects and python has no way to pickle tcl objects.这个 Tcl 解释器对 python 对象一无所知,并且 python 没有办法腌制 tcl 对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM