第2次按下按钮后,纸/剪刀/石头会出现错误:

_tkinter.TclError: bad window path name ".!button4"

我该如何修复它?

代码如下:

import tkinter as tk
from PIL import ImageTk
from random import randint

root = tk.Tk()
root.geometry('700x512')
root.resizable(0, 0)
player_selection = ''
psr = ['p', 's', 'r']
ai_selection = ''
game_result = ''
index = 0
ai_image, player_image = '', ''
imgpaper = ImageTk.PhotoImage(file='paper.png')
imgscissors = ImageTk.PhotoImage(file='scissors.png')
imgrock = ImageTk.PhotoImage(file='rock.png')
imgempty = ImageTk.PhotoImage(file='empty.png')

def ai_moves():
    global ai_selection, index
    index = randint(0, 2)
    ai_selection = psr[index]
    image_to_output_ai()
    win_check()

def image_to_output_ai():
    global ai_image, ai_selection
    if ai_selection == 'p':
        ai_image = imgpaper
    elif ai_selection == 's':
        ai_image = imgscissors
    elif ai_selection == 'r':
        ai_image = imgrock
    elif ai_selection == '':
        ai_selection = imgempty

def image_to_output_player():
    global player_image
    if player_selection == 'p':
        player_image = imgpaper
    elif player_selection == 's':
        player_image = imgscissors
    elif player_selection == 'r':
        player_image = imgrock
    elif player_selection == '':
        player_image = imgempty

def block_buttons():
    btnpaper.config(state='disabled')
    btnscissors.config(state='disabled')
    btnrock.config(state='disabled')

def win_check():
    global game_result, ai_selection
    global ai_image
    image_to_output_player()
    player_label.config(image=player_image)
    ai_label.config(image=ai_image)
    if player_selection == ai_selection:
        game_result = 'draw'
    elif (player_selection == 'p' and ai_selection == 'r') or (player_selection == 's' and ai_selection == 'p') or (player_selection == 'r' and ai_selection == 's'):
        game_result = 'win'
    elif (ai_selection == 'p' and player_selection == 'r') or (ai_selection == 's' and player_selection == 'p') or (ai_selection == 'r' and player_selection == 's'):
        game_result = 'lose'
    block_buttons()
    restart_btn.place(x=350, y=320)
    print(game_result)

def player_selection_def(what):
    global player_selection
    player_selection = what
    ai_moves()

def restart():
    player_label.config(image=imgempty)
    ai_label.config(image=imgempty)
    btnpaper.config(state='normal')
    btnscissors.config(state='normal')
    btnrock.config(state='normal')
    ai_selection, player_selection = '', ''
    ai_image, player_image = '', ''
    if 'restart_btn' in globals():
        restart_btn.destroy()

btnpaper = tk.Button(root, text='paper', font='arial 18 bold', command=lambda: player_selection_def('p'))
btnpaper.place(width=135, height=54, x=50, y=400)
btnscissors = tk.Button(root, text='scissors', font='arial 18 bold', command=lambda: player_selection_def('s'))
btnscissors.place(width=135, height=54, x=280, y=400)
btnrock = tk.Button(root, text='rock', font='arial 18 bold', command=lambda: player_selection_def('r'))
btnrock.place(width=135, height=54, x=525, y=400)
player_label = tk.Label(root)
player_label.place(x=50, y=50)
ai_label = tk.Label(root)
ai_label.place(x=400, y=50)
label_win_lose = tk.Label(text='', font='arial 18 bold')
label_win_lose.place(x=350, y=300)
restart_btn = tk.Button(text='try again', font='arial 18 bold', command=restart)
root.mainloop()

每次按下布/剪刀/摇滚按钮后,第一次按下布/剪刀/摇滚按钮后都会发生什么.但第2次按下按钮后,纸/剪刀/石头会出现错误: _tkinter. Tcl错误:错误的窗口路径名称".!按钮4"

推荐答案

code _tkinter. Tcl错误:错误的窗口路径名称".!按钮4"

这个问题可以解决.

注释掉第66行,并将此restart_btn.place(x=350, y=320)移至第97行之后.

restart_btn = tk.Button(text='try again', font='arial 18 bold', command=restart)
restart_btn.place(x=350, y=520) #<== change value 320 to 520

注释掉第82和82行

#if restart_btn in globals():
    #restart_btn.destroy()

你准备好出发了.

编辑:

截图 :

enter image description here

Python相关问答推荐

Pandas 修改原始excel

在Docker中运行HAProxy时无法获得503服务

如何匹配3D圆柱体的轴和半径?

替换字符串中的点/逗号,以便可以将其转换为浮动

如何将Matplotlib的fig.add_axes本地坐标与我的坐标关联起来?

Python -Polars库中的滚动索引?

使用polars .滤镜进行切片速度比pandas .loc慢

列表上值总和最多为K(以O(log n))的最大元素数

如何在具有重复数据的pandas中对groupby进行总和,同时保留其他列

替换字符串中的多个重叠子字符串

如何使用symy打印方程?

聚合具有重复元素的Python字典列表,并添加具有重复元素数量的新键

从dict的列中分钟

NP.round解算数据后NP.unique

ODE集成中如何终止solve_ivp的无限运行

在Python中动态计算范围

迭代嵌套字典的值

为什么numpy. vectorize调用vectorized函数的次数比vector中的元素要多?

在Python中使用yaml渲染(多行字符串)

人口全部乱序 - Python—Matplotlib—映射