Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error in code _tkinter.TclError: bad window path name ".!button4"

After 2-nd press of buttons paper/scissors/rock gives an error:

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

How can I fix it?

Here’s the code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

after EACH paper/scissors/rock button press, what happens after the first paper/scissors/rock button press should happen. but after 2-nd press of buttons paper/scissors/rock gives an error:
_tkinter.TclError: bad window path name ".!button4"

>Solution :

error in code _tkinter.TclError: bad window path name ".!button4"

The problem can be fixed.

Comment out line 66 and move this restart_btn.place(x=350, y=320) after line 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

Comment out line 82 and 82

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

You ready to go.

Edit:

Screenshot:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading