Python Forum

Full Version: Invalid command error with Entry object
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I begin in python programmation and i have some problems. I tried to make a handman game with tkinter GUI but I encountered some errors that I don't understand. There are some photos of the code with this post. Can anyone help me pls? thank you

Je parle aussi français
No images please, post your code.
(May-18-2023, 07:14 PM)Axel_Erfurt Wrote: [ -> ]No images please, post your code.

from donnees import *
from tkinter import *
import pickle
from pathlib import Path
from donnees import *
from random import choice
import os


root = Tk()
root.geometry("1080x720")
root.title("Jeu du Pendu")
root.iconbitmap("jeu-du-pendu.ico")


def saisie_utilisateur():
    user_entry = frame.entry2.get()
    frame.entry2.delete(0, END)
    return user_entry


class Interface(Frame):

    def __init__(self, fenetre, **kwargs):
        Frame.__init__(self, fenetre, width=1080, height=720, **kwargs)
        self.pack(expand=YES)

        self.entry1 = Entry(self, bg="#ffe599", width=80, font=("Helvetica", 25))
        self.entry2 = Entry(self, bg="#a2b3d8", width=80, font=("Helvetica", 25))

        self.entry1.pack()
        self.entry2.pack(pady=20)

        self.button = Button(self, text="Entrer", bg="#9999ff", width=20, height=1, fg="white", font=("Helvetica", 20),
                             command=saisie_utilisateur)
        self.button.pack(pady=30)


frame = Interface(root)


def recuperer_scores() -> dict[str, int]:
    chemin_scores = Path(NOM_FICHIER_SCORES)
    if chemin_scores.exists and os.path.getsize(chemin_scores) > 0:
        with chemin_scores.open("rb") as fichier_scores:
            scores = pickle.load(fichier_scores)
    else:
        scores = {}
    return scores


def enregistrer_scores(scores: dict[str, int]):
    chemin_scores = Path(NOM_FICHIER_SCORES)
    with chemin_scores.open("wb") as fichier_scores:
        pickle.dump(scores, fichier_scores)


def enregistrer_nom_utilisateur() -> str:
    frame.entry1.delete(0, END)
    frame.entry1.insert(0, "Saississez votre pseudo dans l'encadré bleu.")
    nom_utilisateur = saisie_utilisateur()
    nom_utilisateur = nom_utilisateur.capitalize()
    if not nom_utilisateur.isalnum():
        frame.entry1.delete(0, END)
        frame.entry1.insert(0, "Le pseudo saisi n'est pas valide.")
        return enregistrer_nom_utilisateur()
    else:
        return nom_utilisateur


def recuperer_lettre() -> str:
    lettre = input("\nSaississez une lettre contenue dans le mot caché:")
    lettre = lettre.lower()
    if not lettre.isalpha() or len(lettre) > 1:
        print("\nVeuillez saisir une lettre valide:")
        return recuperer_lettre()
    else:
        return lettre


def recuperer_mot_masque(mot_complet: str, lettres_trouvees: set[str]) -> str:
    mot_masque = ""
    for lettre in mot_complet:
        if lettre in lettres_trouvees:
            mot_masque += lettre
        else:
            mot_masque += "*"
    return mot_masque


def choisir_mot() -> str:
    mot_choisi = choice(liste_solutions)
    return mot_choisi


root.mainloop()
enregistrer_nom_utilisateur()
error message:

Error:
C:\Users\Eli._.am\AppData\Local\Programs\Python\Python311\pythonw.exe "C:\Users\Eli._.am\PycharmProjects\1\Pendu Ameliore\fonctions.py" Traceback (most recent call last): File "C:\Users\Eli._.am\PycharmProjects\1\Pendu Ameliore\fonctions.py", line 97, in <module> enregistrer_nom_utilisateur() File "C:\Users\Eli._.am\PycharmProjects\1\Pendu Ameliore\fonctions.py", line 59, in enregistrer_nom_utilisateur frame.entry1.delete(0, END) File "C:\Users\Eli._.am\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 3105, in delete self.tk.call(self._w, 'delete', first, last) _tkinter.TclError: invalid command name ".!interface.!entry"
The reason for your error is because of this:
root.mainloop()
enregistrer_nom_utilisateur()
mainloop() runs until the window is closed. When the window is closed, all the widgets (frames, entries) are deleted. When you call enregistrer_nom_utilisateur(), there is not a frame.entry1 object anymore. When you try to delete the text in frame.entry1 this causes an error.

You can look here to see a tkinter version of hangman.

https://www.codespeedy.com/hangman-game-...g-tkinter/
(May-18-2023, 08:36 PM)deanhystad Wrote: [ -> ]The reason for your error is because of this:
root.mainloop()
enregistrer_nom_utilisateur()
mainloop() runs until the window is closed. When the window is closed, all the widgets (frames, entries) are deleted. When you call enregistrer_nom_utilisateur(), there is not a frame.entry1 object anymore. When you try to delete the text in frame.entry1 this causes an error.

You can look here to see a tkinter version of hangman.

https://www.codespeedy.com/hangman-game-...g-tkinter/
thank you but I get this error message after inverting mainloop() and enregistrer_nom_utilisateur() places:

Error:
C:\Users\Eli._.am\AppData\Local\Programs\Python\Python311\pythonw.exe "C:\Users\Eli._.am\PycharmProjects\1\Pendu Ameliore\fonctions.py" Traceback (most recent call last): File "C:\Users\Eli._.am\PycharmProjects\1\Pendu Ameliore\fonctions.py", line 96, in <module> enregistrer_nom_utilisateur() File "C:\Users\Eli._.am\PycharmProjects\1\Pendu Ameliore\fonctions.py", line 66, in enregistrer_nom_utilisateur return enregistrer_nom_utilisateur() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Eli._.am\PycharmProjects\1\Pendu Ameliore\fonctions.py", line 66, in enregistrer_nom_utilisateur return enregistrer_nom_utilisateur() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Eli._.am\PycharmProjects\1\Pendu Ameliore\fonctions.py", line 66, in enregistrer_nom_utilisateur return enregistrer_nom_utilisateur() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ [Previous line repeated 993 more times] File "C:\Users\Eli._.am\PycharmProjects\1\Pendu Ameliore\fonctions.py", line 61, in enregistrer_nom_utilisateur nom_utilisateur = saisie_utilisateur() ^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Eli._.am\PycharmProjects\1\Pendu Ameliore\fonctions.py", line 17, in saisie_utilisateur user_entry = frame.entry2.get() ^^^^^^^^^^^^^^^^^^ File "C:\Users\Eli._.am\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 3109, in get return self.tk.call(self._w, 'get') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RecursionError: maximum recursion depth exceeded while calling a Python object Process finished with exit code 1
That is because your game logic is wrong. enregistrer_nom_utilisateur() should not call itself. It should be called when you press Entrer. Did you check out that link in my previous post. Your code is a mess. It shows that you do not understand how to organize a tkinter application. That link will help you understand how tkinter works, understand the event driven nature of GUI programming which is very different from script programming.
(May-18-2023, 09:19 PM)deanhystad Wrote: [ -> ]That is because your game logic is wrong. enregistrer_nom_utilisateur() should not call itself. It should be called when you press Entrer. Did you check out that link in my previous post. Your code is a mess. It shows that you do not understand how to organize a tkinter application. That link will help you understand how tkinter works, understand the event driven nature of GUI programming which is very different from script programming.
ok thank you